]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Python Config Generator: protocols can have names and options
authorMaria Matejka <mq@ucw.cz>
Thu, 18 May 2023 11:29:41 +0000 (13:29 +0200)
committerMaria Matejka <mq@ucw.cz>
Tue, 23 May 2023 11:44:48 +0000 (13:44 +0200)
python/BIRD/Config.py
python/BIRD/__init__.py
python/conftest.py

index 1c9d7298b7d203f4a55b204b4b83526bfc7a8787..79208f5ff6960e3e5aeb78ff1caa66edb65589bc 100644 (file)
@@ -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),
+            }
index bec04011c56b0331ec5ed005887fefa578a8197d..74eff5534282f38ec2b6aa47ae56c85c07044a1b 100644 (file)
@@ -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)
index 50cfd46cd3bd3095c91e3cd4ccacf76d97e4b11f..97cb68c98c8ccb85c902ecc2598c3c8983e17895 100644 (file)
@@ -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")