File: objutils.py

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (442 lines) | stat: -rw-r--r-- 16,548 bytes parent folder | download | duplicates (3)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#----------------------------------------------------------------------------
# Name:         objutils.py
# Purpose:      Object Utilities
#
# Author:       Alan Mullendore
#
# Created:      5/10/05
# CVS-ID:       $Id$
# Copyright:    (c) 2004-2005 ActiveGrid, Inc.
# License:      wxWindows License
#----------------------------------------------------------------------------

import logging
import traceback
import sys
import os
import __builtin__
import types
import activegrid.util.utillang as utillang
import activegrid.util.datetimeparser as datetimeparser
from types import *
from activegrid.util.lang import *

FUNCTION_HAS_ATTR = '_hasAttr'
FUNCTION_GET_ATTR = '_getAttr'
FUNCTION_SET_ATTR = '_setAttr'
FUNCTION_DEL_ATTR = '_delAttr'

def hasRawAttr(obj, name):
    if obj == None:
        return False
    if name != FUNCTION_HAS_ATTR and hasattr(obj, FUNCTION_HAS_ATTR):
        return obj._hasAttr(name)
    return obj.__dict__.has_key(name)
    
def getRawAttr(obj, name):
    if name != FUNCTION_GET_ATTR and hasattr(obj, FUNCTION_GET_ATTR):
        return obj._getAttr(name)
    return obj.__dict__.get(name)
    
def setRawAttr(obj, name, value):
    if name != FUNCTION_SET_ATTR and hasattr(obj, FUNCTION_SET_ATTR):
        obj._setAttr(name, value)
    else:
        obj.__dict__[name] = value
    
def delRawAttr(obj, name):
    if name != FUNCTION_DEL_ATTR and hasattr(obj, FUNCTION_DEL_ATTR):
        obj._delAttr(name)
    else:
        del obj.__dict__[name]

def getStaticAttr(obj, attr):
    if (isinstance(obj, types.TypeType)):
        classDesc = obj
    else:
        classDesc = obj.__class__
    if (hasattr(classDesc, attr)):
        return getattr(classDesc, attr)
    return None
    
def setStaticAttr(obj, attr, value):
    if (isinstance(obj, types.TypeType)):
        classDesc = obj
    else:
        classDesc = obj.__class__
    setattr(classDesc, attr, value)

def hasAttrFast(obj, name):
    if hasRawAttr(obj, name):
        return True
    if hasattr(obj, '_complexType'):
        complexType=obj._complexType
        element=complexType.findElement(name)
        if element:
            return True
    if hasattr(obj, name):
        return True
    return False

def moduleForName(moduleName):
    module = None
    pathList = moduleName.split('.')
    if (len(moduleName) > 0):
        module = __import__(moduleName)
        for name in pathList[1:]:
            if (name in module.__dict__):
                module = module.__dict__[name]
            else:
                module = None
                break
    return module
    
def typeForName(typeName):
    i = typeName.rfind('.')
    if (i >= 0):
        module = moduleForName(typeName[:i])
        if (module != None):
            name = typeName[i+1:]
            if (name in module.__dict__):
                return module.__dict__[name]
    elif __builtin__.__dict__.has_key(typeName):
        return __builtin__.__dict__[typeName]
    return None
    
def functionForName(functionName):
    ftype = typeForName(functionName)
    if (isinstance(ftype, (types.FunctionType, types.MethodType, types.BuiltinFunctionType, types.BuiltinMethodType))):
        return ftype
    return None
    
def classForName(className):
    ctype = typeForName(className)
    if (isinstance(ctype, (types.ClassType, types.TypeType))):
        return ctype
    return None

def newInstance(className, objargs=None):
    "dynamically create an object based on the className and return it."

    if not isinstance(objargs, list):
        objargs = [objargs]

    if className == "None":
        return None
    elif className == "bool":
        if ((len(objargs) < 1) or (objargs[0].lower() == "false") or (not objargs[0])):
            return False
        return True
    if className == "str" or className == "unicode": # don't strip: blanks are significant
        if len(objargs) > 0:
            try:
                return utillang.unescape(objargs[0]).encode()
            except:
                return "?"
        else:
            return ""
            
    if className == "date":
        return datetimeparser.parse(objargs[0], asdate=True)
    if className == "datetime":
        return datetimeparser.parse(objargs[0])
    if className == "time":
        return datetimeparser.parse(objargs[0], astime=True)
        
    classtype = classForName(className)
    if (classtype == None):
        raise Exception("Could not find class %s" % className)
        
    if (len(objargs) > 0):
        return classtype(*objargs)
    else:
        return classtype()

