From 0f0f8166d4a1cb582c4b88a5c8fd370b052f2629 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Sun, 21 May 2023 11:58:08 +0200 Subject: [PATCH] Python CLI Package: added basic reconfiguration stuff --- python/BIRD/Actions.py | 40 ++++++++++++++++++++++++++++++++++++++++ python/BIRD/Basic.py | 2 ++ python/BIRD/__init__.py | 2 ++ python/test.py | 10 ++++++++++ 4 files changed, 54 insertions(+) create mode 100644 python/BIRD/Actions.py diff --git a/python/BIRD/Actions.py b/python/BIRD/Actions.py new file mode 100644 index 00000000..32ddf74b --- /dev/null +++ b/python/BIRD/Actions.py @@ -0,0 +1,40 @@ +from BIRD.Basic import Basic, Code + +class Actions(Basic): + def __init__(self, **kwargs): + super().__init__(**kwargs) + + class ConfigureState: + def __init__(self, bird): + self.bird = bird + self.data = {} + self.configure_dispatch = { + Code.ReadingConfiguration: self.reading, + Code.Reconfigured: self.done, + } + + def reading(self, data): + if "reading_from" in self.data: + raise ActionException(f"Duplicit configuration file name in response: {data}") + + if not data.startswith(pfx := "Reading configuration from "): + raise ActionException(f"Malformed configuration file name notice in response: {data}") + + self.data["reading_from"] = data[len(pfx):] + + def done(self, data): + if "done" in self.data: + raise ActionException(f"Reconfiguration finished twice") + + self.data["done"] = True + + async def configure(self): + await self.bird.cli.open() + data = await self.bird.cli.socket.command("configure") + state = self.ConfigureState(self.bird) + + for line in data: + state.configure_dispatch[line["code"]](line["data"]) + + return state.data + diff --git a/python/BIRD/Basic.py b/python/BIRD/Basic.py index 01ff0d6f..537a6f40 100644 --- a/python/BIRD/Basic.py +++ b/python/BIRD/Basic.py @@ -27,6 +27,8 @@ class Basic: class Code: OK = 0 Welcome = 1 + ReadingConfiguration = 2 + Reconfigured = 3 Status = 13 Version = 1000 ProtocolInfo = 1002 diff --git a/python/BIRD/__init__.py b/python/BIRD/__init__.py index 5511be89..16b13595 100644 --- a/python/BIRD/__init__.py +++ b/python/BIRD/__init__.py @@ -6,6 +6,7 @@ from BIRD.Basic import BIRDException from BIRD.Socket import Socket from BIRD.Status import Status, Version from BIRD.Protocol import ProtocolList +from BIRD.Actions import Actions from BIRD.Config import Timestamp, ProtocolConfig, DeviceProtocolConfig @@ -115,6 +116,7 @@ class BIRD: self.version = Version(bird=self) self.status = Status(bird=self) self.protocols = ProtocolList(bird=self) + self.actions = Actions(bird=self) self.within = False diff --git a/python/test.py b/python/test.py index 67252d5f..c303b358 100644 --- a/python/test.py +++ b/python/test.py @@ -17,4 +17,14 @@ async def main(): for name, channel in protocol.channels.items(): print(f" {name}: {channel.route_change_stats}") + print(await b.actions.configure()) + + await b.protocols.update() + print(b.protocols) + + for name, protocol in b.protocols.data.items(): + print(f"{name}: {protocol.channels}") + for name, channel in protocol.channels.items(): + print(f" {name}: {channel.route_change_stats}") + asyncio.run(main())