-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobjects.py
220 lines (187 loc) · 7.82 KB
/
objects.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python
# coding: utf-8
"""
Copyright (C) 2011 julien CORON - http://julien.coron.free.fr
-- Licence GPL --
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from xml.dom.minidom import *
import os
"""This class is the result of the XML parsing.
"""
class Field:
def __init__(self, dbName= "", obName = "", isKey = False):
self.dbName = dbName
self.obName = obName
self.isKey = isKey
self.nullable = True
self.sqlType = "no_type"
self.description = "";
self.autoincrement = False
self.referencedObject = None
self.display = None
self.access = None
self.node = None
def sysout(self):
if self.isKey :
print (" > %s : %s" % (self.dbName, self.obName))
else :
print (" - %s : %s" % (self.dbName, self.obName))
def getAttribute(this, attrName):
return this.node.getAttribute(attrName)
"""This class is the result of the XML parsing.
"""
class CIObject:
def __init__(self, dbTableName = "", obName = ""):
self.dbTableName = dbTableName
self.obName = obName
self.displayName = ""
self.description = ""
self.isCrossTable = False
self.fields = []
self.keyFields = []
self.nonKeyFields = []
def fromXML(self, aFilename):
try:
doc = parse(aFilename)
except xml.parsers.expat.ExpatError:
print(">>>>> Error in file %s" % aFilename)
raise
objectDef = doc.getElementsByTagName("object")[0]
self.dbTableName = objectDef.getAttribute("shortName")
self.displayName = objectDef.getAttribute("displayName")
self.obName = objectDef.getAttribute("obName")
if objectDef.getAttribute("isCrossTable") :
if objectDef.getAttribute("isCrossTable").upper() == "TRUE" or objectDef.getAttribute("isCrossTable").upper() == "YES":
self.isCrossTable = True
descriptionDef = objectDef.getElementsByTagName("description")[0]
self.description = descriptionDef.firstChild.data
# stockage des objets référencés pour ne pas avoir à les charger en boucle
allSubStructure = {}
allSubStructure[self.displayName] = self
for attributeTag in objectDef.getElementsByTagName("attribute"):
aField = Field()
aField.dbName = attributeTag.getAttribute("id")
aField.obName = attributeTag.getAttribute("name")
if attributeTag.getAttribute("type") != "":
aField.sqlType = attributeTag.getAttribute("type")
aField.sqlType = aField.sqlType.replace("<","<").replace(">",">")
#print("-------%s" % aField.sqlType)
if attributeTag.getAttribute("nullable") != "":
aField.nullable = (attributeTag.getAttribute("nullable") == "YES")
#print("....%s: nullable:%s" % (aField.dbName,aField.nullable) )
if attributeTag.getAttribute("isKey") != "":
aField.isKey = (attributeTag.getAttribute("isKey") == "YES")
if attributeTag.getAttribute("autoincrement") != "":
aField.autoincrement = (attributeTag.getAttribute("autoincrement") == "YES")
if attributeTag.getAttribute("referencedObject") != "":
#print("... Finding %s" % attributeTag.getAttribute("referencedObject"))
#print(" in " , allSubStructure.keys())
if attributeTag.getAttribute("referencedObject") not in allSubStructure:
# print("--->", attributeTag.getAttribute("referencedObject"), " not in ", allSubStructure.keys())
subStructure = CIObject()
filePath = os.path.dirname(aFilename)
subStructure.fromXML(os.path.join(filePath, attributeTag.getAttribute("referencedObject") + ".xml"))
allSubStructure[attributeTag.getAttribute("referencedObject")] = subStructure
aField.referencedObject = allSubStructure[attributeTag.getAttribute("referencedObject")]
#print("...... key: %s" % aField.referencedObject.keyFields[0].dbName)
if attributeTag.getAttribute("access") != "":
aField.access = attributeTag.getAttribute("access")
if attributeTag.getAttribute("display") != "":
aField.display = attributeTag.getAttribute("display")
# ajout de tous les attributs
aField.node = attributeTag
descriptionDef = attributeTag.getElementsByTagName("description")[0]
if descriptionDef.firstChild :
aField.description = descriptionDef.firstChild.data
else:
aField.description = ""
self.addFieldObject(aField)
def addFieldObject(self, aField):
self.fields.append(aField)
if aField.isKey :
self.keyFields.append(aField)
else :
self.nonKeyFields.append(aField)
def addField(self, dbName, obName, isKey = False):
self.addFieldObject(Field(dbName, obName, isKey ))
def sysout(self):
print ("%s (%s):" % (self.obName, self.dbTableName))
for field in self.keyFields:
field.sysout()
for field in self.nonKeyFields:
field.sysout()
""" replace de maniere generique le texte template avec les donnees de la structure d'objet
"""
def dbAndObVariablesList(self, template, dbName, obName, indent=1, includesKey=True):
variables = ""
if includesKey :
for variable in self.keyFields:
variableDeclaration = ""
variableDeclaration = template.replace("(%s)s" % dbName, variable.dbName)
variableDeclaration = variableDeclaration.replace("(%s)s" % obName, variable.obName)
variables += variableDeclaration
prefix = ", "
if indent > 0 :
prefix = "\n" + ("\t"*indent)
for variable in self.nonKeyFields:
variableDeclaration = ""
variableDeclaration = template.replace("(%s)s" % dbName, variable.dbName)
variableDeclaration = variableDeclaration.replace("(%s)s" % obName, variable.obName)
if variables != "" :
variables += prefix
variables += variableDeclaration
return variables
def dbVariablesList(self, template, variableName, typeName="", descrName="", indent=1, includesKey=True, suffix=""):
prefix = ", "
if indent > 0 :
prefix = "\n" + ("\t"*indent)
variables = ""
variableDeclaration = template
if includesKey :
for field in self.keyFields:
if variables != "":
variables += prefix
variableDeclaration = template
variableDeclaration = variableDeclaration.replace("(%s)s" % variableName, field.dbName)
if typeName != "":
variableDeclaration = variableDeclaration.replace("(%s)s" % typeName, field.sqlType)
if descrName != "" :
description = field.description
variableDeclaration = variableDeclaration.replace("(%s)s" % descrName, description)
variables += variableDeclaration + suffix
for variable in self.nonKeyFields:
if variables != "":
variables += prefix
variableDeclaration = template
variableDeclaration = variableDeclaration.replace("(%s)s" % variableName, variable.dbName)
if typeName != "":
variableDeclaration = variableDeclaration.replace("(%s)s" % typeName, variable.sqlType)
if descrName != "" :
description = variable.description
variableDeclaration = variableDeclaration.replace("(%s)s" % descrName, description)
variables += variableDeclaration + suffix
# suppression du dernier suffixe
variables = variables[:len(variables)-len(suffix)]
return variables
def listOfKeys(self, fieldPrefix = "", fieldSuffix = "", withIntConversion = False):
resultString = ""
for field in self.keyFields:
if field.sqlType == "int" and withIntConversion:
resultString += ("(int)"+fieldPrefix + field.dbName + fieldSuffix)
else:
resultString += (fieldPrefix+ field.dbName + fieldSuffix)
if len(fieldSuffix) > 0:
suffixLength = 0 - len(fieldSuffix)
return resultString[:suffixLength]
else:
return resultString