linting ...
[oam.git] / code / network-generator / network_generation / view / network_viewer.py
1 # Copyright 2023 highstreet technologies GmbH
2 #
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
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # !/usr/bin/python
16 """
17 Provides functions to convert the Network into different formats
18 """
19
20 import json
21 import xml.etree.ElementTree as ET
22 from typing import Any
23
24 from network_generation.model.python.o_ran_network import ORanNetwork
25
26
27 class NetworkViewer:
28     """
29     This class contains all functions converting the Network into
30     different formats
31     """
32
33     # constructor
34     def __init__(self, network: ORanNetwork) -> None:
35         self.__network = network
36
37     # json format
38
39     def json(self) -> "NetworkViewer":
40         """
41         Getter returns the class as json object
42         :return The class itself, as it is json serializable
43         """
44         return self
45
46     def show_as_json(self) -> None:
47         """
48         Method printing the class in json format.
49         """
50         print(self.__network.json())
51
52     def show(self) -> None:
53         """
54         Method printing the network
55         """
56         print(self.__network)
57
58     def save(self, filename: str) -> None:
59         """
60         Method saving the class content to a file in json format.
61         :param filename: A valid path to a file on the system.
62         :type filename: string
63         """
64         with open(filename, "w", encoding="utf-8") as json_file:
65             output: dict[str, Any] = self.__network.to_topology()
66             json.dump(output, json_file, ensure_ascii=False, indent=2)
67             print("File '" + filename + "' saved!")
68
69     def readStylesFromFile(self) -> str:
70         """
71         Method reading the css styles from known file
72         return: content of the file as string
73         """
74         with open("network_generation/view/svg.style.css") as styles:
75             content = styles.read()
76             return content
77
78     def svg(self, filename: str) -> None:
79         """
80         Method saving the class content to a file in xml/svg format.
81
82         :param filename: A valid path to a file on the system.
83         :type filename: string
84         """
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(
90             filename, encoding="utf-8", xml_declaration=True
91         )
92         print("File '" + filename + "' saved!")
93
94     def kml(self, filename: str) -> None:
95         """
96         Method saving the class content to a file in xml/kml format.
97
98         :param filename: A valid path to a file on the system.
99         :type filename: string
100         """
101         root = self.__network.toKml()
102         with open("network_generation/view/kml.styles.json") as kml_styles:
103             styles: dict[str, dict] = json.load(kml_styles)
104             for key, value in styles.items():
105                 # add style
106                 style = ET.Element("Style", {"id": key})
107                 line_style = ET.SubElement(style, "LineStyle")
108                 color = ET.SubElement(line_style, "color")
109                 color.text = str(value["stroke"]["color"])
110                 width = ET.SubElement(line_style, "width")
111                 width.text = str(value["stroke"]["width"])
112                 poly_style = ET.SubElement(style, "PolyStyle")
113                 fill = ET.SubElement(poly_style, "color")
114                 fill.text = str(value["fill"]["color"])
115                 root.findall(".//Document")[0].append(style)
116
117         ET.ElementTree(root).write(
118             filename, encoding="utf-8", xml_declaration=True
119         )
120         print("File '" + filename + "' saved!")