0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 17:51:53 +00:00

Python Config Generator: protocols can have names and options

This commit is contained in:
Maria Matejka 2023-05-18 13:29:41 +02:00
parent 11bd97b69b
commit 49ac4bd161
3 changed files with 51 additions and 9 deletions

View File

@ -15,15 +15,47 @@ class Timestamp(ConfigObject):
super().__init__() super().__init__()
self.comment = f"{comment} at {datetime.now()}" 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): 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__() super().__init__()
self.name = name self.name = name
if template is not None: if template is not None:
raise NotImplementedError() 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): 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): def __str__(self):
inside = self.block_inside(1) inside = self.block_inside(1)
@ -32,9 +64,12 @@ class ProtocolConfig(ConfigObject):
if inside is None: if inside is None:
return header + " {}\n" return header + " {}\n"
else: else:
return header + " {\n" + inside + "}\n" return header + " {" + inside + "\n}\n"
class DeviceProtocolConfig(ProtocolConfig): class DeviceProtocolConfig(ProtocolConfig):
name_prefix = "device" name_prefix = "device"
protocol_type = "device" protocol_type = "device"
options = ProtocolConfig.options | {
"scan_time": BlockOption("scan time", int, 60),
}

View File

@ -24,7 +24,6 @@ class Config:
def __enter__(self): def __enter__(self):
if self.config.auto_device: if self.config.auto_device:
self.auto_device = DeviceProtocolConfig() self.auto_device = DeviceProtocolConfig()
self.config.add(self.auto_device)
self.begin = Timestamp("Config dump started") self.begin = Timestamp("Config dump started")
self.config.add(self.begin) self.config.add(self.begin)
@ -33,15 +32,20 @@ class Config:
def dump(self, _file): def dump(self, _file):
for i in self.config._items: for i in self.config._items:
if i is not None: if i is None:
_file.write(str(i)) 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"))) _file.write(str(Timestamp("Config dump finished")))
def __exit__(self, *args): def __exit__(self, *args):
self.config.remove(self.begin) self.config.remove(self.begin)
if self.auto_device is not None:
self.config.remove(self.auto_device)
def finalized(self): def finalized(self):
return self.FinalizedConfig(self) return self.FinalizedConfig(self)

View File

@ -1,4 +1,7 @@
from BIRD import Config 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")