]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/ide/activegrid/util/objutils.py
Applied patch [ 1284335 ] doc update for wxString::operator[]
[wxWidgets.git] / wxPython / samples / ide / activegrid / util / objutils.py
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
17 from types import *
18
19 def classForName(className):
20 pathList = className.split('.')
21 moduleName = '.'.join(pathList[:-1])
22 code = __import__(moduleName)
23 for name in pathList[1:]:
24 code = code.__dict__[name]
25 return code
26
27 def hasPropertyValue(obj, attr):
28 hasProp = False
29 try:
30 prop = obj.__class__.__dict__[attr]
31 if (isinstance(prop, property)):
32 hasProp = hasattr(obj, attr)
33 if (hasProp):
34 # It's a property and it has a value but sometimes we don't want it.
35 # If there is a _hasattr method execute it and the
36 # result will tell us whether to include this value
37 try:
38 hasProp = obj._hasattr(attr)
39 except:
40 pass
41 except KeyError:
42 pass
43 return hasProp
44
45 def toDiffableString(value):
46 s = str(value)
47 ds = ""
48 i = s.find(" at 0x")
49 start = 0
50 while (i >= 0):
51 j = s.find(">", i)
52 if (j < i):
53 break
54 ds += s[start:i]
55 start = j
56 i = s.find(" at 0x", start)
57 return ds + s[start:]
58
59 def toString(value, options=0):
60 if ((options & PRINT_OBJ_DIFFABLE) > 0):
61 return toDiffableString(value)
62 return value
63
64 def toTypeString(obj):
65 if (isinstance(obj, BooleanType)):
66 return "bool"
67 elif (isinstance(obj, UnicodeType)):
68 return "unicode"
69 elif (isinstance(obj, basestring)):
70 return "string"
71 elif (isinstance(obj, IntType)):
72 return "int"
73 elif (isinstance(obj, FloatType)):
74 return "float"
75 elif (type(obj) == ListType):
76 return "list"
77 elif (isinstance(obj, DictType)):
78 return "dict"
79 elif (isinstance(obj, TupleType)):
80 return "tuple"
81 elif (isinstance(obj, InstanceType)):
82 return type(obj)
83 else:
84 return type(obj)
85
86 PRINT_OBJ_GETATTR = 1
87 PRINT_OBJ_HIDE_INTERNAL = 2
88 PRINT_OBJ_COMPACT = 4
89 PRINT_OBJ_NONONE = 8
90 PRINT_OBJ_DIFFABLE = 16
91 PRINT_OBJ_INTERNAL = 512
92
93 def printObject(out, object, name="", indent=0, flags=0, exclude=None, maxIndent=30):
94 if ((maxIndent != None) and (indent > maxIndent)):
95 print >> out, " "*indent, "%s: %s" % (name, toString(str(object), flags)),
96 if ((flags & PRINT_OBJ_INTERNAL) == 0):
97 print >> out
98 return True
99 finalNewLine = False
100 printed = True
101 ## if (exclude == None):
102 ## exclude = []
103 if ((flags & PRINT_OBJ_COMPACT) > 0):
104 if (exclude and object in exclude):
105 return
106 indent = 0
107 if ((flags & PRINT_OBJ_INTERNAL) == 0):
108 finalNewLine = True
109 flags |= PRINT_OBJ_INTERNAL
110 if (object is None):
111 if (flags & PRINT_OBJ_NONONE) == 0:
112 print >> out, " "*indent, name, " = None",
113 else:
114 finalNewLine = False
115 printed = False
116 elif (name.startswith("_") and ((flags & PRINT_OBJ_HIDE_INTERNAL) > 0)):
117 finalNewLine = False
118 printed = False
119 elif (isinstance(object, (list, tuple))):
120 if ((exclude != None) and object in exclude):
121 print >> out, " "*indent, name, " : ", toTypeString(object), " of length = ", len(object), " (already printed)",
122 elif ((exclude != None) and name in exclude):
123 print >> out, " "*indent, name, " : ", toTypeString(object), " of length = ", len(object), " (excluded)",
124 else:
125 if ((exclude != None) and (len(object) > 0)): exclude.append(object)
126 print >> out, " "*indent, name, " : ", toTypeString(object), " of length = %d" % len(object),
127 for i, o in enumerate(object):
128 print >> out
129 printObject(out, o, name="[%d]" % i, indent=indent+2, flags=flags, exclude=exclude, maxIndent=maxIndent)
130 elif (isinstance(object, dict)):
131 if ((exclude != None) and object in exclude):
132 print >> out, " "*indent, name, " : ", toTypeString(object), " (already printed)",
133 else:
134 if ((exclude != None) and (len(object) > 0)): exclude.append(object)
135 if (len(name) > 0):
136 print >> out, " "*indent, name,
137 if ((flags & PRINT_OBJ_COMPACT) == 0):
138 print >> out
139 indent += 2
140 print >> out, " "*indent, "{",
141 if ((flags & PRINT_OBJ_COMPACT) == 0):
142 print >> out
143 keys = object.keys()
144 keys.sort()
145 for key in keys:
146 if (key != None):
147 n = key
148 if (not (isinstance(n, basestring))):
149 n = str(n)
150 if ((not n.startswith("_") or ((flags & PRINT_OBJ_HIDE_INTERNAL) == 0))):
151 if printObject(out, object[key], name=n, indent=indent+2, flags=(flags | PRINT_OBJ_INTERNAL), exclude=exclude, maxIndent=maxIndent):
152 if ((flags & PRINT_OBJ_COMPACT) == 0):
153 print >> out
154 else:
155 print >> out, ",",
156 print >> out, " "*indent, "}",
157 elif (hasattr(object, "__dict__")):
158 if ((exclude != None) and object in exclude):
159 print >> out, " "*indent, name, " : ", toTypeString(object), " (already printed) = ", toDiffableString(object),
160 else:
161 if (exclude != None): exclude.append(object)
162 if (name.startswith("_")): ## and ((flags & PRINT_OBJ_HIDE_INTERNAL) > 0)):
163 print >> out, " "*indent, name, " : ", toTypeString(object),
164 elif ((exclude != None) and object.__dict__ in exclude):
165 print >> out, " "*indent, name, " : ", toTypeString(object), " (already printed)",
166 else:
167 print >> out, " "*indent, name, " : ", toTypeString(object),
168 if ((flags & PRINT_OBJ_GETATTR) == 0):
169 if ((flags & PRINT_OBJ_COMPACT) == 0):
170 print >> out
171 printObject(out, object.__dict__, indent=indent, flags=flags, exclude=exclude, maxIndent=maxIndent)
172 else:
173 keys = object.__dict__.keys()
174 keys.sort()
175 for n in keys:
176 if ((flags & PRINT_OBJ_COMPACT) == 0):
177 print >> out
178 printObject(out, getattr(object, n), name=n, indent=indent+2, flags=flags, exclude=exclude, maxIndent=maxIndent)
179 elif (indent < 0):
180 print >> out, object,
181 elif isinstance(object, basestring):
182 if ((exclude != None) and name in exclude):
183 print >> out, " "*indent, name, " : ", toTypeString(object), " of length = ", len(object), " (excluded)",
184 elif (len(object) > 100):
185 print >> out, " "*indent, name, ":", toTypeString(object), "[%d] = %s...%s" % (len(object), object[:50], object[-50:]),
186 else:
187 print >> out, " "*indent, name, ":", toTypeString(object), "=", str(object),
188 ## elif (isinstance(object, float)):
189 ## val = str(object)
190 ## if (len(val) > 17):
191 ## val = val[:17]
192 ## print >> out, " "*indent, name, ":", type(object), "=", val,
193 else:
194 print >> out, " "*indent, name, ":", toTypeString(object), "=", str(object),
195 if (finalNewLine):
196 print >> out
197 return printed