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