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 to_directory(self, parent_dir: str) -> None:
63 Method converting the network to a subdirectory file structure
65 self.__network.to_directory(parent_dir)
66 print(f'Directory structure saved to "{parent_dir}"')
68 def save(self, filename: str, compressed: bool = True) -> None:
70 Method saving the class content to a file in json format.
71 :param filename: A valid path to a file on the system.
72 :param compressed: if True, svg is stored as svgz format.
73 :type filename: string
75 output: dict[str, Any] = self.__network.to_topology()
76 if compressed is True:
77 with gzip.open(f'{filename}-operational.json.gz', 'wb') as zf:
78 zf.write(json.dumps(output, indent=2).encode('utf-8'))
79 print(f'File "{filename}-operational.json.gz" saved!')
82 f'{filename}-operational.json', "w", encoding="utf-8"
84 json.dump(output, jf, ensure_ascii=False, indent=2)
85 print(f'File "{filename}-operational.json" saved!')
87 def readStylesFromFile(self) -> str:
89 Method reading the css styles from known file
90 return: content of the file as string
92 with open("network_generation/view/svg.style.css") as styles:
93 content = styles.read()
96 def svg(self, filename: str, compressed: bool = True) -> None:
98 Method saving the class content to a file in xml/svg format.
100 :param filename: A valid path to a file on the system.
101 :param compressed: if True, svg is stored as svgz format.
102 :type filename: string
104 root = self.__network.toSvg()
105 style = ET.Element("style")
106 style.text = self.readStylesFromFile()
107 root.findall(".//desc")[0].append(style)
109 if compressed is True:
110 svg_output: Buffer = ET.tostring(
111 root, encoding="utf-8", xml_declaration=True)
112 with gzip.open(f'{filename}.svgz', 'wb') as zf:
114 print(f'File "{filename}.svgz" saved!')
116 ET.ElementTree(root).write(
117 f'{filename}.svg', encoding="utf-8", xml_declaration=True
119 print(f'File "{filename}.svg" saved!')
121 def kml(self, filename: str, compressed: bool = True) -> None:
123 Method saving the class content to a file in xml/kml format.
125 :param filename: A valid path to a file on the system.
126 :param compressed: if True, kml is stored as kmz format.
127 :type filename: string
129 root = self.__network.toKml()
130 with open("network_generation/view/kml.styles.json") as kml_styles:
131 styles: dict[str, dict] = json.load(kml_styles)
132 for key, value in styles.items():
134 style = ET.Element("Style", {"id": key})
135 line_style = ET.SubElement(style, "LineStyle")
136 color = ET.SubElement(line_style, "color")
137 color.text = str(value["stroke"]["color"])
138 width = ET.SubElement(line_style, "width")
139 width.text = str(value["stroke"]["width"])
140 poly_style = ET.SubElement(style, "PolyStyle")
141 fill = ET.SubElement(poly_style, "color")
142 fill.text = str(value["fill"]["color"])
143 root.findall(".//Document")[0].append(style)
145 kml: str = ET.tostring(
146 root, encoding="utf-8", xml_declaration=True)
147 if compressed is True:
148 with zipfile.ZipFile(
149 f'{filename}.kmz', 'w', zipfile.ZIP_DEFLATED
151 zf.writestr(f'{filename.split("/")[1]}.kml', data=kml)
152 print(f'File "{filename}.kmz" saved!')
154 kml_file = open(f'{filename}.kml', 'w')
157 print(f'File "{filename}.kml" saved!')