Feature-9904: Enhancing NG-UI to enable Juju operational view dashboard
[osm/N2VC.git] / n2vc / utils.py
index e8cf64d..14ac5d1 100644 (file)
@@ -17,6 +17,7 @@ from juju.machine import Machine
 from juju.application import Application
 from juju.action import Action
 from juju.unit import Unit
+import yaml
 
 
 class N2VCDeploymentStatus(Enum):
@@ -100,3 +101,33 @@ DB_DATA = Dict(
         )
     }
 )
+
+
+def obj_to_yaml(obj: object) -> str:
+    """
+    Converts object to yaml format
+    :return: yaml data
+    """
+    # dump to yaml
+    dump_text = yaml.dump(obj, default_flow_style=False, indent=2)
+    # split lines
+    lines = dump_text.splitlines()
+    # remove !!python/object tags
+    yaml_text = ""
+    for line in lines:
+        index = line.find("!!python/object")
+        if index >= 0:
+            line = line[:index]
+        yaml_text += line + "\n"
+    return yaml_text
+
+
+def obj_to_dict(obj: object) -> dict:
+    """
+    Converts object to dictionary format
+    :return: dict data
+    """
+    # convert obj to yaml
+    yaml_text = obj_to_yaml(obj)
+    # parse to dict
+    return yaml.load(yaml_text, Loader=yaml.Loader)