1 # Copyright 2022 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 lxml import etree
22 from model.python.tapi_common_context import TapiCommonContext
27 This class contains all functions converting the Network into different formats
29 __network: TapiCommonContext = None
32 def __init__(self, network: TapiCommonContext):
33 self.__network = network
37 def json(self) -> 'NetworkViewer':
39 Getter returns the class as json object
40 :return The class itsself, 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.json()
64 json.dump(output, json_file,
65 ensure_ascii=False, indent=2)
66 for key in ["Node", "Link"]:
67 print(key + "s:", len(output
68 ["tapi-common:context"]
69 ["tapi-topology:topology-context"]
70 ["topology"][0][key.lower()])
72 print("File '" + filename + "' saved!")
74 def readStylesFromFile(self) -> str:
76 Method reading the css styles from known file
77 return: content of the file as string
79 with open('view/svg.style.css') as styles:
80 content = styles.read()
83 def svg(self, filename: str):
85 Method saving the class content to a file in xml/svg format.
87 :param filename: A valid path to a file on the system.
88 :type filename: string
90 root = self.__network.svg(0, 0)
91 # not preferred see OAM-257
93 # etree.ProcessingInstruction("xml-stylesheet",
94 # 'href="svg.style.css" type="text/css"')
96 style = etree.Element("style")
97 style.text = self.readStylesFromFile()
98 root.getchildren()[0].addnext(style)
99 etree.ElementTree(root).write(filename,
101 xml_declaration=True,
103 '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"\n'
104 ' "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'
108 print("File '" + filename + "' saved!")