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 model.python.o_ran_network import ORanNetwork
22 import xml.etree.ElementTree as ET
27 This class contains all functions converting the Network into different formats
29 __network: ORanNetwork = None
32 def __init__(self, network: ORanNetwork):
33 self.__network = network
37 def json(self) -> 'NetworkViewer':
39 Getter returns the class as json object
40 :return The class itself, as it is json serializable
44 def show_as_json(self) -> dict:
46 Method printing the class in json format.
48 print(self.__network.json())
52 Method printing the network
56 def save(self, filename: str):
58 Method saving the class content to a file in json format.
59 :param filename: A valid path to a file on the system.
60 :type filename: string
62 with open(filename, "w", encoding='utf-8') as json_file:
63 output = self.__network.toTopology()
64 json.dump(output, json_file,
65 ensure_ascii=False, indent=2)
66 print("File '" + filename + "' saved!")
68 def readStylesFromFile(self) -> str:
70 Method reading the css styles from known file
71 return: content of the file as string
73 with open('view/svg.style.css') as styles:
74 content = styles.read()
77 def svg(self, filename: str):
79 Method saving the class content to a file in xml/svg format.
81 :param filename: A valid path to a file on the system.
82 :type filename: string
84 root = self.__network.toSvg()
85 style = ET.Element("style")
86 style.text = self.readStylesFromFile()
87 root.findall(".//desc")[0].append(style)
88 ET.ElementTree(root).write(filename,
92 print("File '" + filename + "' saved!")
94 def kml(self, filename: str):
96 Method saving the class content to a file in xml/kml format.
98 :param filename: A valid path to a file on the system.
99 :type filename: string
101 root = self.__network.toKml()
102 style = ET.Element("style")
103 style.text = self.readStylesFromFile()
105 ET.ElementTree(root).write(filename,
109 print("File '" + filename + "' saved!")