1 #---------------------------------------------------------------------------- 
   3 # Purpose:      Object Utilities 
   5 # Author:       Alan Mullendore 
   9 # Copyright:    (c) 2004-2005 ActiveGrid, Inc. 
  10 # License:      wxWindows License 
  11 #---------------------------------------------------------------------------- 
  19 import xml
.sax
.saxutils 
as saxutils
 
  21 from activegrid
.util
.lang 
import * 
  23 FUNCTION_HAS_ATTR 
= '_hasAttr' 
  24 FUNCTION_GET_ATTR 
= '_getAttr' 
  25 FUNCTION_SET_ATTR 
= '_setAttr' 
  26 FUNCTION_DEL_ATTR 
= '_delAttr' 
  28 def hasRawAttr(obj
, name
): 
  31     if name 
!= FUNCTION_HAS_ATTR 
and hasattr(obj
, FUNCTION_HAS_ATTR
): 
  32         return obj
._hasAttr
(name
) 
  33     return obj
.__dict
__.has_key(name
) 
  35 def getRawAttr(obj
, name
): 
  36     if name 
!= FUNCTION_GET_ATTR 
and hasattr(obj
, FUNCTION_GET_ATTR
): 
  37         return obj
._getAttr
(name
) 
  38     return obj
.__dict
__.get(name
) 
  40 def setRawAttr(obj
, name
, value
): 
  41     if name 
!= FUNCTION_SET_ATTR 
and hasattr(obj
, FUNCTION_SET_ATTR
): 
  42         obj
._setAttr
(name
, value
) 
  44         obj
.__dict
__[name
] = value
 
  46 def delRawAttr(obj
, name
): 
  47     if name 
!= FUNCTION_DEL_ATTR 
and hasattr(obj
, FUNCTION_DEL_ATTR
): 
  50         del obj
.__dict
__[name
] 
  52 def getStaticAttr(obj
, attr
): 
  53     if (isinstance(obj
, types
.TypeType
)): 
  56         classDesc 
= obj
.__class
__ 
  57     if (hasattr(classDesc
, attr
)): 
  58         return getattr(classDesc
, attr
) 
  61 def setStaticAttr(obj
, attr
, value
): 
  62     if (isinstance(obj
, types
.TypeType
)): 
  65         classDesc 
= obj
.__class
__ 
  66     setattr(classDesc
, attr
, value
) 
  68 def moduleForName(moduleName
): 
  70     pathList 
= moduleName
.split('.') 
  71     if (len(moduleName
) > 0): 
  72         module 
= __import__(moduleName
) 
  73         for name 
in pathList
[1:]: 
  74             if (name 
in module
.__dict
__): 
  75                 module 
= module
.__dict
__[name
] 
  81 def typeForName(typeName
): 
  82     i 
= typeName
.rfind('.') 
  84         module 
= moduleForName(typeName
[:i
]) 
  87             if (name 
in module
.__dict
__): 
  88                 return module
.__dict
__[name
] 
  89     elif __builtin__
.__dict
__.has_key(typeName
): 
  90         return __builtin__
.__dict
__[typeName
] 
  93 def functionForName(functionName
): 
  94     ftype 
= typeForName(functionName
) 
  95     if (isinstance(ftype
, (types
.FunctionType
, types
.MethodType
, types
.BuiltinFunctionType
, types
.BuiltinMethodType
))): 
  99 def classForName(className
): 
 100     ctype 
= typeForName(className
) 
 101     if (isinstance(ctype
, (types
.ClassType
, types
.TypeType
))): 
 105 def newInstance(className
, objargs
=None): 
 106     "dynamically create an object based on the className and return it." 
 108     if not isinstance(objargs
, list): 
 111     if className 
== "None": 
 113     elif className 
== "bool": 
 114         if ((len(objargs
) < 1) or (objargs
[0].lower() == "false") or (not objargs
[0])): 
 117     if className 
== "str" or className 
== "unicode": # don"t strip: blanks are significant 
 120                 return saxutils