def getClassProperty(classType, propertyName):
    return getattr(classType, propertyName)
    
def toDiffableRepr(value, maxLevel=None):
    if (value == None):
        return "None"
    if (maxLevel == None):
        maxLevel = 8
    maxLevel -= 1
    if (maxLevel < 0):
        return typeToString(value, PRINT_OBJ_DIFFABLE)
##    if ((exclude != None) and not isinstance(value, (basestring, int))):
##        for v in exclude:
##            if (v is value):
##                return "<recursive reference>"
##        exclude.append(value)
##    elif (isinstance(value, ObjectType) and hasattr(value, "__dict__")):
##        if (exclude == None):
##            exclude = []
##        s = "%s(%s)" % (type(value), toDiffableString(value.__dict__, exclude))
    if (not isinstance(value, (BooleanType, ClassType, ComplexType, DictType, DictionaryType, 
                               FloatType, IntType, ListType, LongType, StringType, TupleType, 
                               UnicodeType, BufferType, BuiltinFunctionType, BuiltinMethodType,
                               CodeType, FrameType, FunctionType, GeneratorType, InstanceType,
                               LambdaType, MethodType, ModuleType, SliceType, TracebackType,
                               TypeType, XRangeType))):
        if (hasattr(value, "_toDiffableString")):
            s = value._toDiffableString(maxLevel)
        elif (hasattr(value, "__str__")):
            s = str(value)
        elif (hasattr(value, "__dict__")):
            s = "%s(%s)" % (type(value), toDiffableString(value.__dict__, maxLevel))
        else:
            s = str(type(value))
        ix2 = s.find(" object at 0x")
        if (ix2 > 0):
            ix = s.rfind(".")
            if (ix > 0):
                s = "<class %s>" %s[ix+1:ix2]
    elif (isinstance(value, bool)):
        if (value):
            return "True"
        else:
            return "False"
    elif (isinstance(value, (tuple, list))):
        items = []
        for v in value:
            if (isinstance(v, basestring)):
                if (v.find("'") >= 0):
                    items.append('"%s"' % v)
                else:
                    items.append("'%s'" % v)
            else:
                items.append(toDiffableString(v, maxLevel))
        s = "[" + ", ".join(items) + "]"
    elif (isinstance(value, dict)):
        items = []
        for key, val in value.iteritems():
            if (isinstance(val, UnicodeType)):
                items.append("'%s': u'%s'" % (key, toDiffableString(val, maxLevel)))
            elif (isinstance(val, basestring)):
                items.append("'%s': '%s'" % (key, toDiffableString(val, maxLevel)))
            else:
                items.append("'%s': %s" % (key, toDiffableString(val, maxLevel)))
        s = "{" + ", ".join(items) + "}"
    else:
        s = str(value)
    return s
    
def toDiffableString(value, maxLevel=None):
##    if (value == None):
##        return "None"
##    if ((exclude != None) and not isinstance(value, (basestring, int))):
##        for v in exclude:
##            if (v is value):
##                return "<recursive reference>"
##        exclude.append(value)
    s = toDiffableRepr(value, maxLevel)
    ds = ""
    i = s.find(" at 0x") 
    start = 0
    while (i >= 0):
        j = s.find(">", i)
        if (j < i):
            break
        ds += s[start:i]
        start = j
        i = s.find(" at 0x", start) 
    ds = ds + s[start:]
    i = ds.find("\\src\\")
    if (i < 0):
        i = ds.find("/src/")
    else:
        ds = ds.replace("\\", "/")
    if (i > 0):
        i += 4
        if (ds[i:i+5] == "\\php\\"):
            i += 4
        elif (ds[i:i+8] == "\\python\\"):
            i += 7
        ds = "filepath: ..." + ds[i:]
    return ds
        
