]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/datafile.py
Adjust and re-export desertpillars data with new features
[libs/gl.git] / blender / io_mspgl / datafile.py
1 class Statement:
2         def __init__(self, keyword, *args):
3                 self.keyword = keyword
4                 self.args = list(args)
5                 self.sub = []
6
7         def append(self, v):
8                 self.args.append(v)
9
10         def write_to_file(self, f, indent=0):
11                 s = self.keyword
12                 for a in self.args:
13                         if type(a)==float:
14                                 s += " {:#.6g}".format(a)
15                         elif type(a)==bool:
16                                 if a:
17                                         s += " true"
18                                 else:
19                                         s += " false"
20                         elif type(a)==str:
21                                 s += ' "{}"'.format(a)
22                         elif type(a)==Resource:
23                                 s += ' "{}"'.format(a.name)
24                         else:
25                                 s += " {}".format(a)
26
27                 tabs = '\t'*indent
28                 f.write("{}{}".format(tabs, s))
29                 if self.sub:
30                         f.write("\n{}{{\n".format(tabs))
31                         for s in self.sub:
32                                 s.write_to_file(f, indent+1)
33                         f.write("{}}};\n".format(tabs))
34                 else:
35                         f.write(";\n")
36
37
38 class Token:
39         def __init__(self, text):
40                 self.text = text
41
42         def __str__(self):
43                 return self.text
44
45
46 class Resource:
47         def __init__(self, name, keyword):
48                 self.name = name
49                 self.keyword = keyword
50                 self.statements = []
51                 self.references = []
52
53         def create_reference_statement(self, keyword, *args):
54                 if len(args)<1:
55                         raise TypeError("create_reference_statement expected at least 2 arguments, got {}".format(1+len(args)))
56
57                 resources = []
58                 for a in args:
59                         if type(a)==Resource:
60                                 resources.append(a)
61
62                 if not resources:
63                         raise TypeError("create_reference_statement expected a Resource argument, found none")
64
65                 self.references += [r for r in resources if r not in self.references]
66                 return Statement(keyword, *args)
67
68         def create_embed_statement(self, keyword, *args):
69                 if len(args)<1:
70                         raise TypeError("create_embed_statement expected at least 2 arguments, got {}".format(1+len(args)))
71                 res = args[-1]
72                 if type(res)!=Resource:
73                         raise TypeError("create_embed_statement expected a Resource as last argument, got {}".format(type(args[-1]).__name__))
74
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
78                 return st
79
80         def collect_references(self):
81                 refs = []
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]
85                 return refs
86
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)
91
92         def write_collection(self, fn, *, exclude_self=False, filter=None):
93                 items = self.collect_references()
94                 if not exclude_self:
95                         items.append(self)
96                 with open(fn, "w") as out_file:
97                         for i in items:
98                                 if filter and not filter(i):
99                                         continue
100                                 st = Statement(i.keyword, i.name)
101                                 st.sub = i.statements
102                                 st.write_to_file(out_file)