.unescape(objargs
[0]).encode() 
 126     classtype 
= classForName(className
) 
 127     if (classtype 
== None): 
 128         raise Exception("Could not find class %s" % className
) 
 130     if (len(objargs
) > 0): 
 131         return classtype(*objargs
) 
 135 def getClassProperty(classType
, propertyName
): 
 136     return getattr(classType
, propertyName
) 
 138 def toDiffableRepr(value
, exclude
=None): 
 141 ##    elif (isinstance(value, ObjectType) and hasattr(value, "__dict__")): 
 142 ##        if (exclude == None): 
 144 ##        s = "%s(%s)" % (type(value), toDiffableString(value.__dict__, exclude)) 
 145     elif (not isinstance(value
, (BooleanType
, ClassType
, ComplexType
, DictType
, DictionaryType
,  
 146                                FloatType
, IntType
, ListType
, LongType
, StringType
, TupleType
,  
 147                                UnicodeType
, BufferType
, BuiltinFunctionType
, BuiltinMethodType
, 
 148                                CodeType
, FrameType
, FunctionType
, GeneratorType
, InstanceType
, 
 149                                LambdaType
, MethodType
, ModuleType
, SliceType
, TracebackType
, 
 150                                TypeType
, XRangeType
))): 
 151         if (hasattr(value
, "__str__")): 
 153         elif (hasattr(value
, "__dict__")): 
 154             s 
= "%s(%s)" % (type(value
), toDiffableString(value
.__dict
__, exclude
)) 
 157         ix2 
= s
.find(" object at 0x") 
 161                 s 
= "<class %s>" %s[ix
+1:ix2
] 
 162     elif (isinstance(value
, bool)): 
 167     elif (isinstance(value
, (tuple, list))): 
 170             if (isinstance(v
, basestring
)): 
 171                 if (v
.find("'") >= 0): 
 172                     items
.append('"%s"' % v
) 
 174                     items
.append("'%s'" % v
) 
 176                 items
.append(toDiffableString(v
, exclude
)) 
 177         s 
= "[" + ", ".join(items
) + "]" 
 178     elif (isinstance(value
, dict)): 
 179         if (exclude 
== None): 
 182         for key
, val 
in value
.iteritems(): 
 183             if (isinstance(val
, UnicodeType
)): 
 184                 items
.append("'%s': u'%s'" % (key
, toDiffableString(val
, exclude
))) 
 185             elif (isinstance(val
, basestring
)): 
 186                 items
.append("'%s': '%s'" % (key
, toDiffableString(val
, exclude
))) 
 188                 items
.append("'%s': %s" % (key
, toDiffableString(val
, exclude
))) 
 189         s 
= "{" + ", ".join(items) + "}" 
 194 def toDiffableString(value
, exclude
=None): 
 197     if ((exclude 
!= None) and not isinstance(value
, (basestring
, int))): 
 200                 return "<recursive reference>" 
 201         exclude
.append(value
) 
 202     s 
= toDiffableRepr(value
) 
 212         i 
= s
.find(" at 0x", start
)  
 214     i 
= ds
.find("\\src\\") 
 218         ds 
= ds
.replace("\\", "/") 
 221         if (ds
[i
:i
+5] == "\\php\\"): 
 223         elif (ds
[i
:i
+8] == "\\python\\"): 
 225         ds 
= "filepath: ..." + ds
[i
:] 
 228 def toString(value
, options
=0): 
 229     if ((options 
& PRINT_OBJ_DIFFABLE
) > 0): 
 230         return toDiffableString(value
) 
 231     elif (not isinstance(value
, basestring
)): 
 235 def typeToString(obj
, options
=0): 
 236     if (isinstance(obj
, BooleanType
)): 
 238     elif (isinstance(obj
, UnicodeType
)): 
 239         if ((options 
& PRINT_OBJ_DIFFABLE
) > 0): 
 242     elif (isinstance(obj
, basestring
)): 
 244     elif (isinstance(obj
, IntType
)): 
 246     elif (isinstance(obj
, LongType
)): 
 247         if ((options 
& PRINT_OBJ_DIFFABLE
) > 0): 
 250     elif (isinstance(obj
, FloatType
)): 
 252     elif (type(obj
) == ListType
): 
 254     elif (isinstance(obj
, DictType
)): 
 256     elif (isinstance(obj
, TupleType
)): 
 258     elif (isinstance(obj
, InstanceType
)): 
 259 ##        ds = str(type(obj)) 
 260         ds 
