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