author | lbessard |
Mon, 13 Aug 2007 18:04:19 +0200 | |
changeset 69 | 8fbff50141f8 |
parent 67 | 3a1b0afdaf84 |
child 75 | 82d371634f15 |
permissions | -rw-r--r-- |
2 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
58 | 7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
2 | 8 |
# |
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
58 | 19 |
#General Public License for more details. |
2 | 20 |
# |
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
from xml.dom import minidom |
|
26 |
import sys,re |
|
27 |
from types import * |
|
28 |
from datetime import * |
|
29 |
||
30 |
""" |
|
31 |
Time and date definitions |
|
32 |
""" |
|
33 |
TimeType = time(0,0,0).__class__ |
|
34 |
DateType = date(1,1,1).__class__ |
|
35 |
DateTimeType = datetime(1,1,1,0,0,0).__class__ |
|
36 |
||
37 |
""" |
|
38 |
Regular expression models for extracting dates and times from a string |
|
39 |
""" |
|
24 | 40 |
time_model = re.compile('([0-9]{2}):([0-9]{2}):([0-9]{2}(?:.[0-9]*)?)') |
2 | 41 |
date_model = re.compile('([0-9]{4})-([0-9]{2})-([0-9]{2})') |
24 | 42 |
datetime_model = re.compile('([0-9]{4})-([0-9]{2})-([0-9]{2})[ T]([0-9]{2}):([0-9]{2}):([0-9]{2}(?:.[0-9]*)?)') |
2 | 43 |
|
44 |
""" |
|
45 |
Dictionaries for stocking Classes and Types created from XML |
|
46 |
""" |
|
47 |
XMLClasses = {} |
|
48 |
||
49 |
""" |
|
50 |
This function calculates the number of whitespace for indentation |
|
51 |
""" |
|
52 |
def getIndent(indent, balise): |
|
53 |
first = indent * 2 |
|
54 |
second = first + len(balise) + 1 |
|
55 |
return "\t".expandtabs(first), "\t".expandtabs(second) |
|
56 |
||
57 |
""" |
|
58 |
This function opens the xsd file and generate the classes from the xml tree |
|
59 |
""" |
|
60 |
def GenerateClassesFromXSD(filename): |
|
61 |
xsdfile = open(filename, 'r') |
|
62 |
Generate_xsd_classes(minidom.parse(xsdfile), None) |
|
63 |
xsdfile.close() |
|
64 |
||
65 |
""" |
|
66 |
This function recursively creates a definition of the classes and their attributes |
|
67 |
for plcopen from the xsd file of plcopen opened in a DOM model |
|
68 |
""" |
|
69 |
def Generate_xsd_classes(tree, parent, sequence = False): |
|
70 |
attributes = {} |
|
71 |
inheritance = [] |
|
72 |
if sequence: |
|
73 |
order = [] |
|
74 |
# The lists of attributes and inheritance of the node are generated from the childrens |
|
75 |
for node in tree.childNodes: |
|
76 |
# We make fun of #text elements and all other tags that don't are xsd tags |
|
77 |
if node.nodeName != "#text" and node.nodeName.startswith("xsd:"): |
|
78 |
recursion = False |
|
79 |
name = node.nodeName[4:].encode() |
|
80 |
||
81 |
# This tags defines an attribute of the class |
|
82 |
if name in ["element", "attribute"]: |
|
83 |
nodename = GetAttributeValue(node._attrs["name"]) |
|
84 |
if "type" in node._attrs: |
|
85 |
nodetype = GetAttributeValue(node._attrs["type"]) |
|
86 |
if nodetype.startswith("xsd"): |
|
87 |
nodetype = nodetype.replace("xsd", "bse") |
|
88 |
elif nodetype.startswith("ppx"): |
|
89 |
nodetype = nodetype.replace("ppx", "cls") |
|
90 |
else: |
|
91 |
# The type of attribute is defines in the child tree so we generate a new class |
|
92 |
# No name is defined so we create one from nodename and parent class name |
|
93 |
# (because some different nodes can have the same name) |
|
94 |
if parent: |
|
95 |
classname = "%s_%s"%(parent, nodename) |
|
96 |
else: |
|
97 |
classname = nodename |
|
98 |
Generate_xsd_classes(node, classname) |
|
99 |
nodetype = "cls:%s"%classname |
|
100 |
if name == "attribute": |
|
101 |
if "use" in node._attrs: |
|
102 |
use = GetAttributeValue(node._attrs["use"]) |
|
103 |
else: |
|
104 |
use = "optional" |
|
105 |
if name == "element": |
|
106 |
# If a tag can be written more than one time we define a list attribute |
|
107 |
if "maxOccurs" in node._attrs and GetAttributeValue(node._attrs["maxOccurs"]) == "unbounded": |
|
108 |
nodetype = "%s[]"%nodetype |
|
109 |
if "minOccurs" in node._attrs and GetAttributeValue(node._attrs["minOccurs"]) == "0": |
|
110 |
use = "optional" |
|
111 |
else: |
|
112 |
use = "required" |
|
113 |
attributes[nodename] = (nodetype, name, use) |
|
114 |
if sequence: |
|
115 |
order.append(nodename) |
|
116 |
||
117 |
# This tag defines a new class |
|
118 |
elif name == "complexType" or name == "simpleType": |
|
119 |
if "name" in node._attrs: |
|
120 |
classname = GetAttributeValue(node._attrs["name"]) |
|
121 |
super, attrs = Generate_xsd_classes(node, classname) |
|
122 |
else: |
|
123 |
classname = parent |
|
124 |
super, attrs = Generate_xsd_classes(node, classname.split("_")[-1]) |
|
125 |
# When all attributes and inheritances have been extracted, the |
|
126 |
# values are added in the list of classes to create |
|
127 |
if classname not in XMLClasses: |
|
128 |
XMLClasses[classname] = (super, attrs) |
|
129 |
elif XMLClasses[classname] != (super, attrs): |
|
130 |
print "A different class has already got %s for name"%classname |
|
131 |
||
132 |
# This tag defines an attribute that can have different types |
|
133 |
elif name == "choice": |
|
134 |
super, attrs = Generate_xsd_classes(node, parent) |
|
135 |
if "ref" in attrs.keys(): |
|
136 |
choices = attrs |
|
137 |
else: |
|
138 |
choices = {} |
|
139 |
for attr, (attr_type, xml_type, write_type) in attrs.items(): |
|
140 |
choices[attr] = attr_type |
|
141 |
if "maxOccurs" in node._attrs and GetAttributeValue(node._attrs["maxOccurs"]) == "unbounded": |
|
142 |
attributes["multichoice_content"] = choices |
|
143 |
if sequence: |
|
144 |
order.append("multichoice_content") |
|
145 |
else: |
|
146 |
attributes["choice_content"] = choices |
|
147 |
if sequence: |
|
148 |
order.append("choice_content") |
|
149 |
||
150 |
# This tag defines the order in which class attributes must be written |
|
151 |
# in plcopen xml file. We have to store this order like an attribute |
|
152 |
elif name in "sequence": |
|
153 |
super, attrs, order = Generate_xsd_classes(node, parent, True) |
|
154 |
if "maxOccurs" in node._attrs and GetAttributeValue(node._attrs["maxOccurs"]) == "unbounded": |
|
155 |
for attr, (attr_type, xml_type, write_type) in attrs.items(): |
|
156 |
attrs[attr] = ("%s[]"%attr_type, xml_type, write_type) |
|
157 |
if "minOccurs" in node._attrs and GetAttributeValue(node._attrs["minOccurs"]) == "0": |
|
158 |
for attr, (attr_type, xml_type, write_type) in attrs.items(): |
|
159 |
attrs[attr] = (attr_type, xml_type, "optional") |
|
160 |
inheritance.extend(super) |
|
161 |
attributes.update(attrs) |
|
162 |
attributes["order"] = order |
|
163 |
||
164 |
# This tag defines of types |
|
165 |
elif name == "group": |
|
166 |
if "name" in node._attrs: |
|
167 |
nodename = GetAttributeValue(node._attrs["name"]) |
|
168 |
super, attrs = Generate_xsd_classes(node, None) |
|
169 |
XMLClasses[nodename] = (super, {"group":attrs["choice_content"]}) |
|
170 |
elif "ref" in node._attrs: |
|
171 |
if "ref" not in attributes: |
|
172 |
attributes["ref"] = [GetAttributeValue(node._attrs["ref"])] |
|
173 |
else: |
|
174 |
attributes["ref"].append(GetAttributeValue(node._attrs["ref"])) |
|
175 |
||
176 |
# This tag define a base class for the node |
|
177 |
elif name == "extension": |
|
178 |
super = GetAttributeValue(node._attrs["base"]) |
|
179 |
if super.startswith("xsd"): |
|
180 |
super = super.replace("xsd", "bse") |
|
181 |
elif super.startswith("ppx"): |
|
182 |
super = super.replace("ppx", "cls") |
|
183 |
inheritance.append(super[4:]) |
|
184 |
recursion = True |
|
185 |
||
186 |
# This tag defines a restriction on the type of attribute |
|
187 |
elif name == "restriction": |
|
188 |
basetype = GetAttributeValue(node._attrs["base"]) |
|
189 |
if basetype.startswith("xsd"): |
|
190 |
basetype = basetype.replace("xsd", "bse") |
|
191 |
elif basetype.startswith("ppx"): |
|
192 |
basetype = basetype.replace("ppx", "cls") |
|
193 |
attributes["basetype"] = basetype |
|
194 |
recursion = True |
|
195 |
||
196 |
# This tag defines an enumerated type |
|
197 |
elif name == "enumeration": |
|
198 |
if "enum" not in attributes: |
|
199 |
attributes["enum"] = [GetAttributeValue(node._attrs["value"])] |
|
200 |
else: |
|
201 |
attributes["enum"].append(GetAttributeValue(node._attrs["value"])) |
|
202 |
||
203 |
# This tags defines a restriction on a numerical value |
|
204 |
elif name in ["minInclusive","maxInclusive"]: |
|
205 |
if "limit" not in attributes: |
|
206 |
attributes["limit"] = {} |
|
207 |
if name == "minInclusive": |
|
208 |
attributes["limit"]["min"] = eval(GetAttributeValue(node._attrs["value"])) |
|
209 |
elif name == "maxInclusive": |
|
210 |
attributes["limit"]["max"] = eval(GetAttributeValue(node._attrs["value"])) |
|
211 |
||
212 |
# This tag are not important but their childrens are. The childrens are then parsed. |
|
213 |
elif name in ["complexContent", "schema"]: |
|
214 |
recursion = True |
|
215 |
||
216 |
# We make fun of xsd documentation |
|
217 |
elif name in ["annotation"]: |
|
218 |
pass |
|
219 |
||
220 |
else: |
|
221 |
#print name |
|
222 |
Generate_xsd_classes(node, parent) |
|
223 |
||
224 |
# Parse the childrens of node |
|
225 |
if recursion: |
|
226 |
super, attrs = Generate_xsd_classes(node, parent) |
|
227 |
inheritance.extend(super) |
|
228 |
attributes.update(attrs) |
|
229 |
||
230 |
# if sequence tag have been found, order is returned |
|
231 |
if sequence: |
|
232 |
return inheritance, attributes, order |
|
233 |
else: |
|
234 |
return inheritance, attributes |
|
235 |
""" |
|
236 |
Function that extracts data from a node |
|
237 |
""" |
|
238 |
def GetAttributeValue(attr): |
|
239 |
if len(attr.childNodes) == 1: |
|
240 |
return attr.childNodes[0].data.encode() |
|
241 |
else: |
|
67
3a1b0afdaf84
Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents:
58
diff
changeset
|
242 |
text = "" |
3a1b0afdaf84
Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents:
58
diff
changeset
|
243 |
for node in attr.childNodes: |
3a1b0afdaf84
Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents:
58
diff
changeset
|
244 |
if node.nodeName != "#text": |
3a1b0afdaf84
Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents:
58
diff
changeset
|
245 |
text += node.data.encode() |
3a1b0afdaf84
Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents:
58
diff
changeset
|
246 |
return text |
2 | 247 |
|
248 |
""" |
|
249 |
Funtion that returns the Python type and default value for a given type |
|
250 |
""" |
|
251 |
def GetTypeInitialValue(attr_type): |
|
252 |
type_compute = attr_type[4:].replace("[]", "") |
|
253 |
if attr_type.startswith("bse:"): |
|
254 |
if type_compute == "boolean": |
|
255 |
return BooleanType, "False" |
|
256 |
elif type_compute in ["decimal","unsignedLong","long","integer"]: |
|
257 |
return IntType, "0" |
|
258 |
elif type_compute in ["string","anyURI","NMTOKEN"]: |
|
259 |
return StringType, "\"\"" |
|
260 |
elif type_compute == "time": |
|
24 | 261 |
return TimeType, "time(0,0,0,0)" |
2 | 262 |
elif type_compute == "date": |
263 |
return DateType, "date(1,1,1)" |
|
264 |
elif type_compute == "dateTime": |
|
24 | 265 |
return DateTimeType, "datetime(1,1,1,0,0,0,0)" |
2 | 266 |
elif type_compute == "language": |
267 |
return StringType, "\"en-US\"" |
|
268 |
else: |
|
269 |
print "Can't affect: %s"%type_compute |
|
270 |
elif attr_type.startswith("cls:"): |
|
271 |
if type_compute in XMLClasses: |
|
272 |
return XMLClasses[type_compute],"%s()"%type_compute |
|
273 |
||
274 |
""" |
|
275 |
Function that computes value from a python type (Only Boolean are critical because |
|
276 |
there is no uppercase in plcopen) |
|
277 |
""" |
|
278 |
def ComputeValue(value): |
|
279 |
if type(value) == BooleanType: |
|
280 |
if value: |
|
281 |
return "true" |
|
282 |
else: |
|
283 |
return "false" |
|
284 |
else: |
|
285 |
return str(value) |
|
286 |
||
287 |
""" |
|
288 |
Function that extracts a value from a string following the xsd type given |
|
289 |
""" |
|
290 |
def GetComputedValue(attr_type, value): |
|
291 |
type_compute = attr_type[4:].replace("[]", "") |
|
292 |
if type_compute == "boolean": |
|
293 |
if value == "true": |
|
294 |
return True |
|
295 |
elif value == "false": |
|
296 |
return False |
|
297 |
else: |
|
298 |
raise ValueError, "\"%s\" is not a valid boolean!"%value |
|
299 |
elif type_compute in ["decimal","unsignedLong","long","integer"]: |
|
300 |
return int(value) |
|
301 |
elif type_compute in ["string","anyURI","NMTOKEN","language"]: |
|
302 |
return value |
|
303 |
elif type_compute == "time": |
|
304 |
result = time_model.match(value) |
|
305 |
if result: |
|
24 | 306 |
values = result.groups() |
307 |
time_values = [int(v) for v in values[:2]] |
|
308 |
seconds = float(values[2]) |
|
309 |
time_values.extend([int(seconds), int((seconds % 1) * 1000000)]) |
|
2 | 310 |
return time(*time_values) |
311 |
else: |
|
312 |
raise ValueError, "\"%s\" is not a valid time!"%value |
|
313 |
elif type_compute == "date": |
|
314 |
result = date_model.match(value) |
|
315 |
if result: |
|
316 |
date_values = [int(v) for v in result.groups()] |
|
317 |
return date(*date_values) |
|
318 |
else: |
|
319 |
raise ValueError, "\"%s\" is not a valid date!"%value |
|
320 |
elif type_compute == "dateTime": |
|
321 |
result = datetime_model.match(value) |
|
322 |
if result: |
|
24 | 323 |
values = result.groups() |
324 |
datetime_values = [int(v) for v in values[:5]] |
|
325 |
seconds = float(values[5]) |
|
326 |
datetime_values.extend([int(seconds), int((seconds % 1) * 1000000)]) |
|
2 | 327 |
return datetime(*datetime_values) |
328 |
else: |
|
329 |
raise ValueError, "\"%s\" is not a valid datetime!"%value |
|
330 |
else: |
|
331 |
print "Can't affect: %s"%type_compute |
|
332 |
return None |
|
333 |
||
334 |
""" |
|
335 |
This is the Metaclass for PLCOpen element classes. It generates automatically |
|
336 |
the basic useful methods for manipulate the differents attributes of the classes |
|
337 |
""" |
|
338 |
class MetaClass(type): |
|
339 |
||
340 |
def __init__(cls, name, bases, dict, user_classes): |
|
341 |
super(MetaClass, cls).__init__(name, bases, {}) |
|
342 |
#print name, bases, dict, "\n" |
|
343 |
initialValues = {} |
|
344 |
for attr, values in dict.items(): |
|
345 |
if attr in ["order", "basetype"]: |
|
346 |
pass |
|
347 |
||
348 |
# Class is a enumerated type |
|
349 |
elif attr == "enum": |
|
350 |
value_type, initial = GetTypeInitialValue(dict["basetype"]) |
|
351 |
initialValues["value"] = "\"%s\""%values[0] |
|
352 |
setattr(cls, "value", values[0]) |
|
353 |
setattr(cls, "setValue", MetaClass.generateSetEnumMethod(cls, values, value_type)) |
|
354 |
setattr(cls, "getValue", MetaClass.generateGetMethod(cls, "value")) |
|
355 |
||
356 |
# Class is a limited type |
|
357 |
elif attr == "limit": |
|
358 |
value_type, initial = GetTypeInitialValue(dict["basetype"]) |
|
359 |
initial = 0 |
|
360 |
if "min" in values: |
|
361 |
initial = max(initial, values["min"]) |
|
362 |
if "max" in values: |
|
363 |
initial = min(initial, values["max"]) |
|
364 |
initialValues["value"] = "%d"%initial |
|
365 |
setattr(cls, "value", initial) |
|
366 |
setattr(cls, "setValue", MetaClass.generateSetLimitMethod(cls, values, value_type)) |
|
367 |
setattr(cls, "getValue", MetaClass.generateGetMethod(cls, "value")) |
|
368 |
||
369 |
# Class has an attribute that can have different value types |
|
370 |
elif attr == "choice_content": |
|
371 |
setattr(cls, "content", None) |
|
372 |
initialValues["content"] = "None" |
|
373 |
setattr(cls, "deleteContent", MetaClass.generateDeleteMethod(cls, "content")) |
|
374 |
setattr(cls, "setContent", MetaClass.generateSetChoiceMethod(cls, values)) |
|
375 |
setattr(cls, "getContent", MetaClass.generateGetMethod(cls, "content")) |
|
376 |
elif attr == "multichoice_content": |
|
377 |
setattr(cls, "content", []) |
|
378 |
initialValues["content"] = "[]" |
|
379 |
setattr(cls, "appendContent", MetaClass.generateAppendChoiceMethod(cls, values)) |
|
380 |
setattr(cls, "insertContent", MetaClass.generateInsertChoiceMethod(cls, values)) |
|
381 |
setattr(cls, "removeContent", MetaClass.generateRemoveMethod(cls, "content")) |
|
382 |
setattr(cls, "countContent", MetaClass.generateCountMethod(cls, "content")) |
|
383 |
setattr(cls, "setContent", MetaClass.generateSetMethod(cls, "content", ListType)) |
|
384 |
setattr(cls, "getContent", MetaClass.generateGetMethod(cls, "content")) |
|
385 |
||
386 |
# It's an attribute of the class |
|
387 |
else: |
|
388 |
attrname = attr[0].upper()+attr[1:] |
|
389 |
attr_type, xml_type, write_type = values |
|
390 |
value_type, initial = GetTypeInitialValue(attr_type) |
|
391 |
# Value of the attribute is a list |
|
392 |
if attr_type.endswith("[]"): |
|
393 |
setattr(cls, attr, []) |
|
394 |
initialValues[attr] = "[]" |
|
395 |
setattr(cls, "append"+attrname, MetaClass.generateAppendMethod(cls, attr, value_type)) |
|
396 |
setattr(cls, "insert"+attrname, MetaClass.generateInsertMethod(cls, attr, value_type)) |
|
397 |
setattr(cls, "remove"+attrname, MetaClass.generateRemoveMethod(cls, attr)) |
|
398 |
setattr(cls, "count"+attrname, MetaClass.generateCountMethod(cls, attr)) |
|
399 |
setattr(cls, "set"+attrname, MetaClass.generateSetMethod(cls, attr, ListType)) |
|
400 |
else: |
|
401 |
if write_type == "optional": |
|
402 |
setattr(cls, attr, None) |
|
403 |
initialValues[attr] = "None" |
|
404 |
setattr(cls, "add"+attrname, MetaClass.generateAddMethod(cls, attr, initial, user_classes)) |
|
405 |
setattr(cls, "delete"+attrname, MetaClass.generateDeleteMethod(cls, attr)) |
|
406 |
else: |
|
407 |
setattr(cls, attr, initial) |
|
408 |
initialValues[attr] = initial |
|
409 |
setattr(cls, "set"+attrname, MetaClass.generateSetMethod(cls, attr, value_type)) |
|
410 |
setattr(cls, "get"+attrname, MetaClass.generateGetMethod(cls, attr)) |
|
411 |
setattr(cls, "__init__", MetaClass.generateInitMethod(cls, bases, initialValues, user_classes)) |
|
412 |
setattr(cls, "loadXMLTree", MetaClass.generateLoadXMLTree(cls, bases, dict, user_classes)) |
|
413 |
setattr(cls, "generateXMLText", MetaClass.generateGenerateXMLText(cls, bases, dict)) |
|
414 |
setattr(cls, "singleLineAttributes", True) |
|
415 |
||
416 |
""" |
|
417 |
Method that generate the method for loading an xml tree by following the |
|
418 |
attributes list defined |
|
419 |
""" |
|
420 |
def generateLoadXMLTree(cls, bases, dict, user_classes): |
|
421 |
def loadXMLTreeMethod(self, tree): |
|
422 |
# If class is derived, values of inheritance classes are loaded |
|
423 |
for base in bases: |
|
424 |
base.loadXMLTree(self, tree) |
|
425 |
# Class is a enumerated or limited value |
|
426 |
if "enum" in dict.keys() or "limit" in dict.keys(): |
|
427 |
attr_value = GetAttributeValue(tree) |
|
428 |
attr_type = dict["basetype"] |
|
429 |
val = GetComputedValue(attr_type, attr_value) |
|
430 |
self.setValue(val) |
|
431 |
else: |
|
432 |
||
433 |
# Load the node attributes if they are defined in the list |
|
434 |
for attrname, attr in tree._attrs.items(): |
|
435 |
if attrname in dict.keys(): |
|
436 |
attr_type, xml_type, write_type = dict[attrname] |
|
437 |
attr_value = GetAttributeValue(attr) |
|
438 |
if write_type != "optional" or attr_value != "": |
|
439 |
# Extracts the value |
|
440 |
if attr_type.startswith("bse:"): |
|
441 |
val = GetComputedValue(attr_type, attr_value) |
|
442 |
elif attr_type.startswith("cls:"): |
|
443 |
val = eval("%s()"%attr_type[4:], globals().update(user_classes)) |
|
444 |
val.loadXMLTree(attr) |
|
445 |
setattr(self, attrname, val) |
|
446 |
||
447 |
# Load the node childs if they are defined in the list |
|
448 |
for node in tree.childNodes: |
|
449 |
name = node.nodeName |
|
450 |
# We make fun of #text elements |
|
451 |
if name != "#text": |
|
452 |
||
453 |
# Class has an attribute that can have different value types |
|
454 |
if "choice_content" in dict.keys() and name in dict["choice_content"].keys(): |
|
455 |
attr_type = dict["choice_content"][name] |
|
456 |
# Extracts the value |
|
457 |
if attr_type.startswith("bse:"): |
|
458 |
attr_value = GetAttributeValue(node) |
|
459 |
if write_type != "optional" or attr_value != "": |
|
460 |
val = GetComputedValue(attr_type.replace("[]",""), attr_value) |
|
461 |
else: |
|
462 |
val = None |
|
463 |
elif attr_type.startswith("cls:"): |
|
464 |
val = eval("%s()"%attr_type[4:].replace("[]",""), globals().update(user_classes)) |
|
465 |
val.loadXMLTree(node) |
|
466 |
# Stock value in content attribute |
|
467 |
if val: |
|
468 |
if attr_type.endswith("[]"): |
|
469 |
if self.content: |
|
470 |
self.content["value"].append(val) |
|
471 |
else: |
|
472 |
self.content = {"name":name,"value":[val]} |
|
473 |
else: |
|
474 |
self.content = {"name":name,"value":val} |
|
475 |
||
476 |
# Class has a list of attributes that can have different value types |
|
477 |
elif "multichoice_content" in dict.keys() and name in dict["multichoice_content"].keys(): |
|
478 |
attr_type = dict["multichoice_content"][name] |
|
479 |
# Extracts the value |
|
480 |
if attr_type.startswith("bse:"): |
|
481 |
attr_value = GetAttributeValue(node) |
|
482 |
if write_type != "optional" or attr_value != "": |
|
483 |
val = GetComputedValue(attr_type, attr_value) |
|
484 |
else: |
|
485 |
val = None |
|
486 |
elif attr_type.startswith("cls:"): |
|
487 |
val = eval("%s()"%attr_type[4:], globals().update(user_classes)) |
|
488 |
val.loadXMLTree(node) |
|
489 |
# Add to content attribute list |
|
490 |
if val: |
|
491 |
self.content.append({"name":name,"value":val}) |
|
492 |
||
493 |
# The node child is defined in the list |
|
494 |
elif name in dict.keys(): |
|
495 |
attr_type, xml_type, write_type = dict[name] |
|
496 |
# Extracts the value |
|
497 |
if attr_type.startswith("bse:"): |
|
498 |
attr_value = GetAttributeValue(node) |
|
499 |
if write_type != "optional" or attr_value != "": |
|
500 |
val = GetComputedValue(attr_type.replace("[]",""), attr_value) |
|
501 |
else: |
|
502 |
val = None |
|
503 |
elif attr_type.startswith("cls:"): |
|
504 |
val = eval("%s()"%attr_type[4:].replace("[]",""), globals().update(user_classes)) |
|
505 |
val.loadXMLTree(node) |
|
506 |
# Stock value in attribute |
|
507 |
if val: |
|
508 |
if attr_type.endswith("[]"): |
|
509 |
getattr(self, name).append(val) |
|
510 |
else: |
|
511 |
setattr(self, name, val) |
|
512 |
return loadXMLTreeMethod |
|
513 |
||
514 |
""" |
|
515 |
Method that generates the method for generating an xml text by following the |
|
516 |
attributes list defined |
|
517 |
""" |
|
518 |
def generateGenerateXMLText(cls, bases, dict): |
|
519 |
def generateXMLTextMethod(self, name, indent, extras = {}, derived = False): |
|
520 |
ind1, ind2 = getIndent(indent, name) |
|
521 |
if not derived: |
|
522 |
text = ind1 + "<%s"%name |
|
523 |
else: |
|
524 |
text = "" |
|
525 |
if len(bases) > 0: |
|
526 |
base_extras = {} |
|
527 |
if "order" in dict.keys(): |
|
528 |
order = dict["order"] |
|
529 |
else: |
|
530 |
order = [] |
|
531 |
if "choice_content" in dict.keys() and "choice_content" not in order: |
|
532 |
order.append("choice_content") |
|
533 |
if "multichoice_content" in dict.keys() and "multichoice_content" not in order: |
|
534 |
order.append("multichoice_content") |
|
535 |
size = 0 |
|
536 |
first = True |
|
537 |
for attr, value in extras.items(): |
|
538 |
if not first and not self.singleLineAttributes: |
|
539 |
text += "\n%s"%(ind2) |
|
540 |
text += " %s=\"%s\""%(attr, ComputeValue(value)) |
|
541 |
first = False |
|
542 |
for attr, values in dict.items(): |
|
543 |
if attr in ["order","choice_content","multichoice_content"]: |
|
544 |
pass |
|
545 |
elif attr in ["enum","limit"]: |
|
546 |
if not derived: |
|
547 |
text += ">%s</%s>\n"%(ComputeValue(self.value),name) |
|
548 |
else: |
|
549 |
text += ComputeValue(self.value) |
|
550 |
return text |
|
551 |
elif values[1] == "attribute": |
|
552 |
value = getattr(self, attr, None) |
|
553 |
if values[2] != "optional" or value != None: |
|
554 |
if not first and not self.singleLineAttributes: |
|
555 |
text += "\n%s"%(ind2) |
|
556 |
if values[0].startswith("cls"): |
|
557 |
if len(bases) > 0: |
|
558 |
base_extras[attr] = value.getValue() |
|
559 |
else: |
|
560 |
text += " %s=\"%s\""%(attr, ComputeValue(value.getValue())) |
|
561 |
else: |
|
562 |
if len(bases) > 0: |
|
563 |
base_extras[attr] = value |
|
564 |
else: |
|
565 |
text += " %s=\"%s\""%(attr, ComputeValue(value)) |
|
566 |
first = False |
|
567 |
if len(bases) > 0: |
|
568 |
first, new_text = bases[0].generateXMLText(self, name, indent, base_extras, True) |
|
569 |
text += new_text |
|
570 |
else: |
|
571 |
first = True |
|
572 |
ind3, ind4 = getIndent(indent + 1, name) |
|
573 |
for attr in order: |
|
574 |
value = getattr(self, attr, None) |
|
575 |
if attr == "choice_content": |
|
576 |
if self.content: |
|
577 |
if first: |
|
578 |
text += ">\n" |
|
579 |
first = False |
|
580 |
value_type = dict[attr][self.content["name"]] |
|
581 |
if value_type.startswith("bse:"): |
|
582 |
if value_type.endswith("[]"): |
|
583 |
for content in self.content["value"]: |
|
584 |
text += ind1 + "<%s>%s</%s>\n"%(self.content["name"], ComputeValue(content), self.content["name"]) |
|
585 |
else: |
|
586 |
text += ind1 + "<%s>%s</%s>\n"%(self.content["name"], ComputeValue(self.content["value"]), self.content["name"]) |
|
587 |
elif value_type.endswith("[]"): |
|
588 |
for content in self.content["value"]: |
|
589 |
text += content.generateXMLText(self.content["name"], indent + 1) |
|
590 |
else: |
|
591 |
text += self.content["value"].generateXMLText(self.content["name"], indent + 1) |
|
592 |
elif attr == "multichoice_content": |
|
593 |
if len(self.content) > 0: |
|
594 |
for element in self.content: |
|
595 |
if first: |
|
596 |
text += ">\n" |
|
597 |
first = False |
|
598 |
value_type = dict[attr][element["name"]] |
|
599 |
if value_type.startswith("bse:"): |
|
600 |
text += ind1 + "<%s>%s</%s>\n"%(element["name"], ComputeValue(element["value"]), element["name"]) |
|
601 |
else: |
|
602 |
text += element["value"].generateXMLText(element["name"], indent + 1) |
|
603 |
elif dict[attr][2] != "optional" or value != None: |
|
604 |
if dict[attr][0].endswith("[]"): |
|
605 |
if first and len(value) > 0: |
|
606 |
text += ">\n" |
|
607 |
first = False |
|
608 |
for element in value: |
|
609 |
if dict[attr][0].startswith("bse:"): |
|
610 |
text += ind3 + "<%s>%s</%s>\n"%(attr, ComputeValue(element), attr) |
|
611 |
else: |
|
612 |
text += element.generateXMLText(attr, indent + 1) |
|
613 |
else: |
|
614 |
if first: |
|
615 |
text += ">\n" |
|
616 |
first = False |
|
617 |
if dict[attr][0].startswith("bse:"): |
|
618 |
text += ind3 + "<%s>%s</%s>\n"%(attr, ComputeValue(value), attr) |
|
619 |
else: |
|
620 |
text += getattr(self, attr).generateXMLText(attr, indent + 1) |
|
621 |
if not derived: |
|
622 |
if first: |
|
623 |
text += "/>\n" |
|
624 |
else: |
|
625 |
text += ind1 + "</%s>\n"%(name) |
|
626 |
return text |
|
627 |
else: |
|
628 |
return first, text |
|
629 |
return generateXMLTextMethod |
|
630 |
||
631 |
""" |
|
632 |
Methods that generates the different methods for setting and getting the attributes |
|
633 |
""" |
|
634 |
||
635 |
def generateInitMethod(cls, bases, dict, user_classes): |
|
636 |
def initMethod(self): |
|
637 |
for base in bases: |
|
638 |
base.__init__(self) |
|
639 |
for attr, initial in dict.items(): |
|
640 |
setattr(self, attr, eval(initial, globals().update(user_classes))) |
|
641 |
return initMethod |
|
642 |
||
643 |
def generateSetMethod(cls, attr, choice_type): |
|
644 |
def setMethod(self, value): |
|
645 |
setattr(self, attr, value) |
|
646 |
return setMethod |
|
647 |
||
648 |
def generateSetChoiceMethod(cls, attr_type): |
|
649 |
def setChoiceMethod(self, name, value): |
|
650 |
self.content = {"name":name,"value":value} |
|
651 |
return setChoiceMethod |
|
652 |
||
653 |
def generateSetEnumMethod(cls, enum, attr_type): |
|
654 |
def setEnumMethod(self, value): |
|
655 |
if value in enum: |
|
656 |
self.value = value |
|
657 |
else: |
|
658 |
raise ValueError, "%s is not a valid value. Must be in %s"%(value, str(enum)) |
|
659 |
return setEnumMethod |
|
660 |
||
661 |
def generateSetLimitMethod(cls, limit, attr_type): |
|
662 |
def setMethod(self, value): |
|
663 |
if "min" in limit and value < limit["min"]: |
|
664 |
raise ValueError, "%s is not a valid value. Must be greater than %d"%(value, limit["min"]) |
|
665 |
elif "max" in limit and value > limit["max"]: |
|
666 |
raise ValueError, "%s is not a valid value. Must be smaller than %d"%(value, limit["max"]) |
|
667 |
else: |
|
668 |
self.value = value |
|
669 |
return setMethod |
|
670 |
||
671 |
def generateGetMethod(cls, attr): |
|
672 |
def getMethod(self): |
|
673 |
return getattr(self, attr, None) |
|
674 |
return getMethod |
|
675 |
||
676 |
def generateAddMethod(cls, attr, initial, user_classes): |
|
677 |
def addMethod(self): |
|
678 |
setattr(self, attr, eval(initial, globals().update(user_classes))) |
|
679 |
return addMethod |
|
680 |
||
681 |
def generateDeleteMethod(cls, attr): |
|
682 |
def deleteMethod(self): |
|
683 |
setattr(self, attr, None) |
|
684 |
return deleteMethod |
|
685 |
||
686 |
def generateAppendMethod(cls, attr, attr_type): |
|
687 |
def appendMethod(self, value): |
|
688 |
getattr(self, attr).append(value) |
|
689 |
return appendMethod |
|
690 |
||
691 |
def generateInsertMethod(cls, attr, attr_type): |
|
692 |
def insertMethod(self, index, value): |
|
693 |
getattr(self, attr).insert(index, value) |
|
694 |
return insertMethod |
|
695 |
||
696 |
def generateAppendChoiceMethod(cls, choice_types): |
|
697 |
def appendMethod(self, name, value): |
|
698 |
self.content.append({"name":name,"value":value}) |
|
699 |
return appendMethod |
|
700 |
||
701 |
def generateInsertChoiceMethod(cls, choice_types): |
|
702 |
def insertMethod(self, index, name, value): |
|
703 |
self.content.insert(index, {"name":name,"value":value}) |
|
704 |
return insertMethod |
|
705 |
||
706 |
def generateRemoveMethod(cls, attr): |
|
707 |
def removeMethod(self, index): |
|
708 |
getattr(self, attr).pop(index) |
|
709 |
return removeMethod |
|
710 |
||
711 |
def generateCountMethod(cls, attr): |
|
712 |
def countMethod(self): |
|
713 |
return len(getattr(self, attr)) |
|
714 |
return countMethod |
|
715 |
||
716 |
""" |
|
717 |
Methods that generate the classes |
|
718 |
""" |
|
719 |
def CreateClasses(user_classes, user_types): |
|
720 |
for classname in XMLClasses.keys(): |
|
721 |
CreateClass(classname, user_classes, user_types) |
|
722 |
||
723 |
def CreateClass(classname, user_classes, user_types): |
|
724 |
# Checks that classe haven't been generated yet |
|
725 |
if classname not in user_classes and classname not in user_types and classname in XMLClasses: |
|
726 |
inheritance, attributes = XMLClasses[classname] |
|
727 |
#print classe, inheritance, attributes |
|
728 |
dict = {} |
|
729 |
bases = [] |
|
730 |
||
731 |
# If inheritance classes haven't been generated |
|
732 |
for base in inheritance: |
|
733 |
if base not in user_classes: |
|
734 |
CreateClass(base, user_classes, user_types) |
|
735 |
bases.append(user_classes[base]) |
|
736 |
||
737 |
# Checks that all attribute types are available |
|
738 |
for attribute, type_attribute in attributes.items(): |
|
739 |
if attribute == "group": |
|
740 |
user_types[classname] = type_attribute |
|
741 |
elif attribute == "ref": |
|
742 |
user_types[classname] = {} |
|
743 |
for attr in type_attribute: |
|
744 |
if attr[4:] not in user_types: |
|
745 |
CreateClass(attr[4:], user_classes, user_types) |
|
746 |
user_types[classname].update(user_types[attr[4:]]) |
|
747 |
elif attribute in ["choice_content","multichoice_content"]: |
|
748 |
element_types = {} |
|
749 |
for attr, value in type_attribute.items(): |
|
750 |
if attr == "ref": |
|
751 |
for ref in type_attribute["ref"]: |
|
752 |
if ref[4:] not in user_types: |
|
753 |
CreateClass(ref[4:], user_classes, user_types) |
|
754 |
element_types.update(user_types[ref[4:]]) |
|
755 |
else: |
|
756 |
element_types[attr] = value |
|
757 |
dict[attribute] = element_types |
|
758 |
else: |
|
759 |
dict[attribute] = type_attribute |
|
760 |
if attribute == "enum": |
|
761 |
user_types["%s_enum"%classname] = type_attribute |
|
762 |
elif attribute not in ["limit", "order"]: |
|
763 |
if type_attribute[0].startswith("ppx:"): |
|
764 |
type_compute = type_attribute[0][4:].replace("[]","") |
|
765 |
if type_compute not in user_classes: |
|
766 |
CreateClass(type_compute, user_classes, user_types) |
|
767 |
if "group" not in attributes.keys() and "ref" not in attributes.keys(): |
|
768 |
cls = MetaClass.__new__(MetaClass, classname, tuple(bases), dict) |
|
769 |
MetaClass.__init__(cls, classname, tuple(bases), dict, user_classes) |
|
770 |
user_classes[classname] = cls |
|
771 |
||
772 |
""" |
|
773 |
Methods that print the classes generated |
|
774 |
""" |
|
775 |
def PrintClasses(): |
|
776 |
for classname, xmlclass in XMLClasses.items(): |
|
777 |
print "%s : %s\n"%(classname, str(xmlclass)) |
|
778 |
||
779 |
def PrintClassNames(): |
|
780 |
classnames = XMLClasses.keys() |
|
781 |
classnames.sort() |
|
782 |
for classname in classnames: |
|
783 |
print classname |