1 # Copyright 2023 highstreet technologies GmbH
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 Provides functions to convert the Network into different formats
21 from typing import Dict
22 from model.python.o_ran_network import ORanNetwork
23 import xml.etree.ElementTree as ET
28 This class contains all functions converting the Network into different formats
30 __network: ORanNetwork = None
33 def __init__(self, network: ORanNetwork):
34 self.__network = network
38 def json(self) -> 'NetworkViewer':
40 Getter returns the class as json object
41 :return The class itself, as it is json serializable
45 def show_as_json(self) -> dict:
47 Method printing the class in json format.
49 print(self.__network.json())
53 Method printing the network
57 def save(self, filename: str):
59 Method saving the class content to a file in json format.
60 :param filename: A valid path to a file on the system.
61 :type filename: string
63 with open(filename, "w", encoding='utf-8') as json_file:
64 output = self.__network.toTopology()
65 json.dump(output, json_file,
66 ensure_ascii=False, indent=2)
67 print("File '" + filename + "' saved!")
69 def readStylesFromFile(self) -> str:
71 Method reading the css styles from known file
72 return: content of the file as string
74 with open('view/svg.style.css') as styles:
75 content = styles.read()
78 def svg(self, filename: str):
80 Method saving the class content to a file in xml/svg format.
82 :param filename: A valid path to a file on the system.
83 :type filename: string
85 root = self.__network.toSvg()
86 style = ET.Element("style")
87 style.text = self.readStylesFromFile()
88 root.findall(".//desc")[0].append(style)
89 ET.ElementTree(root).write(filename,
93 print("File '" + filename + "' saved!")
95 def kml(self, filename: str):
97 Method saving the class content to a file in xml/kml format.
99 :param filename: A valid path to a file on the system.
100 :type filename: string
102 root = self.__network.toKml()
103 with open('view/kml.styles.json') as kml_styles:
104 styles:Dict[str,Dict] = json.load(kml_styles)
105 for key, value in styles.items():
107 style = ET.Element("Style",{"id":key})
108 line_style = ET.SubElement(style, "LineStyle")
109 color = ET.SubElement(line_style, "color")
110 color.text = value['stroke']['color']
111 width = ET.SubElement(line_style, "width")
112 width.text = value['stroke']['width']
113 poly_style = ET.SubElement(style, "PolyStyle")
114 fill = ET.SubElement(poly_style, "color")
115 fill.text = value['fill']['color']
116 root.findall(".//Document")[0].append(style)
118 ET.ElementTree(root).write(filename,
122 print("File '" + filename + "' saved!")