]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/ide/activegrid/util/__init__.py
More updates from Morgan Hua
[wxWidgets.git] / wxPython / samples / ide / activegrid / util / __init__.py
1 #----------------------------------------------------------------------------
2 # Name: __init__.py
3 # Purpose: Utilities
4 #
5 # Author: Joel Hare
6 #
7 # Created: 7/28/04
8 # CVS-ID: $Id$
9 # Copyright: (c) 2004-2005 ActiveGrid, Inc.
10 # License: wxWindows License
11 #----------------------------------------------------------------------------
12
13 import logging
14 import cStringIO
15 import traceback
16 import sys
17 import string
18 import os
19
20 def classForName(className):
21 pathList = className.split('.')
22 moduleName = string.join(pathList[:-1], '.')
23 code = __import__(moduleName)
24 for name in pathList[1:]:
25 code = code.__dict__[name]
26 return code
27
28 def hasattrignorecase(object, name):
29 for attr in dir(object):
30 if attr.lower() == name.lower():
31 return True
32 for attr in dir(object):
33 if attr.lower() == '_' + name.lower():
34 return True
35 return False
36
37
38 def setattrignorecase(object, name, value):
39 for attr in object.__dict__:
40 if attr.lower() == name.lower():
41 object.__dict__[attr] = value
42 return
43 ## for attr in dir(object):
44 ## if attr.lower() == '_' + name.lower():
45 ## object.__dict__[attr] = value
46 ## return
47 object.__dict__[name] = value
48
49 def getattrignorecase(object, name):
50 for attr in object.__dict__:
51 if attr.lower() == name.lower():
52 return object.__dict__[attr]
53 ## for attr in dir(object):
54 ## if attr.lower() == '_' + name.lower():
55 ## return object.__dict__[attr]
56 return object.__dict__[name]
57
58
59 def defaultLoad(fileObject, knownTypes=None):
60 xml = fileObject.read()
61 loadedObject = xmlmarshaller.unmarshal(xml, knownTypes=knownTypes)
62 if hasattr(fileObject, 'name'):
63 loadedObject.fileName = os.path.abspath(fileObject.name)
64 loadedObject.initialize()
65 return loadedObject
66
67 def defaultSave(fileObject, objectToSave, knownTypes=None):
68 xml = xmlmarshaller.marshal(objectToSave, prettyPrint=True, knownTypes=knownTypes)
69 fileObject.write(xml)
70 fileObject.close()
71
72 def clone(objectToClone):
73 xml = xmlmarshaller.marshal(objectToClone, prettyPrint=True)
74 clonedObject = xmlmarshaller.unmarshal(xml)
75 if hasattr(objectToClone, 'fileName'):
76 clonedObject.fileName = objectToClone.fileName
77 clonedObject.initialize()
78 return clonedObject
79
80 def exceptionToString(e):
81 sio = cStringIO.StringIO()
82 traceback.print_exception(e.__class__, e, sys.exc_traceback, file=sio)
83 return sio.getvalue()
84