2005-05-04 20:10:29 -04:00
|
|
|
#----------------------------------------------------------------------------
|
|
|
|
# Name: __init__.py
|
|
|
|
# Purpose: Utilities
|
|
|
|
#
|
|
|
|
# Author: Joel Hare
|
|
|
|
#
|
|
|
|
# Created: 7/28/04
|
|
|
|
# CVS-ID: $Id$
|
|
|
|
# Copyright: (c) 2004-2005 ActiveGrid, Inc.
|
|
|
|
# License: wxWindows License
|
|
|
|
#----------------------------------------------------------------------------
|
|
|
|
|
2005-04-08 18:54:02 -04:00
|
|
|
import logging
|
|
|
|
import cStringIO
|
|
|
|
import traceback
|
|
|
|
import sys
|
|
|
|
import string
|
|
|
|
import os
|
|
|
|
|
|
|
|
def classForName(className):
|
|
|
|
pathList = className.split('.')
|
|
|
|
moduleName = string.join(pathList[:-1], '.')
|
|
|
|
code = __import__(moduleName)
|
|
|
|
for name in pathList[1:]:
|
|
|
|
code = code.__dict__[name]
|
|
|
|
return code
|
|
|
|
|
|
|
|
def hasattrignorecase(object, name):
|
|
|
|
for attr in dir(object):
|
|
|
|
if attr.lower() == name.lower():
|
|
|
|
return True
|
|
|
|
for attr in dir(object):
|
|
|
|
if attr.lower() == '_' + name.lower():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def setattrignorecase(object, name, value):
|
|
|
|
for attr in object.__dict__:
|
|
|
|
if attr.lower() == name.lower():
|
|
|
|
object.__dict__[attr] = value
|
|
|
|
return
|
|
|
|
## for attr in dir(object):
|
|
|
|
## if attr.lower() == '_' + name.lower():
|
|
|
|
## object.__dict__[attr] = value
|
|
|
|
## return
|
|
|
|
object.__dict__[name] = value
|
2005-05-04 20:10:29 -04:00
|
|
|
|
2005-04-08 18:54:02 -04:00
|
|
|
def getattrignorecase(object, name):
|
|
|
|
for attr in object.__dict__:
|
|
|
|
if attr.lower() == name.lower():
|
|
|
|
return object.__dict__[attr]
|
|
|
|
## for attr in dir(object):
|
|
|
|
## if attr.lower() == '_' + name.lower():
|
|
|
|
## return object.__dict__[attr]
|
|
|
|
return object.__dict__[name]
|
|
|
|
|
|
|
|
|
2005-05-04 20:10:29 -04:00
|
|
|
def defaultLoad(fileObject, knownTypes=None):
|
2005-04-08 18:54:02 -04:00
|
|
|
xml = fileObject.read()
|
2005-05-04 20:10:29 -04:00
|
|
|
loadedObject = xmlmarshaller.unmarshal(xml, knownTypes=knownTypes)
|
2005-04-08 18:54:02 -04:00
|
|
|
if hasattr(fileObject, 'name'):
|
|
|
|
loadedObject.fileName = os.path.abspath(fileObject.name)
|
|
|
|
loadedObject.initialize()
|
|
|
|
return loadedObject
|
|
|
|
|
2005-05-04 20:10:29 -04:00
|
|
|
def defaultSave(fileObject, objectToSave, knownTypes=None):
|
|
|
|
xml = xmlmarshaller.marshal(objectToSave, prettyPrint=True, knownTypes=knownTypes)
|
2005-04-08 18:54:02 -04:00
|
|
|
fileObject.write(xml)
|
2005-05-04 20:10:29 -04:00
|
|
|
fileObject.close()
|
2005-04-08 18:54:02 -04:00
|
|
|
|
|
|
|
def clone(objectToClone):
|
|
|
|
xml = xmlmarshaller.marshal(objectToClone, prettyPrint=True)
|
|
|
|
clonedObject = xmlmarshaller.unmarshal(xml)
|
|
|
|
if hasattr(objectToClone, 'fileName'):
|
|
|
|
clonedObject.fileName = objectToClone.fileName
|
|
|
|
clonedObject.initialize()
|
|
|
|
return clonedObject
|
|
|
|
|
|
|
|
def exceptionToString(e):
|
|
|
|
sio = cStringIO.StringIO()
|
|
|
|
traceback.print_exception(e.__class__, e, sys.exc_traceback, file=sio)
|
|
|
|
return sio.getvalue()
|
|
|
|
|