]>
Commit | Line | Data |
---|---|---|
6f1a3f9c RD |
1 | #---------------------------------------------------------------------------- |
2 | # Name: objutils.py | |
3 | # Purpose: Object Utilities | |
4 | # | |
5 | # Author: Alan Mullendore | |
6 | # | |
7 | # Created: 5/10/05 | |
8 | # CVS-ID: $Id$ | |
9 | # Copyright: (c) 2004-2005 ActiveGrid, Inc. | |
10 | # License: wxWindows License | |
11 | #---------------------------------------------------------------------------- | |
12 | ||
13 | import logging | |
14 | import traceback | |
15 | import sys | |
16 | import os | |
6f1a3f9c RD |
17 | import xmlmarshaller |
18 | ||
26ee3a06 RD |
19 | AG_TYPE_MAPPING = { "ag:append" : "activegrid.model.processmodel.AppendOperation", |
20 | "ag:body" : "activegrid.model.processmodel.Body", | |
21 | "ag:copy" : "activegrid.model.processmodel.CopyOperation", | |
22 | "ag:cssRule" : "activegrid.model.processmodel.CssRule", | |
23 | "ag:datasource" : "activegrid.data.dataservice.DataSource", | |
24 | "ag:debug" : "activegrid.model.processmodel.DebugOperation", | |
25 | "ag:deployment" : "activegrid.server.deployment.Deployment", | |
26 | "ag:glue" : "activegrid.model.processmodel.Glue", | |
27 | "ag:hr" : "activegrid.model.processmodel.HorizontalRow", | |
28 | "ag:image" : "activegrid.model.processmodel.Image", | |
29 | "ag:inputs" : "activegrid.model.processmodel.Inputs", | |
30 | "ag:label" : "activegrid.model.processmodel.Label", | |
31 | "ag:processmodel": "activegrid.model.processmodel.ProcessModel", | |
32 | "ag:processmodelref" : "activegrid.server.deployment.ProcessModelRef", | |
33 | "ag:query" : "activegrid.model.processmodel.Query", | |
34 | "ag:schemaOptions" : "activegrid.model.schema.SchemaOptions", | |
35 | "ag:schemaref" : "activegrid.server.deployment.SchemaRef", | |
36 | "ag:set" : "activegrid.model.processmodel.SetOperation", | |
37 | "ag:text" : "activegrid.model.processmodel.Text", | |
38 | "ag:title" : "activegrid.model.processmodel.Title", | |
39 | "ag:view" : "activegrid.model.processmodel.View", | |
40 | "bpws:case" : "activegrid.model.processmodel.BPELCase", | |
41 | "bpws:catch" : "activegrid.model.processmodel.BPELCatch", | |
42 | "bpws:faultHandlers" : "activegrid.model.processmodel.BPELFaultHandlers", | |
43 | "bpws:invoke" : "activegrid.model.processmodel.BPELInvoke", | |
44 | "bpws:onMessage" : "activegrid.model.processmodel.BPELOnMessage", | |
45 | "bpws:otherwise" : "activegrid.model.processmodel.BPELOtherwise", | |
46 | "bpws:pick" : "activegrid.model.processmodel.BPELPick", | |
47 | "bpws:process" : "activegrid.model.processmodel.BPELProcess", | |
48 | "bpws:receive" : "activegrid.model.processmodel.BPELReceive", | |
49 | "bpws:reply" : "activegrid.model.processmodel.BPELReply", | |
50 | "bpws:scope" : "activegrid.model.processmodel.BPELScope", | |
51 | "bpws:sequence" : "activegrid.model.processmodel.BPELSequence", | |
52 | "bpws:switch" : "activegrid.model.processmodel.BPELSwitch", | |
53 | "bpws:terminate" : "activegrid.model.processmodel.BPELTerminate", | |
54 | "bpws:variable" : "activegrid.model.processmodel.BPELVariable", | |
55 | "bpws:variables" : "activegrid.model.processmodel.BPELVariables", | |
56 | "bpws:while" : "activegrid.model.processmodel.BPELWhile", | |
57 | "wsdl:message" : "activegrid.model.processmodel.WSDLMessage", | |
58 | "wsdl:part" : "activegrid.model.processmodel.WSDLPart", | |
59 | "xforms:group" : "activegrid.model.processmodel.XFormsGroup", | |
60 | "xforms:input" : "activegrid.model.processmodel.XFormsInput", | |
61 | "xforms:label" : "activegrid.model.processmodel.XFormsLabel", | |
62 | "xforms:output" : "activegrid.model.processmodel.XFormsOutput", | |
63 | "xforms:secret" : "activegrid.model.processmodel.XFormsSecret", | |
64 | "xforms:submit" : "activegrid.model.processmodel.XFormsSubmit", | |
65 | "xs:all" : "activegrid.model.schema.XsdSequence", | |
66 | "xs:complexType" : "activegrid.model.schema.XsdComplexType", | |
67 | "xs:element" : "activegrid.model.schema.XsdElement", | |
68 | "xs:field" : "activegrid.model.schema.XsdKeyField", | |
69 | "xs:key" : "activegrid.model.schema.XsdKey", | |
70 | "xs:keyref" : "activegrid.model.schema.XsdKeyRef", | |
71 | "xs:schema" : "activegrid.model.schema.Schema", | |
72 | "xs:selector" : "activegrid.model.schema.XsdKeySelector", | |
73 | "xs:sequence" : "activegrid.model.schema.XsdSequence", | |
74 | "projectmodel" : "activegrid.tool.ProjectEditor.ProjectModel", | |
75 | } | |
76 | ||
6f1a3f9c RD |
77 | def defaultLoad(fileObject, knownTypes=None): |
78 | xml = fileObject.read() | |
26ee3a06 | 79 | loadedObject = defaultUnmarshal(xml, knownTypes=knownTypes) |
6f1a3f9c RD |
80 | if hasattr(fileObject, 'name'): |
81 | loadedObject.fileName = os.path.abspath(fileObject.name) | |
82 | loadedObject.initialize() | |
83 | return loadedObject | |
84 | ||
26ee3a06 RD |
85 | def defaultUnmarshal(xml, knownTypes=None): |
86 | if not knownTypes: knownTypes = AG_TYPE_MAPPING | |
87 | return xmlmarshaller.unmarshal(xml, knownTypes=knownTypes) | |
88 | ||
89 | def defaultSave(fileObject, objectToSave, prettyPrint=True, knownTypes=None, withEncoding=1, encoding='utf-8'): | |
90 | xml = defaultMarshal(objectToSave, prettyPrint=prettyPrint, knownTypes=knownTypes, withEncoding=withEncoding, encoding=encoding) | |
6f1a3f9c RD |
91 | fileObject.write(xml) |
92 | fileObject.flush() | |
26ee3a06 RD |
93 | |
94 | def defaultMarshal(objectToSave, prettyPrint=True, knownTypes=None, withEncoding=1, encoding='utf-8'): | |
95 | if not knownTypes: knownTypes = AG_TYPE_MAPPING | |
96 | return xmlmarshaller.marshal(objectToSave, prettyPrint=prettyPrint, knownTypes=knownTypes, withEncoding=withEncoding, encoding=encoding) | |
97 | ||
6f1a3f9c | 98 | def clone(objectToClone, knownTypes=None, encoding='utf-8'): |
26ee3a06 | 99 | if not knownTypes: knownTypes = AG_TYPE_MAPPING |
6f1a3f9c RD |
100 | xml = xmlmarshaller.marshal(objectToClone, prettyPrint=True, knownTypes=knownTypes, encoding=encoding) |
101 | clonedObject = xmlmarshaller.unmarshal(xml, knownTypes=knownTypes) | |
102 | if hasattr(objectToClone, 'fileName'): | |
103 | clonedObject.fileName = objectToClone.fileName | |
104 | try: | |
105 | clonedObject.initialize() | |
106 | except AttributeError: | |
107 | pass | |
108 | return clonedObject | |
109 | ||
110 | def classForName(className): | |
111 | pathList = className.split('.') | |
112 | moduleName = '.'.join(pathList[:-1]) | |
113 | code = __import__(moduleName) | |
114 | for name in pathList[1:]: | |
115 | code = code.__dict__[name] | |
116 | return code | |
117 | ||
118 | def hasattrignorecase(object, name): | |
119 | namelow = name.lower() | |
120 | for attr in dir(object): | |
121 | if attr.lower() == namelow: | |
122 | return True | |
123 | for attr in dir(object): | |
124 | if attr.lower() == '_' + namelow: | |
125 | return True | |
126 | return False | |
127 | ||
128 | def setattrignorecase(object, name, value): | |
129 | namelow = name.lower() | |
130 | for attr in object.__dict__: | |
131 | if attr.lower() == namelow: | |
132 | object.__dict__[attr] = value | |
133 | return | |
134 | object.__dict__[name] = value | |
135 | ||
136 | def getattrignorecase(object, name): | |
137 | namelow = name.lower() | |
138 | for attr in object.__dict__: | |
139 | if attr.lower() == namelow: | |
140 | return object.__dict__[attr] | |
141 | return object.__dict__[name] | |
142 | ||
143 | def hasPropertyValue(obj, attr): | |
144 | hasProp = False | |
145 | try: | |
146 | prop = obj.__class__.__dict__[attr] | |
147 | if (isinstance(prop, property)): | |
148 | hasProp = hasattr(obj, attr) | |
149 | if (hasProp): | |
150 | # It's a property and it has a value but sometimes we don't want it. | |
151 | # If there is a _hasattr method execute it and the | |
152 | # result will tell us whether to include this value | |
153 | try: | |
154 | hasProp = obj._hasattr(attr) | |
155 | except: | |
156 | pass | |
157 | except KeyError: | |
158 | pass | |
159 | return hasProp | |
26ee3a06 RD |
160 | |
161 | def toDiffableString(value): | |
162 | s = repr(value) | |
163 | ds = "" | |
164 | i = s.find(" at 0x") | |
165 | start = 0 | |
166 | while (i >= 0): | |
167 | j = s.find(">", i) | |
168 | if (j < i): | |
169 | break | |
170 | ds += s[start:i] | |
171 | start = j | |
172 | i = s.find(" at 0x", start) | |
173 | return ds + s[start:] | |
174 | ||
175 | PRINT_OBJ_GETATTR = 1 | |
176 | PRINT_OBJ_HIDE_INTERNAL = 2 | |
177 | PRINT_OBJ_COMPACT = 4 | |
178 | PRINT_OBJ_NONONE = 8 | |
179 | PRINT_OBJ_INTERNAL = 512 | |
180 | ||
181 | def printObject(out, object, name="", indent=0, flags=0, exclude=None, maxIndent=30): | |
182 | if ((maxIndent != None) and (indent > maxIndent)): | |
183 | print >> out, " "*indent, name, str(object) | |
184 | return True | |
185 | finalNewLine = False | |
186 | printed = True | |
187 | if ((flags & PRINT_OBJ_COMPACT) > 0): | |
188 | if (exclude and object in exclude): | |
189 | return | |
190 | indent = 0 | |
191 | if ((flags & PRINT_OBJ_INTERNAL) == 0): | |
192 | finalNewLine = True | |
193 | flags |= PRINT_OBJ_INTERNAL | |
194 | if (object == None): | |
195 | if (flags & PRINT_OBJ_NONONE) == 0: | |
196 | print >> out, " "*indent, name, " = None", | |
197 | else: | |
198 | finalNewLine = False | |
199 | printed = False | |
200 | elif (name.startswith("_") and ((flags & PRINT_OBJ_HIDE_INTERNAL) > 0)): | |
201 | finalNewLine = False | |
202 | printed = False | |
203 | elif (isinstance(object, (list, tuple))): | |
204 | if (exclude and object in exclude): | |
205 | print >> out, " "*indent, name, " : ", type(object), " of length = ", len(object), " (already printed)", | |
206 | elif (exclude and name in exclude): | |
207 | print >> out, " "*indent, name, " : ", type(object), " of length = ", len(object), " (excluded)", | |
208 | else: | |
209 | if (exclude != None): exclude.append(object) | |
210 | print >> out, " "*indent, name, " : ", type(object), " of length = %i" % len(object), | |
211 | for i, o in enumerate(object): | |
212 | print >> out | |
213 | printObject(out, o, name="[%i]" % i, indent=indent+2, flags=flags, exclude=exclude, maxIndent=maxIndent) | |
214 | elif (isinstance(object, dict)): | |
215 | if (exclude and object in exclude): | |
216 | print >> out, " "*indent, name, " : ", type(object), " (already printed)", | |
217 | else: | |
218 | if (exclude != None): exclude.append(object) | |
219 | if (len(name) > 0): | |
220 | print >> out, " "*indent, name, | |
221 | if ((flags & PRINT_OBJ_COMPACT) == 0): | |
222 | print >> out | |
223 | indent += 2 | |
224 | print >> out, " "*indent, "{", | |
225 | if ((flags & PRINT_OBJ_COMPACT) == 0): | |
226 | print >> out | |
227 | keys = object.keys() | |
228 | keys.sort() | |
229 | for n in keys: | |
230 | if ((n != None) and (not n.startswith("_") or ((flags & PRINT_OBJ_HIDE_INTERNAL) == 0))): | |
231 | if printObject(out, object[n], name=n, indent=indent+2, flags=flags, exclude=exclude, maxIndent=maxIndent): | |
232 | if ((flags & PRINT_OBJ_COMPACT) == 0): | |
233 | print >> out | |
234 | else: | |
235 | print >> out, ",", | |
236 | print >> out, " "*indent, "}", | |
237 | elif (hasattr(object, "__dict__")): | |
238 | if (exclude and object in exclude): | |
239 | print >> out, " "*indent, name, " : ", type(object), " (already printed) = ", toDiffableString(object), | |
240 | else: | |
241 | if (exclude != None): exclude.append(object) | |
242 | if (name.startswith("_")): | |
243 | print >> out, " "*indent, name, " : ", type(object), | |
244 | elif (exclude and object.__dict__ in exclude): | |
245 | print >> out, " "*indent, name, " : ", type(object), " (already printed)", | |
246 | else: | |
247 | print >> out, " "*indent, name, " : ", type(object), | |
248 | if ((flags & PRINT_OBJ_GETATTR) == 0): | |
249 | if ((flags & PRINT_OBJ_COMPACT) == 0): | |
250 | print >> out | |
251 | printObject(out, object.__dict__, indent=indent, flags=flags, exclude=exclude, maxIndent=maxIndent) | |
252 | else: | |
253 | keys = object.__dict__.keys() | |
254 | keys.sort() | |
255 | for n in keys: | |
256 | if ((flags & PRINT_OBJ_COMPACT) == 0): | |
257 | print >> out | |
258 | printObject(out, getattr(object, n), name=n, indent=indent+2, flags=flags, exclude=exclude, maxIndent=maxIndent) | |
259 | elif (indent < 0): | |
260 | print >> out, object, | |
261 | elif isinstance(object, basestring): | |
262 | if (exclude and name in exclude): | |
263 | print >> out, " "*indent, name, " : ", type(object), " of length = ", len(object), " (excluded)", | |
264 | elif (len(object) > 100): | |
265 | print >> out, " "*indent, name, ":", type(object), "[%i] = %s...%s" % (len(object), object[:50], object[-50:]), | |
266 | else: | |
267 | print >> out, " "*indent, name, ":", type(object), "=", str(object), | |
268 | else: | |
269 | print >> out, " "*indent, name, ":", type(object), "=", str(object), | |
270 | if (finalNewLine): | |
271 | print >> out | |
272 | return printed |