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:
parent
6e4b190169
commit
23989e4291
|
@ -302,7 +302,7 @@ class Tuhi(GObject.Object):
|
||||||
self.bluez.connect('discovery-started', self._on_bluez_discovery_started)
|
self.bluez.connect('discovery-started', self._on_bluez_discovery_started)
|
||||||
self.bluez.connect('discovery-stopped', self._on_bluez_discovery_stopped)
|
self.bluez.connect('discovery-stopped', self._on_bluez_discovery_stopped)
|
||||||
|
|
||||||
self.config = TuhiConfig(config_dir)
|
self.config = TuhiConfig()
|
||||||
|
|
||||||
self.devices = {}
|
self.devices = {}
|
||||||
|
|
||||||
|
@ -452,6 +452,7 @@ def main(args=sys.argv):
|
||||||
default=DEFAULT_CONFIG_PATH)
|
default=DEFAULT_CONFIG_PATH)
|
||||||
|
|
||||||
ns = parser.parse_args(args[1:])
|
ns = parser.parse_args(args[1:])
|
||||||
|
TuhiConfig.set_base_path(ns.config_dir)
|
||||||
setup_logging(ns.config_dir)
|
setup_logging(ns.config_dir)
|
||||||
|
|
||||||
if ns.verbose:
|
if ns.verbose:
|
||||||
|
|
|
@ -28,14 +28,20 @@ def is_btaddr(addr):
|
||||||
|
|
||||||
|
|
||||||
class TuhiConfig(GObject.Object):
|
class TuhiConfig(GObject.Object):
|
||||||
def __init__(self, config_dir):
|
_instance = None
|
||||||
super().__init__()
|
_base_path = None
|
||||||
self.config_dir = config_dir
|
|
||||||
logger.debug(f'Using config directory: {self.config_dir}')
|
|
||||||
Path(config_dir).mkdir(parents=True, exist_ok=True)
|
|
||||||
|
|
||||||
self._devices = {}
|
def __new__(cls):
|
||||||
self._scan_config_dir()
|
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
|
@GObject.Property
|
||||||
def devices(self):
|
def devices(self):
|
||||||
|
@ -45,7 +51,7 @@ class TuhiConfig(GObject.Object):
|
||||||
return self._devices
|
return self._devices
|
||||||
|
|
||||||
def _scan_config_dir(self):
|
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:
|
for directory in dirs:
|
||||||
settings = Path(directory, 'settings.ini')
|
settings = Path(directory, 'settings.ini')
|
||||||
if not settings.is_file():
|
if not settings.is_file():
|
||||||
|
@ -69,7 +75,7 @@ class TuhiConfig(GObject.Object):
|
||||||
assert protocol != ProtocolVersion.ANY
|
assert protocol != ProtocolVersion.ANY
|
||||||
|
|
||||||
logger.debug(f'{address}: adding new config, UUID {uuid}')
|
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)
|
path.mkdir(exist_ok=True)
|
||||||
|
|
||||||
# The ConfigParser default is to write out options as lowercase, but
|
# The ConfigParser default is to write out options as lowercase, but
|
||||||
|
@ -103,7 +109,7 @@ class TuhiConfig(GObject.Object):
|
||||||
return
|
return
|
||||||
|
|
||||||
logger.debug(f'{address}: adding new drawing, timestamp {drawing.timestamp}')
|
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:
|
with open(path, 'w') as f:
|
||||||
f.write(drawing.to_json())
|
f.write(drawing.to_json())
|
||||||
|
@ -114,7 +120,7 @@ class TuhiConfig(GObject.Object):
|
||||||
if address not in self.devices:
|
if address not in self.devices:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
configdir = Path(self.config_dir, address)
|
configdir = Path(self._base_path, address)
|
||||||
return [Drawing.from_json(f) for f in configdir.glob('*.json')]
|
return [Drawing.from_json(f) for f in configdir.glob('*.json')]
|
||||||
|
|
||||||
def _purge_drawings(self, directory):
|
def _purge_drawings(self, directory):
|
||||||
|
|
Loading…
Reference in New Issue