]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/datafile.py
Check the flat qualifier from the correct member
[libs/gl.git] / blender / io_mspgl / datafile.py
1 import struct
2
3 class Statement:
4         def __init__(self, keyword, *args):
5                 self.keyword = keyword
6                 self.args = list(args)
7                 self.sub = []
8
9         def append(self, v):
10                 self.args.append(v)
11
12         def write_to_file(self, f, indent=0):
13                 s = self.keyword
14                 for a in self.args:
15                         if type(a)==float:
16                                 s += " {:#.6g}".format(a)
17                         elif type(a)==bool:
18                                 if a:
19                                         s += " true"
20                                 else:
21                                         s += " false"
22                         elif type(a)==str:
23                                 s += ' "{}"'.format(a)
24                         elif type(a)==Resource or type(a)==RawData:
25                                 s += ' "{}"'.format(a.name)
26                         else:
27                                 s += " {}".format(a)
28
29                 tabs = '\t'*indent
30                 f.write("{}{}".format(tabs, s))
31                 if self.sub:
32                         f.write("\n{}{{\n".format(tabs))
33                         for s in self.sub:
34                                 s.write_to_file(f, indent+1)
35                         f.write("{}}};\n".format(tabs))
36                 else:
37                         f.write(";\n")
38
39
40 class Token:
41         def __init__(self, text):
42                 self.text = text
43
44         def __str__(self):
45                 return self.text
46
47
48 class RawData:
49         def __init__(self, name, data):
50                 self.name = name
51                 self.data = data
52
53         def collect_references(self):
54                 return []
55
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)
61
62
63 class Resource:
64         def __init__(self, name, keyword):
65                 self.name = name
66                 self.keyword = keyword
67                 self.statements = []
68                 self.references = []
69
70         def create_reference_statement(self, keyword, *args):
71                 if len(args)<1:
72                         raise TypeError("create_reference_statement expected at least 2 arguments, got {}".format(1+len(args)))
73
74                 resources = []
75                 for a in args:
76                         if type(a)==Resource or type(a)==RawData:
77                                 resources.append(a)
78
79                 if not resources:
80                         raise TypeError("create_reference_statement expected a Resource or RawData argument, found none")
81
82                 self.references += [r for r in resources if r not in self.references]
83                 return Statement(keyword, *args)
84
85         def create_embed_statement(self, keyword, *args):
86                 if len(args)<1:
87                         raise TypeError("create_embed_statement expected at least 2 arguments, got {}".format(1+len(args)))
88                 res = args[-1]
89                 if type(res)!=Resource:
90                         raise TypeError("create_embed_statement expected a Resource as last argument, got {}".format(type(args[-1]).__name__))
91
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
95                 return st
96
97         def collect_references(self):
98                 refs = []
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]
102                 return refs
103
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)
108
109         def write_collection(self, fn, *, exclude_self=False, filter=None):
110                 items = self.collect_references()
111                 if not exclude_self:
112                         items.append(self)
113                 with open(fn, "w") as out_file:
114                         for i in items:
115                                 if filter and not filter(i):
116                                         continue
117                                 st = Statement(i.keyword, i.name)
118                                 st.sub = i.statements
119                                 st.write_to_file(out_file)