config: make the TuhiConfig a singleton object too

We only ever have one of those anyway, so let's make it a singleton. This
patch keeps the self.config assignment and config passing to keep the churn at
at a minimum. But this way we can assign runtime options to the config and
have those as globally accessible options.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2019-08-21 13:17:20 +10:00
parent 6e4b190169
commit 23989e4291
2 changed files with 19 additions and 12 deletions

View File

@ -302,7 +302,7 @@ class Tuhi(GObject.Object):
self.bluez.connect('discovery-started', self._on_bluez_discovery_started)
self.bluez.connect('discovery-stopped', self._on_bluez_discovery_stopped)
self.config = TuhiConfig(config_dir)
self.config = TuhiConfig()
self.devices = {}
@ -452,6 +452,7 @@ def main(args=sys.argv):
default=DEFAULT_CONFIG_PATH)
ns = parser.parse_args(args[1:])
TuhiConfig.set_base_path(ns.config_dir)
setup_logging(ns.config_dir)
if ns.verbose:

View File

@ -28,14 +28,20 @@ def is_btaddr(addr):
class TuhiConfig(GObject.Object):
def __init__(self, config_dir):
super().__init__()
self.config_dir = config_dir
logger.debug(f'Using config directory: {self.config_dir}')
Path(config_dir).mkdir(parents=True, exist_ok=True)
_instance = None
_base_path = None
self._devices = {}
self._scan_config_dir()
def __new__(cls):
if cls._instance is None:
cls._instance = super(TuhiConfig, cls).__new__(cls)
self = cls._instance
self.__init__() # for GObject to initialize
logger.debug(f'Using config directory: {self._base_path}')
Path(self._base_path).mkdir(parents=True, exist_ok=True)
self._devices = {}
self._scan_config_dir()
return cls._instance
@GObject.Property
def devices(self):
@ -45,7 +51,7 @@ class TuhiConfig(GObject.Object):
return self._devices
def _scan_config_dir(self):
dirs = [d for d in Path(self.config_dir).iterdir() if d.is_dir() and is_btaddr(d.name)]
dirs = [d for d in Path(self._base_path).iterdir() if d.is_dir() and is_btaddr(d.name)]
for directory in dirs:
settings = Path(directory, 'settings.ini')
if not settings.is_file():
@ -69,7 +75,7 @@ class TuhiConfig(GObject.Object):
assert protocol != ProtocolVersion.ANY
logger.debug(f'{address}: adding new config, UUID {uuid}')
path = Path(self.config_dir, address)
path = Path(self._base_path, address)
path.mkdir(exist_ok=True)
# The ConfigParser default is to write out options as lowercase, but
@ -103,7 +109,7 @@ class TuhiConfig(GObject.Object):
return
logger.debug(f'{address}: adding new drawing, timestamp {drawing.timestamp}')
path = Path(self.config_dir, address, f'{drawing.timestamp}.json')
path = Path(self._base_path, address, f'{drawing.timestamp}.json')
with open(path, 'w') as f:
f.write(drawing.to_json())
@ -114,7 +120,7 @@ class TuhiConfig(GObject.Object):
if address not in self.devices:
return []
configdir = Path(self.config_dir, address)
configdir = Path(self._base_path, address)
return [Drawing.from_json(f) for f in configdir.glob('*.json')]
def _purge_drawings(self, directory):