4 def __init__(self, keyword, *args):
12 def write_to_file(self, f, indent=0):
16 s += " {:#.6g}".format(a)
23 s += ' "{}"'.format(a)
24 elif type(a)==Resource or type(a)==RawData:
25 s += ' "{}"'.format(a.name)
30 f.write("{}{}".format(tabs, s))
32 f.write("\n{}{{\n".format(tabs))
34 s.write_to_file(f, indent+1)
35 f.write("{}}};\n".format(tabs))
41 def __init__(self, text):
49 def __init__(self, name, data):
53 def collect_references(self):
56 def write_to_file(self, fn):
57 with open(fn, "wb") as out_file:
58 out_file.write(b"MDR\x01")
59 out_file.write(struct.pack(">QH", len(self.data), 0))
60 out_file.write(self.data)
64 def __init__(self, name, keyword):
66 self.keyword = keyword
70 def create_reference_statement(self, keyword, *args):
72 raise TypeError("create_reference_statement expected at least 2 arguments, got {}".format(1+len(args)))
76 if type(a)==Resource or type(a)==RawData:
80 raise TypeError("create_reference_statement expected a Resource or RawData argument, found none")
82 self.references += [r for r in resources if r not in self.references]
83 return Statement(keyword, *args)
85 def create_embed_statement(self, keyword, *args):
87 raise TypeError("create_embed_statement expected at least 2 arguments, got {}".format(1+len(args)))
89 if type(res)!=Resource:
90 raise TypeError("create_embed_statement expected a Resource as last argument, got {}".format(type(args[-1]).__name__))
92 self.references += [r for r in res.references if r not in self.references]
93 st = Statement(keyword, *args[:-1])
94 st.sub = res.statements
97 def collect_references(self):
99 for r in self.references:
100 refs += [e for e in r.collect_references() if e not in refs]
101 refs += [e for e in self.references if e not in refs]
104 def write_to_file(self, fn):
105 with open(fn, "w") as out_file:
106 for s in self.statements:
107 s.write_to_file(out_file)
109 def write_collection(self, fn, *, exclude_self=False, filter=None):
110 items = self.collect_references()
113 with open(fn, "w") as out_file:
115 if filter and not filter(i):
117 st = Statement(i.keyword, i.name)
118 st.sub = i.statements
119 st.write_to_file(out_file)