inflab4/main_task.py

135 lines
4.1 KiB
Python
Raw Normal View History

2023-07-20 22:08:19 +00:00
# Номер в ИСУ: 316304
# Вариант: 8
class XmlParser:
class Node:
parent: 'Node' = None
children = None
attribute: str = ''
depth: int = 0
def __init__(self, parent: 'Node', children, attribute: str, depth: int):
self.parent = parent
self.children = children
self.attribute = attribute
self.depth = depth
def __str__(self):
return f'<attribute="{self.attribute}", depth={self.depth}, parent={self.parent}, children={self.children}>'
def get_full_path(self, sep: str) -> str:
attributes = []
node = self
while node is not None:
attributes.append(node.attribute)
node = node.parent
path = sep.join(attributes[::-1])
return path
root: Node = None
xml = ''
json = ''
proto3 = ''
wml = ''
tsv = ['', '']
csv = ['', '']
def parse_xml(self, xml: str):
self.xml = xml
node = None
depth = 0
for line in xml.splitlines():
line = line.strip()
if line[:2] == '<?':
continue
value_end = line.rfind('</')
if value_end == -1:
attribute = line[1:-1]
if node is None:
node = self.Node(None, [], attribute, depth)
self.root = node
else:
element = self.Node(node, [], attribute, depth)
node.children.append(element)
node = element
depth += 1
elif value_end == 0:
node = node.parent
depth -= 1
else:
value_start = line.find('>')
attribute = line[1:value_start]
value = line[value_start + 1:value_end]
element = self.Node(node, value, attribute, depth)
node.children.append(element)
def form_json(self, data: Node):
if isinstance(data.children, list):
self.json += '\t' * (data.depth + 1) + f'"{data.attribute}": ' + '{\n'
for node in data.children:
self.form_json(node)
self.json = self.json[:-2] + '\n' + '\t' * (data.depth + 1) + '},\n'
else:
self.json += '\t' * (data.depth + 1) + f'"{data.attribute}": "{data.children}",\n'
def load_json(self):
self.json = ''
self.form_json(self.root)
self.json = '{\n' + self.json[:-2] + '\n}'
def form_csv(self, data: Node):
if isinstance(data.children, list):
for node in data.children:
self.form_csv(node)
else:
self.csv[0] += data.get_full_path('/') + ','
self.csv[1] += data.children + ','
def load_csv(self):
self.csv = ['', '']
self.form_csv(self.root)
self.csv[0] = self.csv[0][:-1]
self.csv[1] = self.csv[1][:-1]
self.csv = '\n'.join(self.csv)
def form_tsv(self, data: Node):
if isinstance(data.children, list):
for node in data.children:
self.form_tsv(node)
else:
self.tsv[0] += data.get_full_path('.') + '\t'
self.tsv[1] += data.children + '\t'
def load_tsv(self):
self.tsv = ['', '']
self.form_tsv(self.root)
self.tsv[0] = self.tsv[0][:-1]
self.tsv[1] = self.tsv[1][:-1]
self.tsv = '\n'.join(self.tsv)
def get_json(self) -> str:
return self.json
def get_xml(self) -> str:
return self.xml
def get_proto3(self) -> str:
return self.proto3
def get_tsv(self) -> str:
return str(self.tsv)
def get_csv(self) -> str:
return str(self.csv)
if __name__ == '__main__':
file = open('schedule.xml', encoding='utf8')
contents = file.read()
xml_parser = XmlParser()
xml_parser.parse_xml(contents)
xml_parser.load_json()
file = open('schedule.json', 'w', encoding='utf8')
file.writelines(xml_parser.get_json())
print(xml_parser.get_json())