2 def __init__(self, keyword, *args):
10 def write_to_file(self, f, indent=0):
14 s += " {:#.6g}".format(a)
21 s += ' "{}"'.format(a)
22 elif type(a)==Resource:
23 s += ' "{}"'.format(a.name)
28 f.write("{}{}".format(tabs, s))
30 f.write("\n{}{{\n".format(tabs))
32 s.write_to_file(f, indent+1)
33 f.write("{}}};\n".format(tabs))
39 def __init__(self, text):
47 def __init__(self, name, keyword):
49 self.keyword = keyword
53 def create_reference_statement(self, keyword, *args):
55 raise TypeError("create_reference_statement expected at least 2 arguments, got {}".format(1+len(args)))
63 raise TypeError("create_reference_statement expected a Resource argument, found none")
65 self.references += [r for r in resources if r not in self.references]
66 return Statement(keyword, *args)
68 def create_embed_statement(self, keyword, *args):
70 raise TypeError("create_embed_statement expected at least 2 arguments, got {}".format(1+len(args)))
72 if type(res)!=Resource:
73 raise TypeError("create_embed_statement expected a Resource as last argument, got {}".format(type(args[-1]).__name__))
75 self.references += [r for r in res.references if r not in self.references]
76 st = Statement(keyword, *args[:-1])
77 st.sub = res.statements
80 def collect_references(self):
82 for r in self.references:
83 refs += [e for e in r.collect_references() if e not in refs]
84 refs += [e for e in self.references if e not in refs]
87 def write_to_file(self, fn):
88 with open(fn, "w") as out_file:
89 for s in self.statements:
90 s.write_to_file(out_file)
92 def write_collection(self, fn, *, exclude_self=False, filter=None):
93 items = self.collect_references()
96 with open(fn, "w") as out_file:
98 if filter and not filter(i):
100 st = Statement(i.keyword, i.name)
101 st.sub = i.statements
102 st.write_to_file(out_file)