forked from agibli/sansapp
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathascii.py
230 lines (182 loc) · 6.77 KB
/
ascii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import json
from . import common
class MayaAsciiError(ValueError):
pass
class MayaAsciiParserBase(common.MayaParserBase):
def __init__(self):
self.__command_handlers = {
"requires": self._exec_requires,
"fileInfo": self._exec_file_info,
"file": self._exec_file,
"createNode": self._exec_create_node,
"setAttr": self._exec_set_attr,
}
def on_comment(self, value):
pass
def register_handler(self, command, handler):
self.__command_handlers[command] = handler
def exec_command(self, command, args):
handler = self.__command_handlers.get(command, None)
if handler is not None:
handler(args)
def has_command(self, command):
return command in self.__command_handlers
def _exec_requires(self, args):
if args[0] == "maya":
self.on_requires_maya(args[1])
else:
self.on_requires_plugin(args[0], args[1])
def _exec_file_info(self, args):
self.on_file_info(args[0], args[1])
def _exec_file(self, args):
reference = False
reference_depth_info = None
namespace = None
defer_reference = False
reference_node = None
argptr = 0
while argptr < len(args):
arg = args[argptr]
if arg in ("-r", "--reference"):
reference = True
argptr += 1
elif arg in ("-rdi", "--referenceDepthInfo"):
reference_depth_info = int(args[argptr + 1])
argptr += 2
elif arg in ("-ns", "--namespace"):
namespace = args[argptr + 1]
argptr += 2
elif arg in ("-dr", "--deferReference"):
defer_reference = bool(int(args[argptr + 1]))
argptr += 2
elif arg in ("-rfn", "--referenceNode"):
reference_node = args[argptr + 1]
argptr += 2
elif arg in ('-op'):
argptr += 2
else:
break
if argptr < len(args):
path = args[argptr]
self.on_file_reference(path)
def _exec_create_node(self, args):
nodetype = args[0]
name = None
parent = None
argptr = 1
while argptr < len(args):
arg = args[argptr]
if arg in ("-n", "--name"):
name = args[argptr + 1]
argptr += 2
elif arg in ("-p", "--parent"):
parent = args[argptr + 1]
argptr += 2
elif arg in ("-s", "--shared"):
argptr += 1
else:
raise MayaAsciiError("Unexpected argument: %s" % arg)
self.on_create_node(nodetype, name, parent)
def _exec_set_attr(self, args):
name = args.pop(0)[1:]
attrtype = None
value = None
argptr = 1
while argptr < len(args):
arg = args[argptr]
if arg in ("-type", "--type"):
attrtype = args[argptr + 1]
value = args[argptr + 2:]
argptr += 2
else:
# FIXME this is a catch-all; explicitly support flags
argptr += 1
if not value:
value = args[-1]
if not attrtype:
# Implicitly convert between Python types
# FIXME this isn't particularly safe?
types = {
str: "string",
float: "double",
int: "integer"
}
try:
attrtype = types[type(json.loads(value))]
except KeyError:
attrtype = "string"
except ValueError:
attrtype = types.get(type(value), "string")
self.on_set_attr(name, value, attrtype)
class MayaAsciiParser(MayaAsciiParserBase):
def __init__(self, stream):
super(MayaAsciiParser, self).__init__()
self.__stream = stream
def parse(self):
while self.__parse_next_command():
pass
def __parse_next_command(self):
lines = []
line = self.__stream.readline()
while True:
# Check if we've reached the end of the file
if not line:
break
# Handle comments
elif line.startswith("//"):
self.on_comment(line[2:].strip())
# Handle commands
# A command may span multiple lines
else:
line = line.rstrip("\r\n")
if line and line.endswith(";"):
# Remove trailing semicolon here so the command line
# processor doesn't have to deal with it.
lines.append(line[:-1])
break
elif line:
lines.append(line)
line = self.__stream.readline()
if lines:
self.__parse_command_lines(lines)
return True
return False
def __parse_command_lines(self, lines):
# Pop command name from the first line
command, _, lines[0] = lines[0].partition(" ")
command = command.lstrip()
# Only process arguments if we handle this command
if self.has_command(command):
# Tokenize arguments
args = []
for line in lines:
while True:
line = line.strip()
if not line:
break
# Handle strings
if line[0] in "'\"":
string_delim = line[0]
escaped = False
string_end = len(line)
for i in range(1, len(line)):
# Check for end delimeter
if not escaped and line[i] == string_delim:
string_end = i
break
# Check for start of escape sequence
elif not escaped and line[i] == "\\":
escaped = True
# End escape sequence
else:
escaped = False
# Partition string argument from the remainder
# of the command line.
arg, line = line[1:string_end], line[string_end + 1:]
# Handle other arguments
# These, unlike strings, may be tokenized by whitespace
else:
arg, _, line = line.partition(" ")
args.append(arg)
# Done tokenizing arguments, call command handler
self.exec_command(command, args)