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:
|
|
242 |
return ""
|
|
243 |
|
|
244 |
"""
|
|
245 |
Funtion that returns the Python type and default value for a given type
|
|
246 |
"""
|
|
247 |
def GetTypeInitialValue(attr_type):
|
|
248 |
type_compute = attr_type[4:].replace("[]", "")
|
|
249 |
if attr_type.startswith("bse:"):
|
|
250 |
if type_compute == "boolean":
|
|
251 |
return BooleanType, "False"
|
|
252 |
elif type_compute in ["decimal","unsignedLong","long","integer"]:
|
|
253 |
return IntType, "0"
|
|
254 |
elif type_compute in ["string","anyURI","NMTOKEN"]:
|
|
255 |
return StringType, "\"\""
|
|
256 |
elif type_compute == "time":
|
24
|
257 |
return TimeType, "time(0,0,0,0)"
|
2
|
258 |
elif type_compute == "date":
|
|
259 |
return DateType, "date(1,1,1)"
|
|
260 |
elif type_compute == "dateTime":
|
24
|
261 |
return DateTimeType, "datetime(1,1,1,0,0,0,0)"
|
2
|
262 |
elif type_compute == "language":
|
|
263 |
return StringType, "\"en-US\""
|
|
264 |
else:
|
|
265 |
print "Can't affect: %s"%type_compute
|
|
266 |
elif attr_type.startswith("cls:"):
|
|
267 |
if type_compute in XMLClasses:
|
|
268 |
return XMLClasses[type_compute],"%s()"%type_compute
|
|
269 |
|
|
270 |
"""
|
|
271 |
Function that computes value from a python type (Only Boolean are critical because
|
|
272 |
there is no uppercase in plcopen)
|
|
273 |
"""
|
|
274 |
def ComputeValue(value):
|
|
275 |
if type(value) == BooleanType:
|
|
276 |
if value:
|
|
277 |
return "true"
|
|
278 |
else:
|
|
279 |
return "false"
|
|
280 |
else:
|
|
281 |
return str(value)
|
|
282 |
|
|
283 |
"""
|
|
284 |
Function that extracts a value from a string following the xsd type given
|
|
285 |
"""
|
|
286 |
def GetComputedValue(attr_type, value):
|
|
287 |
type_compute = attr_type[4:].replace("[]", "")
|
|
288 |
if type_compute == "boolean":
|
|
289 |
if value == "true":
|
|
290 |
return True
|
|
291 |
elif value == "false":
|
|
292 |
return False
|
|
293 |
else:
|
|
294 |
raise ValueError, "\"%s\" is not a valid boolean!"%value
|
|
295 |
elif type_compute in ["decimal","unsignedLong","long","integer"]:
|
|
296 |
return int(value)
|
|
297 |
elif type_compute in ["string","anyURI","NMTOKEN","language"]:
|
|
298 |
return value
|
|
299 |
elif type_compute == "time":
|
|
300 |
result = time_model.match(value)
|
|
301 |
if result:
|
24
|
302 |
values = result.groups()
|
|
303 |
time_values = [int(v) for v in values[:2]]
|
|
304 |
seconds = float(values[2])
|
|
305 |
time_values.extend([int(seconds), int((seconds % 1) * 1000000)])
|
2
|
306 |
return time(*time_values)
|
|
307 |
else:
|
|
308 |
raise ValueError, "\"%s\" is not a valid time!"%value
|
|
309 |
elif type_compute == "date":
|
|
310 |
result = date_model.match(value)
|
|
311 |
if result:
|
|
312 |
date_values = [int(v) for v in result.groups()]
|
|
313 |
return date(*date_values)
|
|
314 |
else:
|
|
315 |
raise ValueError, "\"%s\" is not a valid date!"%value
|
|
316 |
elif type_compute == "dateTime":
|
|
317 |
result = datetime_model.match(value)
|
|
318 |
if result:
|
24
|
319 |
values = result.groups()
|
|
320 |
datetime_values = [int(v) for v in values[:5]]
|
|
321 |
seconds = float(values[5])
|
|
322 |
datetime_values.extend([int(seconds), int((seconds % 1) * 1000000)])
|
2
|
323 |
return datetime(*datetime_values)
|
|
324 |
else:
|
|
325 |
raise ValueError, "\"%s\" is not a valid datetime!"%value
|
|
326 |
else:
|
|
327 |
print "Can't affect: %s"%type_compute
|
|
328 |
return None
|
|
329 |
|
|
330 |
"""
|
|
331 |
This is the Metaclass for PLCOpen element classes. It generates automatically
|
|
332 |
the basic useful methods for manipulate the differents attributes of the classes
|
|
333 |
"""
|
|
334 |
class MetaClass(type):
|
|
335 |
|
|
336 |
def __init__(cls, name, bases, dict, user_classes):
|
|
337 |
super(MetaClass, cls).__init__(name, bases, {})
|
|
338 |
#print name, bases, dict, "\n"
|
|
339 |
initialValues = {}
|
|
340 |
for attr, values in dict.items():
|
|
341 |
if attr in ["order", "basetype"]:
|
|
342 |
pass
|
|
343 |
|
|
344 |
# Class is a enumerated type
|
|
345 |
elif attr == "enum":
|
|
346 |
value_type, initial = GetTypeInitialValue(dict["basetype"])
|
|
347 |
initialValues["value"] = "\"%s\""%values[0]
|
|
348 |
setattr(cls, "value", values[0])
|
|
349 |
setattr(cls, "setValue", MetaClass.generateSetEnumMethod(cls, values, value_type))
|
|
350 |
setattr(cls, "getValue", MetaClass.generateGetMethod(cls, "value"))
|
|
351 |
|
|
352 |
# Class is a limited type
|
|
353 |
elif attr == "limit":
|
|
354 |
value_type, initial = GetTypeInitialValue(dict["basetype"])
|
|
355 |
initial = 0
|
|
356 |
if "min" in values:
|
|
357 |
initial = max(initial, values["min"])
|
|
358 |
if "max" in values:
|
|
359 |
initial = min(initial, values["max"])
|
|
360 |
initialValues["value"] = "%d"%initial
|
|
361 |
setattr(cls, "value", initial)
|
|
362 |
setattr(cls, "setValue", MetaClass.generateSetLimitMethod(cls, values, value_type))
|
|
363 |
setattr(cls, "getValue", MetaClass.generateGetMethod(cls, "value"))
|
|
364 |
|
|
365 |
# Class has an attribute that can have different value types
|
|
366 |
elif attr == "choice_content":
|
|
367 |
setattr(cls, "content", None)
|
|
368 |
initialValues["content"] = "None"
|
|
369 |
setattr(cls, "deleteContent", MetaClass.generateDeleteMethod(cls, "content"))
|
|
370 |
setattr(cls, "setContent", MetaClass.generateSetChoiceMethod(cls, values))
|
|
371 |
setattr(cls, "getContent", MetaClass.generateGetMethod(cls, "content"))
|
|
372 |
elif attr == "multichoice_content":
|
|
373 |
setattr(cls, "content", [])
|
|
374 |
initialValues["content"] = "[]"
|
|
375 |
setattr(cls, "appendContent", MetaClass.generateAppendChoiceMethod(cls, values))
|
|
376 |
setattr(cls, "insertContent", MetaClass.generateInsertChoiceMethod(cls, values))
|
|
377 |
setattr(cls, "removeContent", MetaClass.generateRemoveMethod(cls, "content"))
|
|
378 |
setattr(cls, "countContent", MetaClass.generateCountMethod(cls, "content"))
|
|
379 |
setattr(cls, "setContent", MetaClass.generateSetMethod(cls, "content", ListType))
|
|
380 |
setattr(cls, "getContent", MetaClass.generateGetMethod(cls, "content"))
|
|
381 |
|
|
382 |
# It's an attribute of the class
|
|
383 |
else:
|
|
384 |
attrname = attr[0].upper()+attr[1:]
|
|
385 |
attr_type, xml_type, write_type = values
|
|
386 |
value_type, initial = GetTypeInitialValue(attr_type)
|
|
387 |
# Value of the attribute is a list
|
|
388 |
if attr_type.endswith("[]"):
|
|
389 |
setattr(cls, attr, [])
|
|
390 |
initialValues[attr] = "[]"
|
|
391 |
setattr(cls, "append"+attrname, MetaClass.generateAppendMethod(cls, attr, value_type))
|
|
392 |
setattr(cls, "insert"+attrname, MetaClass.generateInsertMethod(cls, attr, value_type))
|
|
393 |
setattr(cls, "remove"+attrname, MetaClass.generateRemoveMethod(cls, attr))
|
|
394 |
setattr(cls, "count"+attrname, MetaClass.generateCountMethod(cls, attr))
|
|
395 |
setattr(cls, "set"+attrname, MetaClass.generateSetMethod(cls, attr, ListType))
|
|
396 |
else:
|
|
397 |
if write_type == "optional":
|
|
398 |
setattr(cls, attr, None)
|
|
399 |
initialValues[attr] = "None"
|
|
400 |
setattr(cls, "add"+attrname, MetaClass.generateAddMethod(cls, attr, initial, user_classes))
|
|
401 |
setattr(cls, "delete"+attrname, MetaClass.generateDeleteMethod(cls, attr))
|
|
402 |
else:
|
|
403 |
setattr(cls, attr, initial)
|
|
404 |
initialValues[attr] = initial
|
|
405 |
setattr(cls, "set"+attrname, MetaClass.generateSetMethod(cls, attr, value_type))
|
|
406 |
setattr(cls, "get"+attrname, MetaClass.generateGetMethod(cls, attr))
|
|
407 |
setattr(cls, "__init__", MetaClass.generateInitMethod(cls, bases, initialValues, user_classes))
|
|
408 |
setattr(cls, "loadXMLTree", MetaClass.generateLoadXMLTree(cls, bases, dict, user_classes))
|
|
409 |
setattr(cls, "generateXMLText", MetaClass.generateGenerateXMLText(cls, bases, dict))
|
|
410 |
setattr(cls, "singleLineAttributes", True)
|
|
411 |
|
|
412 |
"""
|
|
413 |
Method that generate the method for loading an xml tree by following the
|
|
414 |
attributes list defined
|
|
415 |
"""
|
|
416 |
def generateLoadXMLTree(cls, bases, dict, user_classes):
|
|
417 |
def loadXMLTreeMethod(self, tree):
|
|
418 |
# If class is derived, values of inheritance classes are loaded
|
|
419 |
for base in bases:
|
|
420 |
base.loadXMLTree(self, tree)
|
|
421 |
# Class is a enumerated or limited value
|
|
422 |
if "enum" in dict.keys() or "limit" in dict.keys():
|
|
423 |
attr_value = GetAttributeValue(tree)
|
|
424 |
attr_type = dict["basetype"]
|
|
425 |
val = GetComputedValue(attr_type, attr_value)
|
|
426 |
self.setValue(val)
|
|
427 |
else:
|
|
428 |
|
|
429 |
# Load the node attributes if they are defined in the list
|
|
430 |
for attrname, attr in tree._attrs.items():
|
|
431 |
if attrname in dict.keys():
|
|
432 |
attr_type, xml_type, write_type = dict[attrname]
|
|
433 |
attr_value = GetAttributeValue(attr)
|
|
434 |
if write_type != "optional" or attr_value != "":
|
|
435 |
# Extracts the value
|
|
436 |
if attr_type.startswith("bse:"):
|
|
437 |
val = GetComputedValue(attr_type, attr_value)
|
|
438 |
elif attr_type.startswith("cls:"):
|
|
439 |
val = eval("%s()"%attr_type[4:], globals().update(user_classes))
|
|
440 |
val.loadXMLTree(attr)
|
|
441 |
setattr(self, attrname, val)
|
|
442 |
|
|
443 |
# Load the node childs if they are defined in the list
|
|
444 |
for node in tree.childNodes:
|
|
445 |
name = node.nodeName
|
|
446 |
# We make fun of #text elements
|
|
447 |
if name != "#text":
|
|
448 |
|
|
449 |
# Class has an attribute that can have different value types
|
|
450 |
if "choice_content" in dict.keys() and name in dict["choice_content"].keys():
|
|
451 |
attr_type = dict["choice_content"][name]
|
|
452 |
# Extracts the value
|
|
453 |
if attr_type.startswith("bse:"):
|
|
454 |
attr_value = GetAttributeValue(node)
|
|
455 |
if write_type != "optional" or attr_value != "":
|
|
456 |
val = GetComputedValue(attr_type.replace("[]",""), attr_value)
|
|
457 |
else:
|
|
458 |
val = None
|
|
459 |
elif attr_type.startswith("cls:"):
|
|
460 |
val = eval("%s()"%attr_type[4:].replace("[]",""), globals().update(user_classes))
|
|
461 |
val.loadXMLTree(node)
|
|
462 |
# Stock value in content attribute
|
|
463 |
if val:
|
|
464 |
if attr_type.endswith("[]"):
|
|
465 |
if self.content:
|
|
466 |
self.content["value"].append(val)
|
|
467 |
else:
|
|
468 |
self.content = {"name":name,"value":[val]}
|
|
469 |
else:
|
|
470 |
self.content = {"name":name,"value":val}
|
|
471 |
|
|
472 |
# Class has a list of attributes that can have different value types
|
|
473 |
elif "multichoice_content" in dict.keys() and name in dict["multichoice_content"].keys():
|
|
474 |
attr_type = dict["multichoice_content"][name]
|
|
475 |
# Extracts the value
|
|
476 |
if attr_type.startswith("bse:"):
|
|
477 |
attr_value = GetAttributeValue(node)
|
|
478 |
if write_type != "optional" or attr_value != "":
|
|
479 |
val = GetComputedValue(attr_type, attr_value)
|
|
480 |
else:
|
|
481 |
val = None
|
|
482 |
elif attr_type.startswith("cls:"):
|
|
483 |
val = eval("%s()"%attr_type[4:], globals().update(user_classes))
|
|
484 |
val.loadXMLTree(node)
|
|
485 |
# Add to content attribute list
|
|
486 |
if val:
|
|
487 |
self.content.append({"name":name,"value":val})
|
|
488 |
|
|
489 |
# The node child is defined in the list
|
|
490 |
elif name in dict.keys():
|
|
491 |
attr_type, xml_type, write_type = dict[name]
|
|
492 |
# Extracts the value
|
|
493 |
if attr_type.startswith("bse:"):
|
|
494 |
attr_value = GetAttributeValue(node)
|
|
495 |
if write_type != "optional" or attr_value != "":
|
|
496 |
val = GetComputedValue(attr_type.replace("[]",""), attr_value)
|
|
497 |
else:
|
|
498 |
val = None
|
|
499 |
elif attr_type.startswith("cls:"):
|
|
500 |
val = eval("%s()"%attr_type[4:].replace("[]",""), globals().update(user_classes))
|
|
501 |
val.loadXMLTree(node)
|
|
502 |
# Stock value in attribute
|
|
503 |
if val:
|
|
504 |
if attr_type.endswith("[]"):
|
|
505 |
getattr(self, name).append(val)
|
|
506 |
else:
|
|
507 |
setattr(self, name, val)
|
|
508 |
return loadXMLTreeMethod
|
|
509 |
|
|
510 |
"""
|
|
511 |
Method that generates the method for generating an xml text by following the
|
|
512 |
attributes list defined
|
|
513 |
"""
|
|
514 |
def generateGenerateXMLText(cls, bases, dict):
|
|
515 |
def generateXMLTextMethod(self, name, indent, extras = {}, derived = False):
|
|
516 |
ind1, ind2 = getIndent(indent, name)
|
|
517 |
if not derived:
|
|
518 |
text = ind1 + "<%s"%name
|
|
519 |
else:
|
|
520 |
text = ""
|
|
521 |
if len(bases) > 0:
|
|
522 |
base_extras = {}
|
|
523 |
if "order" in dict.keys():
|
|
524 |
order = dict["order"]
|
|
525 |
else:
|
|
526 |
order = []
|
|
527 |
if "choice_content" in dict.keys() and "choice_content" not in order:
|
|
528 |
order.append("choice_content")
|
|
529 |
if "multichoice_content" in dict.keys() and "multichoice_content" not in order:
|
|
530 |
order.append("multichoice_content")
|
|
531 |
size = 0
|
|
532 |
first = True
|
|
533 |
for attr, value in extras.items():
|
|
534 |
if not first and not self.singleLineAttributes:
|
|
535 |
text += "\n%s"%(ind2)
|
|
536 |
text += " %s=\"%s\""%(attr, ComputeValue(value))
|
|
537 |
first = False
|
|
538 |
for attr, values in dict.items():
|
|
539 |
if attr in ["order","choice_content","multichoice_content"]:
|
|
540 |
pass
|
|
541 |
elif attr in ["enum","limit"]:
|
|
542 |
if not derived:
|
|
543 |
text += ">%s</%s>\n"%(ComputeValue(self.value),name)
|
|
544 |
else:
|
|
545 |
text += ComputeValue(self.value)
|
|
546 |
return text
|
|
547 |
elif values[1] == "attribute":
|
|
548 |
value = getattr(self, attr, None)
|
|
549 |
if values[2] != "optional" or value != None:
|
|
550 |
if not first and not self.singleLineAttributes:
|
|
551 |
text += "\n%s"%(ind2)
|
|
552 |
if values[0].startswith("cls"):
|
|
553 |
if len(bases) > 0:
|
|
554 |
base_extras[attr] = value.getValue()
|
|
555 |
else:
|
|
556 |
text += " %s=\"%s\""%(attr, ComputeValue(value.getValue()))
|
|
557 |
else:
|
|
558 |
if len(bases) > 0:
|
|
559 |
base_extras[attr] = value
|
|
560 |
else:
|
|
561 |
text += " %s=\"%s\""%(attr, ComputeValue(value))
|
|
562 |
first = False
|
|
563 |
if len(bases) > 0:
|
|
564 |
first, new_text = bases[0].generateXMLText(self, name, indent, base_extras, True)
|
|
565 |
text += new_text
|
|
566 |
else:
|
|
567 |
first = True
|
|
568 |
ind3, ind4 = getIndent(indent + 1, name)
|
|
569 |
for attr in order:
|
|
570 |
value = getattr(self, attr, None)
|
|
571 |
if attr == "choice_content":
|
|
572 |
if self.content:
|
|
573 |
if first:
|
|
574 |
text += ">\n"
|
|
575 |
first = False
|
|
576 |
value_type = dict[attr][self.content["name"]]
|
|
577 |
if value_type.startswith("bse:"):
|
|
578 |
if value_type.endswith("[]"):
|
|
579 |
for content in self.content["value"]:
|
|
580 |
text += ind1 + "<%s>%s</%s>\n"%(self.content["name"], ComputeValue(content), self.content["name"])
|
|
581 |
else:
|
|
582 |
text += ind1 + "<%s>%s</%s>\n"%(self.content["name"], ComputeValue(self.content["value"]), self.content["name"])
|
|
583 |
elif value_type.endswith("[]"):
|
|
584 |
for content in self.content["value"]:
|
|
585 |
text += content.generateXMLText(self.content["name"], indent + 1)
|
|
586 |
else:
|
|
587 |
text += self.content["value"].generateXMLText(self.content["name"], indent + 1)
|
|
588 |
elif attr == "multichoice_content":
|
|
589 |
if len(self.content) > 0:
|
|
590 |
for element in self.content:
|
|
591 |
if first:
|
|
592 |
text += ">\n"
|
|
593 |
first = False
|
|
594 |
value_type = dict[attr][element["name"]]
|
|
595 |
if value_type.startswith("bse:"):
|
|
596 |
text += ind1 + "<%s>%s</%s>\n"%(element["name"], ComputeValue(element["value"]), element["name"])
|
|
597 |
else:
|
|
598 |
text += element["value"].generateXMLText(element["name"], indent + 1)
|
|
599 |
elif dict[attr][2] != "optional" or value != None:
|
|
600 |
if dict[attr][0].endswith("[]"):
|
|
601 |
if first and len(value) > 0:
|
|
602 |
text += ">\n"
|
|
603 |
first = False
|
|
604 |
for element in value:
|
|
605 |
if dict[attr][0].startswith("bse:"):
|
|
606 |
text += ind3 + "<%s>%s</%s>\n"%(attr, ComputeValue(element), attr)
|
|
607 |
else:
|
|
608 |
text += element.generateXMLText(attr, indent + 1)
|
|
609 |
else:
|
|
610 |
if first:
|
|
611 |
text += ">\n"
|
|
612 |
first = False
|
|
613 |
if dict[attr][0].startswith("bse:"):
|
|
614 |
text += ind3 + "<%s>%s</%s>\n"%(attr, ComputeValue(value), attr)
|
|
615 |
else:
|
|
616 |
text += getattr(self, attr).generateXMLText(attr, indent + 1)
|
|
617 |
if not derived:
|
|
618 |
if first:
|
|
619 |
text += "/>\n"
|
|
620 |
else:
|
|
621 |
text += ind1 + "</%s>\n"%(name)
|
|
622 |
return text
|
|
623 |
else:
|
|
624 |
return first, text
|
|
625 |
return generateXMLTextMethod
|
|
626 |
|
|
627 |
"""
|
|
628 |
Methods that generates the different methods for setting and getting the attributes
|
|
629 |
"""
|
|
630 |
|
|
631 |
def generateInitMethod(cls, bases, dict, user_classes):
|
|
632 |
def initMethod(self):
|
|
633 |
for base in bases:
|
|
634 |
base.__init__(self)
|
|
635 |
for attr, initial in dict.items():
|
|
636 |
setattr(self, attr, eval(initial, globals().update(user_classes)))
|
|
637 |
return initMethod
|
|
638 |
|
|
639 |
def generateSetMethod(cls, attr, choice_type):
|
|
640 |
def setMethod(self, value):
|
|
641 |
setattr(self, attr, value)
|
|
642 |
return setMethod
|
|
643 |
|
|
644 |
def generateSetChoiceMethod(cls, attr_type):
|
|
645 |
def setChoiceMethod(self, name, value):
|
|
646 |
self.content = {"name":name,"value":value}
|
|
647 |
return setChoiceMethod
|
|
648 |
|
|
649 |
def generateSetEnumMethod(cls, enum, attr_type):
|
|
650 |
def setEnumMethod(self, value):
|
|
651 |
if value in enum:
|
|
652 |
self.value = value
|
|
653 |
else:
|
|
654 |
raise ValueError, "%s is not a valid value. Must be in %s"%(value, str(enum))
|
|
655 |
return setEnumMethod
|
|
656 |
|
|
657 |
def generateSetLimitMethod(cls, limit, attr_type):
|
|
658 |
def setMethod(self, value):
|
|
659 |
if "min" in limit and value < limit["min"]:
|
|
660 |
raise ValueError, "%s is not a valid value. Must be greater than %d"%(value, limit["min"])
|
|
661 |
elif "max" in limit and value > limit["max"]:
|
|
662 |
raise ValueError, "%s is not a valid value. Must be smaller than %d"%(value, limit["max"])
|
|
663 |
else:
|
|
664 |
self.value = value
|
|
665 |
return setMethod
|
|
666 |
|
|
667 |
def generateGetMethod(cls, attr):
|
|
668 |
def getMethod(self):
|
|
669 |
return getattr(self, attr, None)
|
|
670 |
return getMethod
|
|
671 |
|
|
672 |
def generateAddMethod(cls, attr, initial, user_classes):
|
|
673 |
def addMethod(self):
|
|
674 |
setattr(self, attr, eval(initial, globals().update(user_classes)))
|
|
675 |
return addMethod
|
|
676 |
|
|
677 |
def generateDeleteMethod(cls, attr):
|
|
678 |
def deleteMethod(self):
|
|
679 |
setattr(self, attr, None)
|
|
680 |
return deleteMethod
|
|
681 |
|
|
682 |
def generateAppendMethod(cls, attr, attr_type):
|
|
683 |
def appendMethod(self, value):
|
|
684 |
getattr(self, attr).append(value)
|
|
685 |
return appendMethod
|
|
686 |
|
|
687 |
def generateInsertMethod(cls, attr, attr_type):
|
|
688 |
def insertMethod(self, index, value):
|
|
689 |
getattr(self, attr).insert(index, value)
|
|
690 |
return insertMethod
|
|
691 |
|
|
692 |
def generateAppendChoiceMethod(cls, choice_types):
|
|
693 |
def appendMethod(self, name, value):
|
|
694 |
self.content.append({"name":name,"value":value})
|
|
695 |
return appendMethod
|
|
696 |
|
|
697 |
def generateInsertChoiceMethod(cls, choice_types):
|
|
698 |
def insertMethod(self, index, name, value):
|
|
699 |
self.content.insert(index, {"name":name,"value":value})
|
|
700 |
return insertMethod
|
|
701 |
|
|
702 |
def generateRemoveMethod(cls, attr):
|
|
703 |
def removeMethod(self, index):
|
|
704 |
getattr(self, attr).pop(index)
|
|
705 |
return removeMethod
|
|
706 |
|
|
707 |
def generateCountMethod(cls, attr):
|
|
708 |
def countMethod(self):
|
|
709 |
return len(getattr(self, attr))
|
|
710 |
return countMethod
|
|
711 |
|
|
712 |
"""
|
|
713 |
Methods that generate the classes
|
|
714 |
"""
|
|
715 |
def CreateClasses(user_classes, user_types):
|
|
716 |
for classname in XMLClasses.keys():
|
|
717 |
CreateClass(classname, user_classes, user_types)
|
|
718 |
|
|
719 |
def CreateClass(classname, user_classes, user_types):
|
|
720 |
# Checks that classe haven't been generated yet
|
|
721 |
if classname not in user_classes and classname not in user_types and classname in XMLClasses:
|
|
722 |
inheritance, attributes = XMLClasses[classname]
|
|
723 |
#print classe, inheritance, attributes
|
|
724 |
dict = {}
|
|
725 |
bases = []
|
|
726 |
|
|
727 |
# If inheritance classes haven't been generated
|
|
728 |
for base in inheritance:
|
|
729 |
if base not in user_classes:
|
|
730 |
CreateClass(base, user_classes, user_types)
|
|
731 |
bases.append(user_classes[base])
|
|
732 |
|
|
733 |
# Checks that all attribute types are available
|
|
734 |
for attribute, type_attribute in attributes.items():
|
|
735 |
if attribute == "group":
|
|
736 |
user_types[classname] = type_attribute
|
|
737 |
elif attribute == "ref":
|
|
738 |
user_types[classname] = {}
|
|
739 |
for attr in type_attribute:
|
|
740 |
if attr[4:] not in user_types:
|
|
741 |
CreateClass(attr[4:], user_classes, user_types)
|
|
742 |
user_types[classname].update(user_types[attr[4:]])
|
|
743 |
elif attribute in ["choice_content","multichoice_content"]:
|
|
744 |
element_types = {}
|
|
745 |
for attr, value in type_attribute.items():
|
|
746 |
if attr == "ref":
|
|
747 |
for ref in type_attribute["ref"]:
|
|
748 |
if ref[4:] not in user_types:
|
|
749 |
CreateClass(ref[4:], user_classes, user_types)
|
|
750 |
element_types.update(user_types[ref[4:]])
|
|
751 |
else:
|
|
752 |
element_types[attr] = value
|
|
753 |
dict[attribute] = element_types
|
|
754 |
else:
|
|
755 |
dict[attribute] = type_attribute
|
|
756 |
if attribute == "enum":
|
|
757 |
user_types["%s_enum"%classname] = type_attribute
|
|
758 |
elif attribute not in ["limit", "order"]:
|
|
759 |
if type_attribute[0].startswith("ppx:"):
|
|
760 |
type_compute = type_attribute[0][4:].replace("[]","")
|
|
761 |
if type_compute not in user_classes:
|
|
762 |
CreateClass(type_compute, user_classes, user_types)
|
|
763 |
if "group" not in attributes.keys() and "ref" not in attributes.keys():
|
|
764 |
cls = MetaClass.__new__(MetaClass, classname, tuple(bases), dict)
|
|
765 |
MetaClass.__init__(cls, classname, tuple(bases), dict, user_classes)
|
|
766 |
user_classes[classname] = cls
|
|
767 |
|
|
768 |
"""
|
|
769 |
Methods that print the classes generated
|
|
770 |
"""
|
|
771 |
def PrintClasses():
|
|
772 |
for classname, xmlclass in XMLClasses.items():
|
|
773 |
print "%s : %s\n"%(classname, str(xmlclass))
|
|
774 |
|
|
775 |
def PrintClassNames():
|
|
776 |
classnames = XMLClasses.keys()
|
|
777 |
classnames.sort()
|
|
778 |
for classname in classnames:
|
|
779 |
print classname
|