def toString(value, options=0):
    if ((options & PRINT_OBJ_DIFFABLE) > 0):
        return toDiffableString(value)
    elif (not isinstance(value, basestring)):
        return str(value)
    return value

def typeToString(obj, options=0):
    if (isinstance(obj, BooleanType)):
        return "bool"
    elif (isinstance(obj, UnicodeType)):
        if ((options & PRINT_OBJ_DIFFABLE) > 0):
            return "string"
        return "unicode"
    elif (isinstance(obj, basestring)):
        return "string"
    elif (isinstance(obj, IntType)):
        return "int"
    elif (isinstance(obj, LongType)):
        if ((options & PRINT_OBJ_DIFFABLE) > 0):
            return "int"
        return "long"
    elif (isinstance(obj, FloatType)):
        return "float"
    elif (type(obj) == ListType):
        return "list"
    elif (isinstance(obj, DictType)):
        return "dict"
    elif (isinstance(obj, TupleType)):
        return "tuple"
    elif (isinstance(obj, InstanceType)):
##        ds = str(type(obj))
        ds = "<class %s.%s> " % (obj.__module__, obj.__class__.__name__)
    else:
        ds = str(type(obj))
    if (options == 0):
        import activegrid.util.aglogging
        options = activegrid.util.aglogging.testMode(0, PRINT_OBJ_DIFFABLE)
    if ((options & PRINT_OBJ_DIFFABLE) > 0):
        if (ds.startswith("<class ")):
            ix = ds.rfind(".")
            if (ix < 0):
                ix = 8
            ds = "<class %s>" % ds[ix+1:-2]
    return ds
    
def nameToString(name, options=0):
    if (name.startswith("_v_")):
        return name[3:]
    if ((options & PRINT_OBJ_DIFFABLE) > 0):
        ix = name.find("__")
        if ((ix > 1) and name.startswith("_")):
            name = name[ix:]
        return toDiffableString(name)
    return name
            
PRINT_OBJ_GETATTR = 1
PRINT_OBJ_HIDE_INTERNAL = 2
PRINT_OBJ_COMPACT = 4
PRINT_OBJ_NONONE = 8
PRINT_OBJ_DIFFABLE = 16
PRINT_OBJ_HIDE_EXCLUDED = 32
PRINT_OBJ_INTERNAL = 512

def printObject(out, object, name=None, indent=0, flags=0, exclude=None, remove=None, maxIndent=30):
    if (name == None):
        name = ""
