2021-11-22 18:39:50 -06:00
|
|
|
def filewrapper(file, mode):
|
|
|
|
if type(file) is str:
|
|
|
|
return open(file, mode)
|
|
|
|
elif hasattr(file, "read"):
|
|
|
|
return file
|
|
|
|
else:
|
|
|
|
raise ValueError("need string or file descriptor object")
|
|
|
|
|
2021-11-22 18:16:40 -06:00
|
|
|
class FileParsingError(SyntaxError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Record:
|
|
|
|
def __init__(self):
|
|
|
|
self.name = None
|
|
|
|
self.data = {}
|
|
|
|
|
|
|
|
self.children = {}
|
|
|
|
self.parents = {}
|
|
|
|
|
2021-11-23 11:16:20 -06:00
|
|
|
self.propagated = False
|
|
|
|
|
2021-11-22 18:16:40 -06:00
|
|
|
def __getitem__(self, item):
|
|
|
|
return self.data[item]
|
|
|
|
|
|
|
|
def __setitem__(self, item, value):
|
|
|
|
self.data[item] = value
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f"[{self.data}, {len(self.children)}, {len(self.parents)}]"
|
|
|
|
|
|
|
|
class RecordCollection():
|
|
|
|
def __init__(self, file):
|
2021-11-23 11:20:59 -06:00
|
|
|
self.objects = {}
|
|
|
|
|
2021-11-22 18:43:08 -06:00
|
|
|
with filewrapper(file, "r") as fd:
|
|
|
|
self._fromFile(fd)
|
2021-11-23 11:16:20 -06:00
|
|
|
self._parent()
|
|
|
|
self._propagate()
|
2021-11-22 18:40:27 -06:00
|
|
|
|
2021-11-22 18:16:40 -06:00
|
|
|
def _fromFile(self, fd):
|
|
|
|
lines = [i.rstrip() for i in fd.readlines()]
|
|
|
|
current = Record()
|
2021-11-23 11:32:55 -06:00
|
|
|
cl = 0
|
2021-11-22 18:16:40 -06:00
|
|
|
|
|
|
|
for i in lines:
|
2021-11-23 11:32:55 -06:00
|
|
|
cl += 1
|
2021-11-22 18:16:40 -06:00
|
|
|
ind = i[0:2] == ' '
|
|
|
|
|
|
|
|
if ind:
|
|
|
|
spl = i[2:].split(': ')
|
|
|
|
try:
|
|
|
|
current[spl[0]] = spl[1]
|
|
|
|
except IndexError:
|
2021-11-23 11:32:55 -06:00
|
|
|
raise FileParsingError(f"error parsing '{i}' @ {cl}")
|
2021-11-22 18:16:40 -06:00
|
|
|
|
|
|
|
else:
|
|
|
|
try:
|
2021-11-23 11:29:59 -06:00
|
|
|
if i[-1] != ":":
|
2021-11-23 11:32:55 -06:00
|
|
|
raise FileParsingError(f"colon must be on last character of '{i}' @ {cl}")
|
2021-11-22 18:16:40 -06:00
|
|
|
name = i.split(' ')[1][0:-1]
|
|
|
|
except IndexError:
|
|
|
|
current = Record()
|
|
|
|
continue
|
|
|
|
|
|
|
|
current.name = name
|
|
|
|
self.objects[name] = current
|
|
|
|
|
|
|
|
def _parent(self):
|
|
|
|
for i in self.objects:
|
|
|
|
current = self.objects[i]
|
|
|
|
try:
|
2021-11-22 22:44:36 -06:00
|
|
|
inherit = current['inherit'].split(' ')
|
|
|
|
inherit_order = \
|
2021-11-22 18:16:40 -06:00
|
|
|
[int(i) for i in current['inherit_order'].split(' ')]
|
2021-11-22 22:44:36 -06:00
|
|
|
|
|
|
|
# TODO: add more precise error checking
|
|
|
|
if not len(inherit) == len(inherit_order):
|
2021-11-23 11:32:55 -06:00
|
|
|
raise FileParsingError("len(inherit) != len(inherit_order) @ {cl}")
|
2021-11-22 22:44:36 -06:00
|
|
|
|
|
|
|
for j, k in zip(
|
|
|
|
inherit,
|
|
|
|
inherit_order
|
2021-11-22 18:16:40 -06:00
|
|
|
):
|
|
|
|
|
|
|
|
current.parents[j] = self.objects[j]
|
|
|
|
self.objects[j].children[k] = current
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2021-11-22 18:19:40 -06:00
|
|
|
|
2021-11-23 11:20:19 -06:00
|
|
|
def _resolveNode(self, node):
|
|
|
|
if not node.parents:
|
|
|
|
return
|
|
|
|
elif node.propagated:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
for i in node.parents:
|
|
|
|
if not node.parents[i].propagated:
|
|
|
|
self._resolveNode(node.parents[i])
|
|
|
|
for j in node.parents[i].data:
|
|
|
|
if not j in node.data:
|
|
|
|
node.data[j] = node.parents[i].data[j]
|
|
|
|
node.propagated = True
|
|
|
|
|
|
|
|
def _propagate(self):
|
|
|
|
for i in self.objects:
|
|
|
|
self._resolveNode(self.objects[i])
|
|
|
|
|
2021-11-22 18:19:40 -06:00
|
|
|
def findEntrypoints(self):
|
|
|
|
ret = []
|
|
|
|
|
|
|
|
for i in self.objects:
|
|
|
|
if not self.objects[i].parents:
|
|
|
|
ret.append(self.objects[i])
|
|
|
|
|
|
|
|
return ret
|
2021-11-22 18:39:50 -06:00
|
|
|
|
|
|
|
def write(self, file):
|
2021-11-22 18:43:08 -06:00
|
|
|
with filewrapper(file, "w") as fd:
|
|
|
|
lines = []
|
|
|
|
for i in self.objects:
|
|
|
|
if self.objects[i].children:
|
|
|
|
lines.append(f"cat {self.objects[i].name}:")
|
|
|
|
else:
|
|
|
|
lines.append(f"rec {self.objects[i].name}:")
|
2021-11-22 18:39:50 -06:00
|
|
|
|
2021-11-22 18:43:08 -06:00
|
|
|
for j in self.objects[i].data:
|
|
|
|
lines.append(
|
|
|
|
f" {j}: {self.objects[i].data[j]}"
|
|
|
|
)
|
2021-11-22 18:39:50 -06:00
|
|
|
|
2021-11-22 18:43:08 -06:00
|
|
|
lines.append("")
|
2021-11-22 18:39:50 -06:00
|
|
|
|
2021-11-22 18:43:08 -06:00
|
|
|
lines.pop()
|
2021-11-22 18:39:50 -06:00
|
|
|
|
2021-11-22 18:43:08 -06:00
|
|
|
for i in range(len(lines)):
|
|
|
|
lines[i] += "\n"
|
2021-11-22 18:39:50 -06:00
|
|
|
|
2021-11-22 18:43:08 -06:00
|
|
|
fd.writelines(lines)
|