]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/ide/activegrid/util/objutils.py
More updates to the docview library modules and sample apps from the
[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
18 import xmlmarshaller
19
20 def defaultLoad(fileObject, knownTypes=None):
21 xml = fileObject.read()
22 loadedObject = xmlmarshaller.unmarshal(xml, knownTypes=knownTypes)
23 if hasattr(fileObject, 'name'):
24 loadedObject.fileName = os.path.abspath(fileObject.name)
25 loadedObject.initialize()
26 return loadedObject
27
28 def defaultSave(fileObject, objectToSave, knownTypes=None, withEncoding=1, encoding='utf-8'):
29 xml = xmlmarshaller.marshal(objectToSave, prettyPrint=True, knownTypes=knownTypes, withEncoding=withEncoding, encoding=encoding)
30 fileObject.write(xml)
31 fileObject.flush()
32
33 def clone(objectToClone, knownTypes=None, encoding='utf-8'):
34 xml = xmlmarshaller.marshal(objectToClone, prettyPrint=True, knownTypes=knownTypes, encoding=encoding)
35 clonedObject = xmlmarshaller.unmarshal(xml, knownTypes=knownTypes)
36 if hasattr(objectToClone, 'fileName'):
37 clonedObject.fileName = objectToClone.fileName
38 try:
39 clonedObject.initialize()
40 except AttributeError:
41 pass
42 return clonedObject
43
44 def classForName(className):
45 pathList = className.split('.')
46 moduleName = '.'.join(pathList[:-1])
47 code = __import__(moduleName)
48 for name in pathList[1:]:
49 code = code.__dict__[name]
50 return code
51
52 def hasattrignorecase(object, name):
53 namelow = name.lower()
54 for attr in dir(object):
55 if attr.lower() == namelow:
56 return True
57 for attr in dir(object):
58 if attr.lower() == '_' + namelow:
59 return True
60 return False
61
62 def setattrignorecase(object, name, value):
63 namelow = name.lower()
64 for attr in object.__dict__:
65 if attr.lower() == namelow:
66 object.__dict__[attr] = value
67 return
68 object.__dict__[name] = value
69
70 def getattrignorecase(object, name):
71 namelow = name.lower()
72 for attr in object.__dict__:
73 if attr.lower() == namelow:
74 return object.__dict__[attr]
75 return object.__dict__[name]
76
77 def hasPropertyValue(obj, attr):
78 hasProp = False
79 try:
80 prop = obj.__class__.__dict__[attr]
81 if (isinstance(prop, property)):
82 hasProp = hasattr(obj, attr)
83 if (hasProp):
84 # It's a property and it has a value but sometimes we don't want it.
85 # If there is a _hasattr method execute it and the
86 # result will tell us whether to include this value
87 try:
88 hasProp = obj._hasattr(attr)
89 except:
90 pass
91 except KeyError:
92 pass
93 return hasProp