archive/test.py

75 lines
1.7 KiB
Python
Raw Normal View History

2021-11-22 16:13:24 -06:00
#!/usr/bin/python
import sys
red = '\033[31m'
blue = '\033[34m'
green = '\033[32m'
reset = '\033[0m'
class name:
def __init__(self, name, objtype):
self.name = name
self.objtype = objtype
self.children = {}
self.parents = {}
self.keys = {}
def add(self, key, value):
self.keys[key] = value
def str(self):
return f"{self.objtype} {self.name}: {self.keys}"
def child(self, ind, child):
self.children[ind] = child
def parent(self, ind, parent):
self.parents[ind] = parent
fd = open(sys.argv[1], "r")
lines = [i.rstrip() for i in fd.readlines()]
objects = {}
cobj = None
for i in lines:
if i == '': continue
indented = i[0:2] == ' '
if indented:
splitted = i[2:].split(': ')
cobj.add(splitted[0], splitted[1])
else:
if cobj != None:
objects[cobj.name] = cobj
cobj = name(
i.split(' ')[1][0:-1],
i.split(' ')[0]
)
for i in objects:
try:
groups = objects[i].keys['inherit'].split(' ')
order = objects[i].keys['inherit_order'].split(' ')
except KeyError:
continue
for j, k in zip(groups, order):
objects[j].child(k, objects[i])
objects[i].parent(k, objects[j])
entrypoint = sys.argv[2]
def print_tree(obj, ind):
try: filename = obj.keys['file']
except KeyError: filename = None
try: index = obj.keys['inherit_order']
except KeyError: index = None
print((" " * (ind - 1)) + f"[{red}{index}{reset}] {green}{obj.name}{reset}, {blue}{filename}{reset}")
for i in obj.children:
print_tree(obj.children[i], ind + 1)
print_tree(objects[entrypoint], 1)
fd.close()