]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
YANG: CBOR -> JSON basic converter
authorMaria Matejka <mq@ucw.cz>
Wed, 15 Nov 2023 12:56:37 +0000 (13:56 +0100)
committerMaria Matejka <mq@ucw.cz>
Wed, 15 Nov 2023 12:56:37 +0000 (13:56 +0100)
yang/JSON_CBOR.py [new file with mode: 0644]
yang/cbor-json-yang [new file with mode: 0755]
yang/cbor-json-yang.py [new file with mode: 0644]
yang/json-cbor-yang
yang/json-cbor-yang.py
yang/show_memory.json

diff --git a/yang/JSON_CBOR.py b/yang/JSON_CBOR.py
new file mode 100644 (file)
index 0000000..ccbfc35
--- /dev/null
@@ -0,0 +1,14 @@
+from yangson import DataModel
+
+import cbor
+import json
+
+class Message:
+    def __init__(self, filename, _type):
+        self.filename = filename
+        with open(self.filename, "rb") as sf:
+            self.raw = { "json": json, "cbor": cbor }[_type].load(sf)
+        
+        self.dm = DataModel.from_file('yang-library.json')
+        self.data = self.dm.from_raw(self.raw)
+        self.data.validate()
diff --git a/yang/cbor-json-yang b/yang/cbor-json-yang
new file mode 100755 (executable)
index 0000000..a31b769
--- /dev/null
@@ -0,0 +1,5 @@
+python3 -m venv venv
+. venv/bin/activate
+pip3 install yangson cbor
+
+python3 cbor-json-yang.py "$@"
diff --git a/yang/cbor-json-yang.py b/yang/cbor-json-yang.py
new file mode 100644 (file)
index 0000000..2b4f79f
--- /dev/null
@@ -0,0 +1,13 @@
+import cbor
+import json
+import sys
+
+import JSON_CBOR
+
+#print(sys.argv[1])
+
+msg = JSON_CBOR.Message(sys.argv[1], "cbor")
+with open(sys.argv[2], "w") as of:
+    json.dump(msg.raw, of, indent=2)
+
+print("OK")
index 24b746176d4a3fe13981d840a6b17d11604a116f..97e9546ac2d757e837180b3ed715fad1d035b85f 100755 (executable)
@@ -3,4 +3,3 @@ python3 -m venv venv
 pip3 install yangson cbor
 
 python3 json-cbor-yang.py "$@"
-#python3 json2cbor.py "$@"
index 14b0a1b7d06cae924c41149e2b42a3445d4003f1..f613303b370b3087274c27a7c478cb8fb9f9e809 100644 (file)
@@ -1,23 +1,12 @@
-from yangson import DataModel
 import cbor
 import json
 import sys
 
-class Message:
-    def __init__(self, filename):
-        self.filename = filename
-        with open(self.filename) as sf:
-            self.raw = json.load(sf)
-
-        self.dm = DataModel.from_file('yang-library.json')
-        self.data = self.dm.from_raw(self.raw)
-        self.data.validate()
+import JSON_CBOR
 
 #print(sys.argv[1])
 
-msg = Message(sys.argv[1])
-#print(msg, msg.raw)
-#print(cbor.dumps(msg.raw))
+msg = JSON_CBOR.Message(sys.argv[1], "json")
 with open(sys.argv[2], "wb") as of:
     cbor.dump(msg.raw, of)
 
index f5c712794475772c678becde6334957d2bb4f0dd..85c972f181955c5ff1e7433b5fcba1ae8d4e9a20 100644 (file)
@@ -1,31 +1,31 @@
 {
   "show_memory:message": {
-     "header": "BIRD memory usage",
-     "body": {
-       "routing_tables": {
-         "effective": 324,
-         "overhead": 48
-       },
-       "route_attributes": {
-         "effective": 324,
-         "overhead": 48
-       },
-       "protocols": {
-         "effective": 324,
-         "overhead": 48
-       },
-       "current_config": {
-         "effective": 324,
-         "overhead": 48
-       },
-       "standby_memory": {
-         "effective": 324,
-         "overhead": 48
-       },
-       "total": {
-           "effective": 324,
-           "overhead": 48
-       }
-     }
+    "header": "BIRD memory usage",
+    "body": {
+      "routing_tables": {
+        "effective": 324,
+        "overhead": 48
+      },
+      "route_attributes": {
+        "effective": 324,
+        "overhead": 48
+      },
+      "protocols": {
+        "effective": 324,
+        "overhead": 48
+      },
+      "current_config": {
+        "effective": 324,
+        "overhead": 48
+      },
+      "standby_memory": {
+        "effective": 324,
+        "overhead": 48
+      },
+      "total": {
+        "effective": 324,
+        "overhead": 48
+      }
+    }
   }
 }