##    elif (name.endswith("_") and not name.endswith("__")):
##        name = name[:-1]
    if ((remove != None) and (name in asDict(remove))):
        return False
    if ((maxIndent != None) and (indent > maxIndent)):
        print >> out, " "*indent, "%s: %s" % (name, toString(str(object), flags)),
        if ((flags & PRINT_OBJ_INTERNAL) == 0):
            print >> out
        return True
    finalNewLine = False
    printed = True
    if ((flags & (PRINT_OBJ_COMPACT | PRINT_OBJ_HIDE_EXCLUDED)) > 0):
        if ((exclude != None) and ((object in exclude) or (name in exclude))):
            return
        if ((flags & PRINT_OBJ_COMPACT) > 0):
            indent = 0
    if ((flags & PRINT_OBJ_INTERNAL) == 0):
        finalNewLine = True
    flags |= PRINT_OBJ_INTERNAL
    if (object is None):
        if (flags & PRINT_OBJ_NONONE) == 0:
            print >> out, " "*indent, name, " = None",
        else:
            finalNewLine = False
            printed = False
    elif (name.startswith("_") and ((flags & PRINT_OBJ_HIDE_INTERNAL) > 0) and not name.startswith("_v_")):
        finalNewLine = False
        printed = False
    elif (isinstance(object, (list, tuple))):
        if ((exclude != None) and object in exclude):
            print >> out, " "*indent, name, " : ", typeToString(object, flags), " of length = ", len(object), " (already printed)",
        elif ((exclude != None) and name in exclude):
            print >> out, " "*indent, name, " : ", typeToString(object, flags), " of length = ", len(object), " (excluded)",
        else:
            if ((exclude != None) and (len(object) > 0)): exclude.append(object)
            print >> out, " "*indent, name, " : ", typeToString(object, flags), " of length = %d" % len(object),
            for i, o in enumerate(object):
                print >> out
                printObject(out, o, name="[%d]" % i, indent=indent+2, flags=flags, exclude=exclude, remove=remove, maxIndent=maxIndent)
    elif (isinstance(object, dict)):
        if ((exclude != None) and object in exclude):
            print >> out, " "*indent, name, " : ", typeToString(object, flags), " (already printed)",
        else:
            if ((exclude != None) and (len(object) > 0)): exclude.append(object)
            if (len(name) > 0):
                print >> out, " "*indent, name,
                if ((flags & PRINT_OBJ_COMPACT) == 0):
                    print >> out
                    indent += 2
            print >> out, " "*indent, "{",
            if ((flags & PRINT_OBJ_COMPACT) == 0):
                print >> out
            keys = object.keys()
            keys.sort()
            for key in keys:
                if (key != None):
                    n = key
                    if (not (isinstance(n, basestring))):
                        n = str(n)
                    else:
                        n = nameToString(n, flags)
                    if ((not n.startswith("_") or ((flags & PRINT_OBJ_HIDE_INTERNAL) == 0))):
                        if printObject(out, object[key], name=n, indent=indent+2, flags=(flags | PRINT_OBJ_INTERNAL), exclude=exclude, remove=remove, maxIndent=maxIndent):
                            if ((flags & PRINT_OBJ_COMPACT) == 0):
                                print >> out
                            else:
                                print >> out, ",",
            print >> out, " "*indent, "}",
    elif (hasattr(object, "__dict__")):
        if (name.startswith("_")): ## and ((flags & PRINT_OBJ_HIDE_INTERNAL) > 0)):
            print >> out, " "*indent, name, " : ", typeToString(object, flags),
        elif ((exclude != None) and ((object in exclude) or (object.__dict__ in exclude))):
            print >> out, " "*indent, name, " : ", typeToString(object, flags), " (already printed)",
        else:
            if (exclude != None): exclude.append(object)
            print >> out, " "*indent, name, " : ", typeToString(object, flags),
            if ((flags & PRINT_OBJ_GETATTR) == 0):
                if ((flags & PRINT_OBJ_COMPACT) == 0):
                    print >> out
                printObject(out, object.__dict__, indent=indent, flags=flags, exclude=exclude, remove=remove, maxIndent=maxIndent)
            else:
                if ((flags & PRINT_OBJ_COMPACT) == 0):
                    print >> out
##                    indent += 2
                print >> out, " "*indent, "{",
                keys = object.__dict__.keys()
                keys.sort()
                printed = True
                for key in keys:
                    if ((exclude != None) and (key in exclude)):
                        continue
                    if (printed and ((flags & PRINT_OBJ_COMPACT) == 0)):
                        print >> out
                    n = nameToString(key, flags)
                    printed = printObject(out, getattr(object, n), name=n, indent=indent+2, flags=flags, exclude=exclude, remove=remove, maxIndent=maxIndent)
                if ((flags & PRINT_OBJ_COMPACT) == 0):
                    print >> out
                print >> out, " "*indent, "}",
    elif (indent < 0):
        print >> out, object,
    elif isinstance(object, basestring):
        if ((exclude != None) and name in exclude):
            print >> out, " "*indent, name, " : ", typeToString(object, flags), " of length = ", len(object), " (excluded)",
        elif (len(object) > 100):
            object = toString(object, flags)
            print >> out, " "*indent, name, ":", typeToString(object, flags), "[%d] = %s...%s" % (len(object), object[:50], object[-50:]),
        else:
            print >> out, " "*indent, name, ":", typeToString(object, flags), "=", toString(object, flags),
##    elif (isinstance(object, float)):
##        val = str(object)
##        if (len(val) > 17):
##            val = val[:17]
##        print >> out, " "*indent, name, ":", type(object), "=", val,
    else:
        print >> out, " "*indent, name, ":", typeToString(object, flags), "=", toString(object, flags),
    if (finalNewLine):
        print >> out
    return printed