From 49ac4bd1616023493e706479e149e15ab634ea59 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Thu, 18 May 2023 13:29:41 +0200 Subject: [PATCH] Python Config Generator: protocols can have names and options --- python/BIRD/Config.py | 41 ++++++++++++++++++++++++++++++++++++++--- python/BIRD/__init__.py | 14 +++++++++----- python/conftest.py | 5 ++++- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/python/BIRD/Config.py b/python/BIRD/Config.py index 1c9d7298..79208f5f 100644 --- a/python/BIRD/Config.py +++ b/python/BIRD/Config.py @@ -15,15 +15,47 @@ class Timestamp(ConfigObject): super().__init__() self.comment = f"{comment} at {datetime.now()}" +class BlockOption: + def __init__(self, config_text, _type, value): + if not isinstance(value, _type): + raise Exception("BlockOption value doesn't match declared type") + + self.config_text = config_text + self._type = _type + self.value = value + + def set(self, value): + if value == self.value: + return self + else: + return type(self)(self.config_text, self._type, value) + class ProtocolConfig(ConfigObject): - def __init__(self, name=None, template=None): + options = { + "disabled": BlockOption("disabled", bool, False), + } + + def __init__(self, name=None, template=None, **kwargs): super().__init__() self.name = name + if template is not None: raise NotImplementedError() + self.options_set = {} + for k in kwargs: + if k not in self.options: + raise NotImplementedError() + + self.options_set[k] = self.options[k].set(kwargs[k]) + def block_inside(self, indent): - return None + if len(self.options_set) == 0: + return None + + return ("\n" + " " * indent).join([""] + [ + f"{opt.config_text} {opt.value};" for k,opt in self.options_set.items() if opt != self.options[k] + ]) def __str__(self): inside = self.block_inside(1) @@ -32,9 +64,12 @@ class ProtocolConfig(ConfigObject): if inside is None: return header + " {}\n" else: - return header + " {\n" + inside + "}\n" + return header + " {" + inside + "\n}\n" class DeviceProtocolConfig(ProtocolConfig): name_prefix = "device" protocol_type = "device" + options = ProtocolConfig.options | { + "scan_time": BlockOption("scan time", int, 60), + } diff --git a/python/BIRD/__init__.py b/python/BIRD/__init__.py index bec04011..74eff553 100644 --- a/python/BIRD/__init__.py +++ b/python/BIRD/__init__.py @@ -24,7 +24,6 @@ class Config: def __enter__(self): if self.config.auto_device: self.auto_device = DeviceProtocolConfig() - self.config.add(self.auto_device) self.begin = Timestamp("Config dump started") self.config.add(self.begin) @@ -33,15 +32,20 @@ class Config: def dump(self, _file): for i in self.config._items: - if i is not None: - _file.write(str(i)) + if i is None: + continue + if isinstance(i, DeviceProtocolConfig): + self.auto_device = None + + _file.write(str(i)) + + if self.auto_device is not None: + _file.write(str(self.auto_device)) _file.write(str(Timestamp("Config dump finished"))) def __exit__(self, *args): self.config.remove(self.begin) - if self.auto_device is not None: - self.config.remove(self.auto_device) def finalized(self): return self.FinalizedConfig(self) diff --git a/python/conftest.py b/python/conftest.py index 50cfd46c..97cb68c9 100644 --- a/python/conftest.py +++ b/python/conftest.py @@ -1,4 +1,7 @@ from BIRD import Config +from BIRD.Config import DeviceProtocolConfig -Config().write("test.conf") +cf = Config() +cf.add(DeviceProtocolConfig(name="foo", scan_time=42)) +cf.write("test.conf")