= "<class %s.%s> " % (obj
.__module
__, obj
.__class
__.__name
__) 
 264         import activegrid
.util
.aglogging
 
 265         options 
= activegrid
.util
.aglogging
.testMode(0, PRINT_OBJ_DIFFABLE
) 
 266     if ((options 
& PRINT_OBJ_DIFFABLE
) > 0): 
 267         if (ds
.startswith("<class ")): 
 271             ds 
= "<class %s>" % ds
[ix
+1:-2] 
 274 def nameToString(name
, options
=0): 
 275     if (name
.startswith("_v_")): 
 277     if ((options 
& PRINT_OBJ_DIFFABLE
) > 0): 
 279         if ((ix 
> 1) and name
.startswith("_")): 
 281         return toDiffableString(name
) 
 284 PRINT_OBJ_GETATTR 
= 1 
 285 PRINT_OBJ_HIDE_INTERNAL 
= 2 
 286 PRINT_OBJ_COMPACT 
= 4 
 288 PRINT_OBJ_DIFFABLE 
= 16 
 289 PRINT_OBJ_HIDE_EXCLUDED 
= 32 
 290 PRINT_OBJ_INTERNAL 
= 512 
 292 def printObject(out
, object, name
=None, indent
=0, flags
=0, exclude
=None, remove
=None, maxIndent
=30): 
 295 ##    elif (name.endswith("_") and not name.endswith("__")): 
 297     if ((remove 
!= None) and (name 
in asDict(remove
))): 
 299     if ((maxIndent 
!= None) and (indent 
> maxIndent
)): 
 300         print >> out
, " "*indent
, "%s: %s" % (name
, toString(str(object), flags
)), 
 301         if ((flags 
& PRINT_OBJ_INTERNAL
) == 0): 
 306     if ((flags 
& (PRINT_OBJ_COMPACT | PRINT_OBJ_HIDE_EXCLUDED
)) > 0): 
 307         if ((exclude 
!= None) and ((object in exclude
) or (name 
in exclude
))): 
 309         if ((flags 
& PRINT_OBJ_COMPACT
) > 0): 
 311     if ((flags 
& PRINT_OBJ_INTERNAL
) == 0): 
 313     flags |
= PRINT_OBJ_INTERNAL
 
 315         if (flags 
& PRINT_OBJ_NONONE
) == 0: 
 316             print >> out
, " "*indent
, name
, " = None", 
 320     elif (name
.startswith("_") and ((flags 
& PRINT_OBJ_HIDE_INTERNAL
) > 0) and not name
.startswith("_v_")): 
 323     elif (isinstance(object, (list, tuple))): 
 324         if ((exclude 
!= None) and object in exclude
): 
 325             print >> out
, " "*indent
, name
, " : ", typeToString(object, flags
), " of length = ", len(object), " (already printed)", 
 326         elif ((exclude 
!= None) and name 
in exclude
): 
 327             print >> out
, " "*indent
, name
, " : ", typeToString(object, flags
), " of length = ", len(object), " (excluded)", 
 329             if ((exclude 
!= None) and (len(object) > 0)): exclude
.append(object) 
 330             print >> out
, " "*indent
, name
, " : ", typeToString(object, flags
), " of length = %d" % len(object), 
 331             for i
, o 
in enumerate(object): 
 333                 printObject(out
, o
, name
="[%d]" % i
, indent
=indent
+2, flags
=flags
, exclude
=exclude
, remove
=remove
, maxIndent
=maxIndent
) 
 334     elif (isinstance(object, dict)): 
 335         if ((exclude 
!= None) and object in exclude
): 
 336             print >> out
