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:
parent
11bd97b69b
commit
49ac4bd161
@ -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),
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user