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
22 from typing_extensions import Buffer
24 import xml.etree.ElementTree as ET
25 from typing import Any
27 from network_generation.model.python.o_ran_network import ORanNetwork
32 This class contains all functions converting the Network into
37 def __init__(self, network: ORanNetwork) -> None:
38 self.__network = network
42 def json(self) -> "NetworkViewer":
44 Getter returns the class as json object
45 :return The class itself, as it is json serializable
49 def show_as_json(self) -> None:
51 Method printing the class in json format.
53 print(self.__network.json())
55 def show(self) -> None:
57 Method printing the network
61 def save(self, filename: str, compressed: bool = True) -> None:
63 Method saving the class content to a file in json format.
64 :param filename: A valid path to a file on the system.
65 :param compressed: if True, svg is stored as svgz format.
66 :type filename: string
68 output: dict[str, Any] = self.__network.to_topology()
69 if compressed is True:
70 with gzip.open(f'{filename}-operational.json.gz', 'wb') as zf:
71 zf.write(json.dumps(output, indent=2).encode('utf-8'))
72 print(f'File "{filename}-operational.json.gz" saved!')
75 f'{filename}-operational.json', "w", encoding="utf-8"
77 json.dump(output, jf, ensure_ascii=False, indent=2)
78 print(f'File "{filename}-operational.json" saved!')
80 def readStylesFromFile(self) -> str:
82 Method reading the css styles from known file
83 return: content of the file as string
85 with open("network_generation/view/svg.style.css") as styles:
86 content = styles.read()
89 def svg(self, filename: str, compressed: bool = True) -> None:
91 Method saving the class content to a file in xml/svg format.
93 :param filename: A valid path to a file on the system.
94 :param compressed: if True, svg is stored as svgz format.
95 :type filename: string
97 root = self.__network.toSvg()
98 style = ET.Element("style")
99 style.text = self.readStylesFromFile()
100 root.findall(".//desc")[0].append(style)
102 if compressed is True:
103 svg_output: Buffer = ET.tostring(
104 root, encoding="utf-8", xml_declaration=True)
105 with gzip.open(f'{filename}.svgz', 'wb') as zf:
107 print(f'File "{filename}.svgz" saved!')
109 ET.ElementTree(root).write(
110 f'{filename}.svg', encoding="utf-8", xml_declaration=True
112 print(f'File "{filename}.svg" saved!')
114 def kml(self, filename: str, compressed: bool = True) -> None:
116 Method saving the class content to a file in xml/kml format.
118 :param filename: A valid path to a file on the system.
119 :param compressed: if True, kml is stored as kmz format.
120 :type filename: string
122 root = self.__network.toKml()
123 with open("network_generation/view/kml.styles.json") as kml_styles:
124 styles: dict[str, dict] = json.load(kml_styles)
125 for key, value in styles.items():
127 style = ET.Element("Style", {"id": key})
128 line_style = ET.SubElement(style, "LineStyle")
129 color = ET.SubElement(line_style, "color")
130 color.text = str(value["stroke"]["color"])
131 width = ET.SubElement(line_style, "width")
132 width.text = str(value["stroke"]["width"])
133 poly_style = ET.SubElement(style, "PolyStyle")
134 fill = ET.SubElement(poly_style, "color")
135 fill.text = str(value["fill"]["color"])
136 root.findall(".//Document")[0].append(style)
138 kml: str = ET.tostring(
139 root, encoding="utf-8", xml_declaration=True)
140 if compressed is True:
141 with zipfile.ZipFile(
142 f'{filename}.kmz', 'w', zipfile.ZIP_DEFLATED
144 zf.writestr(f'{filename.split("/")[1]}.kml', data=kml)
145 print(f'File "{filename}.kmz" saved!')
147 kml_file = open(f'{filename}.kml', 'w')
150 print(f'File "{filename}.kml" saved!')