, " "*indent
, name
, " : ", typeToString(object, flags
), " (already printed)", 
 338             if ((exclude 
!= None) and (len(object) > 0)): exclude
.append(object) 
 340                 print >> out
, " "*indent
, name
, 
 341                 if ((flags 
& PRINT_OBJ_COMPACT
) == 0): 
 344             print >> out
, " "*indent
, "{", 
 345             if ((flags 
& PRINT_OBJ_COMPACT
) == 0): 
 352                     if (not (isinstance(n
, basestring
))): 
 355                         n 
= nameToString(n
, flags
) 
 356                     if ((not n
.startswith("_") or ((flags 
& PRINT_OBJ_HIDE_INTERNAL
) == 0))): 
 357                         if printObject(out
, object[key
], name
=n
, indent
=indent
+2, flags
=(flags | PRINT_OBJ_INTERNAL
), exclude
=exclude
, remove
=remove
, maxIndent
=maxIndent
): 
 358                             if ((flags 
& PRINT_OBJ_COMPACT
) == 0): 
 362             print >> out
, " "*indent
, "}", 
 363     elif (hasattr(object, "__dict__")): 
 364         if (name
.startswith("_")): ## and ((flags & PRINT_OBJ_HIDE_INTERNAL) > 0)): 
 365             print >> out
, " "*indent
, name
, " : ", typeToString(object, flags
), 
 366         elif ((exclude 
!= None) and ((object in exclude
) or (object.__dict
__ in exclude
))): 
 367             print >> out
, " "*indent
, name
, " : ", typeToString(object, flags
), " (already printed)", 
 369             if (exclude 
!= None): exclude
.append(object) 
 370             print >> out
, " "*indent
, name
, " : ", typeToString(object, flags
), 
 371             if ((flags 
& PRINT_OBJ_GETATTR
) == 0): 
 372                 if ((flags 
& PRINT_OBJ_COMPACT
) == 0): 
 374                 printObject(out
, object.__dict
__, indent
=indent
, flags
=flags
, exclude
=exclude
, remove
=remove
, maxIndent
=maxIndent
) 
 376                 if ((flags 
& PRINT_OBJ_COMPACT
) == 0): 
 379                 print >> out
, " "*indent
, "{", 
 380                 keys 
= object.__dict
__.keys() 
 384                     if ((exclude 
!= None) and (key 
in exclude
)): 
 386                     if (printed 
and ((flags 
& PRINT_OBJ_COMPACT
) == 0)): 
 388                     n 
= nameToString(key
, flags
) 
 389                     printed 
= printObject(out
, getattr(object, n
), name
=n
, indent
=indent
+2, flags
=flags
, exclude
=exclude
, remove
=remove
, maxIndent
=maxIndent
) 
 390                 if ((flags 
& PRINT_OBJ_COMPACT
) == 0): 
 392                 print >> out
, " "*indent
, "}", 
 394         print >> out
, object, 
 395     elif isinstance(object, basestring
): 
 396         if ((exclude 
!= None) and name 
in exclude
): 
 397             print >> out
, " "*indent
, name
, " : ", typeToString(object, flags
), " of length = ", len(object), " (excluded)", 
 398         elif (len(object) > 100): 
 399             object = toString(object, flags
) 
 400             print >> out
, " "*indent
, name
, ":", typeToString(object, flags
), "[%d] = %s...%s" % (len(object), object[:50], object[-50:]), 
 402             print >> out
, " "*indent
, name
, ":", typeToString(object, flags
), "=", toString(object, flags
), 
 403 ##    elif (isinstance(object, float)): 
 405 ##        if (len(val) > 17): 
 407 ##        print >> out, " "*indent, name, ":", type(object), "=", val, 
 409         print >> out
, " "*indent
, name
, ":", typeToString(object, flags
), "=", toString(object, flags
),