More updates to the docview library modules and sample apps from the
ActiveGrid folks. Their sample IDE is now able to integrate with Subversion. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@34158 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -10,75 +10,19 @@
|
||||
# License: wxWindows License
|
||||
#----------------------------------------------------------------------------
|
||||
|
||||
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 _registerMainModuleDir():
|
||||
global mainModuleDir
|
||||
if sys.executable.find('python') != -1:
|
||||
utilModuleDir = os.path.dirname(__file__)
|
||||
if not os.path.isabs(utilModuleDir):
|
||||
utilModuleDir = os.path.join(os.getcwd(), utilModuleDir)
|
||||
mainModuleDir = os.path.normpath(os.path.join(utilModuleDir, os.path.join(os.path.pardir, os.path.pardir)))
|
||||
else:
|
||||
mainModuleDir = os.path.dirname(sys.executable)
|
||||
|
||||
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
|
||||
|
||||
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]
|
||||
|
||||
|
||||
def defaultLoad(fileObject, knownTypes=None):
|
||||
xml = fileObject.read()
|
||||
loadedObject = xmlmarshaller.unmarshal(xml, knownTypes=knownTypes)
|
||||
if hasattr(fileObject, 'name'):
|
||||
loadedObject.fileName = os.path.abspath(fileObject.name)
|
||||
loadedObject.initialize()
|
||||
return loadedObject
|
||||
|
||||
def defaultSave(fileObject, objectToSave, knownTypes=None):
|
||||
xml = xmlmarshaller.marshal(objectToSave, prettyPrint=True, knownTypes=knownTypes)
|
||||
fileObject.write(xml)
|
||||
fileObject.close()
|
||||
|
||||
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()
|
||||
_registerMainModuleDir()
|
||||
|
||||
|
93
wxPython/samples/ide/activegrid/util/objutils.py
Normal file
93
wxPython/samples/ide/activegrid/util/objutils.py
Normal file
@@ -0,0 +1,93 @@
|
||||
#----------------------------------------------------------------------------
|
||||
# Name: objutils.py
|
||||
# Purpose: Object Utilities
|
||||
#
|
||||
# Author: Alan Mullendore
|
||||
#
|
||||
# Created: 5/10/05
|
||||
# CVS-ID: $Id$
|
||||
# Copyright: (c) 2004-2005 ActiveGrid, Inc.
|
||||
# License: wxWindows License
|
||||
#----------------------------------------------------------------------------
|
||||
|
||||
import logging
|
||||
import traceback
|
||||
import sys
|
||||
import os
|
||||
|
||||
import xmlmarshaller
|
||||
|
||||
def defaultLoad(fileObject, knownTypes=None):
|
||||
xml = fileObject.read()
|
||||
loadedObject = xmlmarshaller.unmarshal(xml, knownTypes=knownTypes)
|
||||
if hasattr(fileObject, 'name'):
|
||||
loadedObject.fileName = os.path.abspath(fileObject.name)
|
||||
loadedObject.initialize()
|
||||
return loadedObject
|
||||
|
||||
def defaultSave(fileObject, objectToSave, knownTypes=None, withEncoding=1, encoding='utf-8'):
|
||||
xml = xmlmarshaller.marshal(objectToSave, prettyPrint=True, knownTypes=knownTypes, withEncoding=withEncoding, encoding=encoding)
|
||||
fileObject.write(xml)
|
||||
fileObject.flush()
|
||||
|
||||
def clone(objectToClone, knownTypes=None, encoding='utf-8'):
|
||||
xml = xmlmarshaller.marshal(objectToClone, prettyPrint=True, knownTypes=knownTypes, encoding=encoding)
|
||||
clonedObject = xmlmarshaller.unmarshal(xml, knownTypes=knownTypes)
|
||||
if hasattr(objectToClone, 'fileName'):
|
||||
clonedObject.fileName = objectToClone.fileName
|
||||
try:
|
||||
clonedObject.initialize()
|
||||
except AttributeError:
|
||||
pass
|
||||
return clonedObject
|
||||
|
||||
def classForName(className):
|
||||
pathList = className.split('.')
|
||||
moduleName = '.'.join(pathList[:-1])
|
||||
code = __import__(moduleName)
|
||||
for name in pathList[1:]:
|
||||
code = code.__dict__[name]
|
||||
return code
|
||||
|
||||
def hasattrignorecase(object, name):
|
||||
namelow = name.lower()
|
||||
for attr in dir(object):
|
||||
if attr.lower() == namelow:
|
||||
return True
|
||||
for attr in dir(object):
|
||||
if attr.lower() == '_' + namelow:
|
||||
return True
|
||||
return False
|
||||
|
||||
def setattrignorecase(object, name, value):
|
||||
namelow = name.lower()
|
||||
for attr in object.__dict__:
|
||||
if attr.lower() == namelow:
|
||||
object.__dict__[attr] = value
|
||||
return
|
||||
object.__dict__[name] = value
|
||||
|
||||
def getattrignorecase(object, name):
|
||||
namelow = name.lower()
|
||||
for attr in object.__dict__:
|
||||
if attr.lower() == namelow:
|
||||
return object.__dict__[attr]
|
||||
return object.__dict__[name]
|
||||
|
||||
def hasPropertyValue(obj, attr):
|
||||
hasProp = False
|
||||
try:
|
||||
prop = obj.__class__.__dict__[attr]
|
||||
if (isinstance(prop, property)):
|
||||
hasProp = hasattr(obj, attr)
|
||||
if (hasProp):
|
||||
# It's a property and it has a value but sometimes we don't want it.
|
||||
# If there is a _hasattr method execute it and the
|
||||
# result will tell us whether to include this value
|
||||
try:
|
||||
hasProp = obj._hasattr(attr)
|
||||
except:
|
||||
pass
|
||||
except KeyError:
|
||||
pass
|
||||
return hasProp
|
@@ -9,14 +9,17 @@
|
||||
# Copyright: (c) 2004-2005 ActiveGrid, Inc.
|
||||
# License: wxWindows License
|
||||
#----------------------------------------------------------------------------
|
||||
from activegrid import util
|
||||
import inspect
|
||||
import __builtin__
|
||||
import sys
|
||||
from types import *
|
||||
import xml.sax
|
||||
import xml.sax.handler
|
||||
import __builtin__
|
||||
from xml.sax import saxutils
|
||||
|
||||
import objutils
|
||||
|
||||
MODULE_PATH = "__main__"
|
||||
|
||||
### ToDO remove maxOccurs "unbounded" resolves to -1 hacks after bug 177 is fixed
|
||||
|
||||
"""
|
||||
@@ -147,52 +150,6 @@ MEMBERS_TO_SKIP = ('__module__', '__doc__', '__xmlname__', '__xmlattributes__',
|
||||
'__xmldefaultnamespace__', '__xmlattrnamespaces__',
|
||||
'__xmlattrgroups__')
|
||||
|
||||
#WELL_KNOWN_OBJECTS = { #"xs:element" : "activegrid.model.schema.XsdElement",
|
||||
#"xs:complexType" : "activegrid.model.schema.XsdComplexType",
|
||||
#"xs:sequence" : "activegrid.model.schema.XsdSequence",
|
||||
#"xs:element" : "activegrid.model.schema.XsdElement",
|
||||
#"xs:key" : "activegrid.model.schema.XsdKey",
|
||||
#"xs:field" : "activegrid.model.schema.XsdKeyField",
|
||||
#"xs:keyref" : "activegrid.model.schema.XsdKeyRef",
|
||||
#"xs:selector" : "activegrid.model.schema.XsdKeySelector",
|
||||
#"xs:schema" : "activegrid.model.schema.Schema",
|
||||
#"ag:schemaOptions":"activegrid.model.schema.SchemaOptions",
|
||||
#"ag:debug" : "activegrid.model.processmodel.DebugOperation",
|
||||
#"ag:body" : "activegrid.model.processmodel.Body", # alan (start)
|
||||
#"ag:cssRule" : "activegrid.model.processmodel.CssRule",
|
||||
#"ag:datasource" : "activegrid.data.dataservice.DataSource",
|
||||
#"ag:deployment" : "activegrid.server.deployment.Deployment",
|
||||
#"ag:glue" : "activegrid.model.processmodel.Glue",
|
||||
#"ag:hr" : "activegrid.model.processmodel.HorizontalRow",
|
||||
#"ag:image" : "activegrid.model.processmodel.Image",
|
||||
#"ag:inputs" : "activegrid.model.processmodel.Inputs",
|
||||
#"ag:label" : "activegrid.model.processmodel.Label",
|
||||
#"ag:processmodel" : "activegrid.model.processmodel.ProcessModel",
|
||||
#"ag:processmodelref" : "activegrid.server.deployment.ProcessModelRef",
|
||||
#"ag:query" : "activegrid.model.processmodel.Query",
|
||||
#"ag:schemaref" : "activegrid.server.deployment.SchemaRef",
|
||||
#"ag:set" : "activegrid.model.processmodel.SetOperation",
|
||||
#"ag:text" : "activegrid.model.processmodel.Text",
|
||||
#"ag:title" : "activegrid.model.processmodel.Title",
|
||||
#"ag:view" : "activegrid.model.processmodel.View",
|
||||
#"bpws:case" : "activegrid.model.processmodel.BPELCase",
|
||||
#"bpws:invoke" : "activegrid.model.processmodel.BPELInvoke",
|
||||
#"bpws:otherwise" : "activegrid.model.processmodel.BPELOtherwise",
|
||||
#"bpws:process" : "activegrid.model.processmodel.BPELProcess",
|
||||
#"bpws:reply" : "activegrid.model.processmodel.BPELReply",
|
||||
#"bpws:switch" : "activegrid.model.processmodel.BPELSwitch",
|
||||
#"bpws:variable" : "activegrid.model.processmodel.BPELVariable",
|
||||
#"projectmodel" : "activegrid.tool.ProjectEditor.ProjectModel",
|
||||
#"wsdl:message" : "activegrid.model.processmodel.WSDLMessage",
|
||||
#"wsdl:part" : "activegrid.model.processmodel.WSDLPart",
|
||||
#"xforms:group" : "activegrid.model.processmodel.XFormsGroup",
|
||||
#"xforms:input" : "activegrid.model.processmodel.XFormsInput",
|
||||
#"xforms:label" : "activegrid.model.processmodel.XFormsLabel",
|
||||
#"xforms:output" : "activegrid.model.processmodel.XFormsOutput",
|
||||
#"xforms:secret" : "activegrid.model.processmodel.XFormsSecret",
|
||||
#"xforms:submit" : "activegrid.model.processmodel.XFormsSubmit"} # alan(end)
|
||||
|
||||
|
||||
################################################################################
|
||||
#
|
||||
# classes and functions
|
||||
@@ -200,45 +157,42 @@ MEMBERS_TO_SKIP = ('__module__', '__doc__', '__xmlname__', '__xmlattributes__',
|
||||
################################################################################
|
||||
|
||||
def _objectfactory(objname, objargs=None, xsname=None):
|
||||
try:
|
||||
'''dynamically create an object based on the objname and return
|
||||
it. look it up in the BASETYPE_ELEMENT_MAP first.
|
||||
'''
|
||||
# split the objname into the typename and module path,
|
||||
# importing the module if need be.
|
||||
if not isinstance(objargs, list):
|
||||
objargs = [objargs]
|
||||
'''dynamically create an object based on the objname and return
|
||||
it. look it up in the BASETYPE_ELEMENT_MAP first.
|
||||
'''
|
||||
# split the objname into the typename and module path,
|
||||
# importing the module if need be.
|
||||
if not isinstance(objargs, list):
|
||||
objargs = [objargs]
|
||||
|
||||
if (xsname):
|
||||
try:
|
||||
objname = knownGlobalTypes[xsname]
|
||||
except KeyError:
|
||||
pass
|
||||
if (xsname):
|
||||
try:
|
||||
objname = knownGlobalTypes[xsname]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
## print "[objectfactory] creating an object of type %s and value %s, xsname=%s" % (objname, objargs, xsname)
|
||||
objtype = objname.split('.')[-1]
|
||||
pathlist = objname.split('.')
|
||||
modulename = '.'.join(pathlist[0:-1])
|
||||
## print "[objectfactory] creating an object of type %s and value %s, xsname=%s" % (objname, objargs, xsname)
|
||||
objtype = objname.split('.')[-1]
|
||||
pathlist = objname.split('.')
|
||||
modulename = '.'.join(pathlist[0:-1])
|
||||
|
||||
## print "[objectfactory] objtype is %s" % objtype
|
||||
## print "[objectfactory] objargs is %s" % `objargs`
|
||||
|
||||
## since the bool constructor will turn a string of non-zero
|
||||
## length into True, we call it with no argument (yielding a
|
||||
## False) if the string contains 'false'
|
||||
if objtype == 'bool' and objargs[0].lower() == 'false':
|
||||
objargs = None
|
||||
|
||||
## if objtype == 'str':
|
||||
## print type(objargs)
|
||||
## print "string we're unescaping: '%s'" % objargs[0]
|
||||
## objargs = saxutils.unescape(objargs[0])
|
||||
if objtype in ('float', 'int', 'str', 'long'):
|
||||
## print "[objectfactory] object [%s] %s(%r)" % (objname, objtype, objargs)
|
||||
if objname == 'bool':
|
||||
return not objargs[0].lower() == 'false'
|
||||
elif objname == 'str': # don't strip strings - blanks are significant !!!
|
||||
if len(objargs) > 0:
|
||||
return saxutils.unescape(objargs[0]).encode()
|
||||
else:
|
||||
return ''
|
||||
elif objname == 'unicode': # don't strip strings - blanks are significant !!!
|
||||
if len(objargs) > 0:
|
||||
return saxutils.unescape(objargs[0]).encode()
|
||||
else:
|
||||
return ''
|
||||
elif objtype in ('float', 'int', 'str', 'long'):
|
||||
objargs = [x.strip() for x in objargs]
|
||||
|
||||
if objtype == 'str':
|
||||
objargs = [saxutils.unescape(x) for x in objargs]
|
||||
|
||||
try:
|
||||
if __builtin__.__dict__.has_key(objname):
|
||||
module = __builtin__
|
||||
else:
|
||||
@@ -379,7 +333,7 @@ class XMLObjectFactory(xml.sax.ContentHandler):
|
||||
if attrname == "maxOccurs" and attr == "unbounded":
|
||||
attr = "-1"
|
||||
attr = _objectfactory(type, attr)
|
||||
util.setattrignorecase(obj, _toAttrName(obj, attrname), attr)
|
||||
objutils.setattrignorecase(obj, _toAttrName(obj, attrname), attr)
|
||||
## obj.__dict__[_toAttrName(obj, attrname)] = attr
|
||||
# stuff any child attributes meant to be in a sequence via the __xmlflattensequence__
|
||||
flattenDict = {}
|
||||
@@ -407,9 +361,7 @@ class XMLObjectFactory(xml.sax.ContentHandler):
|
||||
try:
|
||||
## print "[endElement] obj.__dict__ is: ", obj.__dict__
|
||||
sequencevalue = obj.__dict__[sequencename]
|
||||
except AttributeError:
|
||||
sequencevalue = None
|
||||
except KeyError:
|
||||
except (AttributeError, KeyError):
|
||||
sequencevalue = None
|
||||
if sequencevalue == None:
|
||||
sequencevalue = []
|
||||
@@ -420,7 +372,7 @@ class XMLObjectFactory(xml.sax.ContentHandler):
|
||||
obj.append(child)
|
||||
else:
|
||||
## print "childname = %s, obj = %s, child = %s" % (childname, repr(obj), repr(child))
|
||||
util.setattrignorecase(obj, _toAttrName(obj, childname), child)
|
||||
objutils.setattrignorecase(obj, _toAttrName(obj, childname), child)
|
||||
obj.__dict__[_toAttrName(obj, childname)] = child
|
||||
|
||||
if complexType:
|
||||
@@ -474,6 +426,8 @@ def xsdToPythonType(xsdType):
|
||||
def _getXmlValue(pythonValue):
|
||||
if (isinstance(pythonValue, bool)):
|
||||
return str(pythonValue).lower()
|
||||
elif (isinstance(pythonValue, unicode)):
|
||||
return pythonValue.encode()
|
||||
else:
|
||||
return str(pythonValue)
|
||||
|
||||
@@ -488,7 +442,17 @@ def unmarshal(xmlstr, knownTypes=None):
|
||||
return objectfactory.getRootObject()
|
||||
|
||||
|
||||
def marshal(obj, elementName=None, nameSpacePrefix='', nameSpaces=None, prettyPrint=False, indent=0, knownTypes=None):
|
||||
def marshal(obj, elementName=None, prettyPrint=False, indent=0, knownTypes=None, withEncoding=True, encoding=None):
|
||||
xmlstr = ''.join(_marshal(obj, elementName, prettyPrint=prettyPrint, indent=indent, knownTypes=knownTypes))
|
||||
if withEncoding:
|
||||
if encoding is None:
|
||||
return '<?xml version="1.0" encoding="%s"?>\n%s' % (sys.getdefaultencoding(), xmlstr)
|
||||
else:
|
||||
return '<?xml version="1.0" encoding="%s"?>\n%s' % (encoding, xmlstr.encode(encoding))
|
||||
else:
|
||||
return xmlstr
|
||||
|
||||
def _marshal(obj, elementName=None, nameSpacePrefix='', nameSpaces=None, prettyPrint=False, indent=0, knownTypes=None):
|
||||
if prettyPrint or indent:
|
||||
prefix = ' '*indent
|
||||
newline = '\n'
|
||||
@@ -563,7 +527,7 @@ def marshal(obj, elementName=None, nameSpacePrefix='', nameSpaces=None, prettyPr
|
||||
except KeyError:
|
||||
continue
|
||||
## # But, check and see if it is a property first:
|
||||
## if (hasPropertyValue(obj, attr)):
|
||||
## if (objutils.hasPropertyValue(obj, attr)):
|
||||
## value = getattr(obj, attr)
|
||||
## else:
|
||||
## continue
|
||||
@@ -593,7 +557,7 @@ def marshal(obj, elementName=None, nameSpacePrefix='', nameSpaces=None, prettyPr
|
||||
if hasattr(obj, '__xmlattrnamespaces__'):
|
||||
## print "marshal: found __xmlattrnamespaces__"
|
||||
for nameSpaceKey, nameSpaceAttributes in getattr(obj, '__xmlattrnamespaces__').items():
|
||||
if nameSpaceKey == nameSpacePrefix[:-1]: # Don't need to specify attribute namespace if it is the same as it selement
|
||||
if nameSpaceKey == nameSpacePrefix[:-1]: # Don't need to specify attribute namespace if it is the same as it's element
|
||||
continue
|
||||
if attr in nameSpaceAttributes:
|
||||
attrNameSpacePrefix = nameSpaceKey + ':'
|
||||
@@ -607,95 +571,76 @@ def marshal(obj, elementName=None, nameSpacePrefix='', nameSpaces=None, prettyPr
|
||||
objattrs += ' %s%s="%s"' % (attrNameSpacePrefix, attr, value)
|
||||
## print "marshal: new objattrs is: ", objattrs
|
||||
|
||||
objtype = type(obj)
|
||||
if isinstance(obj, NoneType):
|
||||
#print "marshal: skipping an element with no type"
|
||||
return ''
|
||||
# return '%s<%s objtype="None"/>%s' % (prefix, elementName, newline)
|
||||
elif isinstance(obj, bool):
|
||||
xmlString = '%s<%s objtype="bool">%s</%s>%s' % (prefix, elementName, obj, elementName, newline)
|
||||
#print "marshal: returning a bool element: \n", xmlString
|
||||
return xmlString
|
||||
return ['%s<%s objtype="bool">%s</%s>%s' % (prefix, elementName, obj, elementName, newline)]
|
||||
elif isinstance(obj, int):
|
||||
xmlString = '''%s<%s objtype="int">%s</%s>%s''' % (prefix, elementName, str(obj), elementName, newline)
|
||||
#print "marshal: returning a int element: \n", xmlString
|
||||
return xmlString
|
||||
return ['''%s<%s objtype="int">%s</%s>%s''' % (prefix, elementName, str(obj), elementName, newline)]
|
||||
elif isinstance(obj, long):
|
||||
xmlString = '%s<%s objtype="long">%s</%s>%s' % (prefix, elementName, str(obj), elementName, newline)
|
||||
#print "marshal: returning a long element: \n", xmlString
|
||||
return xmlString
|
||||
return ['%s<%s objtype="long">%s</%s>%s' % (prefix, elementName, str(obj), elementName, newline)]
|
||||
elif isinstance(obj, float):
|
||||
xmlString = '%s<%s objtype="float">%s</%s>%s' % (prefix, elementName, str(obj), elementName, newline)
|
||||
#print "marshal: returning a float element: \n", xmlString
|
||||
return xmlString
|
||||
return ['%s<%s objtype="float">%s</%s>%s' % (prefix, elementName, str(obj), elementName, newline)]
|
||||
elif isinstance(obj, unicode): # have to check before basestring - unicode is instance of base string
|
||||
return ['''%s<%s>%s</%s>%s''' % (prefix, elementName, saxutils.escape(obj.encode()), elementName, newline)]
|
||||
elif isinstance(obj, basestring):
|
||||
xmlString = '''%s<%s>%s</%s>%s''' % (prefix, elementName, saxutils.escape(obj), elementName, newline)
|
||||
#print "marshal: returning a str element: \n", xmlString
|
||||
return xmlString
|
||||
## elif isinstance(obj, unicode):
|
||||
## return '''%s<%s>%s</%s>%s''' % (prefix, elementName, obj, elementName, newline)
|
||||
return ['''%s<%s>%s</%s>%s''' % (prefix, elementName, saxutils.escape(obj), elementName, newline)]
|
||||
elif isinstance(obj, list):
|
||||
if len(obj) < 1:
|
||||
#print "marshal: skipping an empty list"
|
||||
return ''
|
||||
xmlString = '%s<%s objtype="list">%s' % (prefix, elementName, newline)
|
||||
xmlString = ['%s<%s objtype="list">%s' % (prefix, elementName, newline)]
|
||||
for item in obj:
|
||||
xmlString += marshal(item, nameSpaces=nameSpaces, indent=indent+increment, knownTypes=knownTypes)
|
||||
xmlString += '%s</%s>%s' % (prefix, elementName, newline)
|
||||
#print "marshal: returning a list element: \n", xmlString
|
||||
xmlString.extend(_marshal(item, nameSpaces=nameSpaces, indent=indent+increment, knownTypes=knownTypes))
|
||||
xmlString.append('%s</%s>%s' % (prefix, elementName, newline))
|
||||
return xmlString
|
||||
elif isinstance(obj, tuple):
|
||||
if len(obj) < 1:
|
||||
#print "marshal: skipping an empty tuple"
|
||||
return ''
|
||||
xmlString = '%s<%s objtype="list" mutable="false">%s' % (prefix, elementName, newline)
|
||||
xmlString = ['%s<%s objtype="list" mutable="false">%s' % (prefix, elementName, newline)]
|
||||
for item in obj:
|
||||
xmlString += marshal(item, nameSpaces=nameSpaces, indent=indent+increment, knownTypes=knownTypes)
|
||||
xmlString += '%s</%s>%s' % (prefix, elementName, newline)
|
||||
#print "marshal: returning a tuple element: \n", xmlString
|
||||
xmlString.extend(_marshal(item, nameSpaces=nameSpaces, indent=indent+increment, knownTypes=knownTypes))
|
||||
xmlString.append('%s</%s>%s' % (prefix, elementName, newline))
|
||||
return xmlString
|
||||
elif isinstance(obj, dict):
|
||||
xmlString = '%s<%s objtype="dict">%s' % (prefix, elementName, newline)
|
||||
xmlString = ['%s<%s objtype="dict">%s' % (prefix, elementName, newline)]
|
||||
subprefix = prefix + ' '*increment
|
||||
subindent = indent + 2*increment
|
||||
for key, val in obj.iteritems():
|
||||
xmlString += "%s<key>%s%s%s</key>%s%s<value>%s%s%s</value>%s" \
|
||||
% (subprefix, newline, marshal(key, indent=subindent, knownTypes=knownTypes), subprefix, newline, subprefix, newline, marshal(val, nameSpaces=nameSpaces, indent=subindent, knownTypes=knownTypes), subprefix, newline)
|
||||
xmlString += '%s</%s>%s' % (prefix, elementName, newline)
|
||||
#print "marshal: returning a dict element: \n", xmlString
|
||||
xmlString.append("%s<key>%s" % (subprefix, newline))
|
||||
xmlString.extend(_marshal(key, indent=subindent, knownTypes=knownTypes))
|
||||
xmlString.append("%s</key>%s%s<value>%s" % (subprefix, newline, subprefix, newline))
|
||||
xmlString.extend(_marshal(val, nameSpaces=nameSpaces, indent=subindent, knownTypes=knownTypes))
|
||||
xmlString.append("%s</value>%s" % (subprefix, newline))
|
||||
xmlString.append('%s</%s>%s' % (prefix, elementName, newline))
|
||||
return xmlString
|
||||
else:
|
||||
moduleName = obj.__class__.__module__
|
||||
if (moduleName == "activegrid.model.schema"):
|
||||
## print "marshal: found an activegrid.model.schema class element"
|
||||
xmlString = '%s<%s%s%s' % (prefix, elementName, nameSpaceAttrs, objattrs)
|
||||
xmlString = ['%s<%s%s%s' % (prefix, elementName, nameSpaceAttrs, objattrs)]
|
||||
else:
|
||||
## print "marshal: found a ", moduleName, " class element"
|
||||
# Only add the objtype if the element tag is unknown to us.
|
||||
try:
|
||||
objname = knownTypes[elementName]
|
||||
## print "successfully mapped ", elementName, " to known-objtype ", objname
|
||||
xmlString = '%s<%s%s%s' % (prefix, elementName, nameSpaceAttrs, objattrs)
|
||||
xmlString = ['%s<%s%s%s' % (prefix, elementName, nameSpaceAttrs, objattrs)]
|
||||
except KeyError:
|
||||
## print "failed to map elementName: ", elementName, "; knownTypes: ", knownTypes
|
||||
xmlString = '%s<%s%s%s objtype="%s.%s"' % (prefix, elementName, nameSpaceAttrs, objattrs, moduleName, className)
|
||||
xmlString = ['%s<%s%s%s objtype="%s.%s"' % (prefix, elementName, nameSpaceAttrs, objattrs, moduleName, className)]
|
||||
## print "UnknownTypeException: Unknown type (%s.%s) passed to marshaller" % (moduleName, className)
|
||||
# get the member, value pairs for the object, filtering out
|
||||
# the types we don't support.
|
||||
## print "marshal: elementString: \n", xmlString
|
||||
# get the member, value pairs for the object, filtering out the types we don't support
|
||||
if (elementAdd != None):
|
||||
prefix += increment*' '
|
||||
indent += increment
|
||||
|
||||
xmlMemberString = ''
|
||||
xmlMemberString = []
|
||||
if hasattr(obj, '__xmlbody__'):
|
||||
xmlMemberString = getattr(obj, obj.__xmlbody__)
|
||||
xmlbody = getattr(obj, obj.__xmlbody__)
|
||||
if xmlbody != None:
|
||||
xmlMemberString.append(xmlbody)
|
||||
else:
|
||||
entryList = obj.__dict__.items()
|
||||
## # Add in properties
|
||||
## for key in obj.__class__.__dict__.iterkeys():
|
||||
## if (key not in members_to_skip and key not in obj.__dict__
|
||||
## and hasPropertyValue(obj, key)):
|
||||
## and objutils.hasPropertyValue(obj, key)):
|
||||
## value = getattr(obj, key)
|
||||
## entryList.append((key, value))
|
||||
entryList.sort()
|
||||
@@ -719,7 +664,7 @@ def marshal(obj, elementName=None, nameSpacePrefix='', nameSpaces=None, prettyPr
|
||||
if (eName != '__nogroup__'):
|
||||
prefix += increment*' '
|
||||
indent += increment
|
||||
xmlMemberString += '%s<%s objtype="None">%s' % (prefix, eName, newline)
|
||||
xmlMemberString.append('%s<%s objtype="None">%s' % (prefix, eName, newline))
|
||||
for name in eList:
|
||||
value = obj.__dict__[name]
|
||||
## print " ", name, " = ", value
|
||||
@@ -754,91 +699,38 @@ def marshal(obj, elementName=None, nameSpacePrefix='', nameSpaces=None, prettyPr
|
||||
xmlname = name
|
||||
## xmlname = name.lower()
|
||||
for seqitem in value:
|
||||
xmlMemberString += marshal(seqitem, xmlname, subElementNameSpacePrefix, nameSpaces=nameSpaces, indent=indent+increment, knownTypes=knownTypes)
|
||||
xmlMemberString.extend(_marshal(seqitem, xmlname, subElementNameSpacePrefix, nameSpaces=nameSpaces, indent=indent+increment, knownTypes=knownTypes))
|
||||
else:
|
||||
if (hasattr(obj, "__xmlrename__") and name in obj.__xmlrename__):
|
||||
xmlname = obj.__xmlrename__[name]
|
||||
else:
|
||||
xmlname = name
|
||||
## xmlname = name.lower()
|
||||
## # skip
|
||||
## if xmlname.startswith('_') and not xmlname.startswith('__'):
|
||||
## xmlname = xmlname[1:]
|
||||
## if (indent > 30):
|
||||
## print "getting pretty deep, xmlname = ", xmlname
|
||||
## print "marshal: marshalling ", xmlname
|
||||
xmlMemberString += marshal(value, xmlname, subElementNameSpacePrefix, nameSpaces=nameSpaces, indent=indent+increment, knownTypes=knownTypes)
|
||||
## print "marshal: back with new xmlMemberString: \n", xmlMemberString
|
||||
xmlMemberString.extend(_marshal(value, xmlname, subElementNameSpacePrefix, nameSpaces=nameSpaces, indent=indent+increment, knownTypes=knownTypes))
|
||||
if (eName != '__nogroup__'):
|
||||
## print "marshal: Completing attrGroup ", eName
|
||||
xmlMemberString += '%s</%s>%s' % (prefix, eName, newline)
|
||||
xmlMemberString.append('%s</%s>%s' % (prefix, eName, newline))
|
||||
prefix = prefix[:-increment]
|
||||
indent -= increment
|
||||
|
||||
# if we have nested elements, add them here, otherwise close the element tag immediately.
|
||||
if xmlMemberString:
|
||||
xmlString += '>'
|
||||
xmlMemberString = filter(lambda x: len(x)>0, xmlMemberString)
|
||||
if len(xmlMemberString) > 0:
|
||||
xmlString.append('>')
|
||||
if hasattr(obj, '__xmlbody__'):
|
||||
xmlString += xmlMemberString
|
||||
xmlString += '</%s>%s' % (elementName, newline)
|
||||
xmlString.extend(xmlMemberString)
|
||||
xmlString.append('</%s>%s' % (elementName, newline))
|
||||
else:
|
||||
xmlString += newline
|
||||
xmlString.append(newline)
|
||||
if (elementAdd != None):
|
||||
xmlString += '%s<%s>%s' % (prefix, elementAdd, newline)
|
||||
xmlString += xmlMemberString
|
||||
xmlString.append('%s<%s>%s' % (prefix, elementAdd, newline))
|
||||
xmlString.extend(xmlMemberString)
|
||||
if (elementAdd != None):
|
||||
xmlString += '%s</%s>%s' % (prefix, elementAdd, newline)
|
||||
xmlString.append('%s</%s>%s' % (prefix, elementAdd, newline))
|
||||
prefix = prefix[:-increment]
|
||||
indent -= increment
|
||||
xmlString += '%s</%s>%s' % (prefix, elementName, newline)
|
||||
xmlString.append('%s</%s>%s' % (prefix, elementName, newline))
|
||||
else:
|
||||
xmlString = xmlString + '/>%s' % newline
|
||||
xmlString.append('/>%s' % newline)
|
||||
return xmlString
|
||||
|
||||
# We don't use this anymore but in case we want to get properties this is how
|
||||
# you do it
|
||||
def hasPropertyValue(obj, attr):
|
||||
hasProp = False
|
||||
try:
|
||||
prop = obj.__class__.__dict__[attr]
|
||||
if (isinstance(prop, property)):
|
||||
hasProp = hasattr(obj, attr)
|
||||
if (hasProp):
|
||||
# It's a property and it has a value but sometimes we don't want it.
|
||||
# If there is a _hasattr method execute it and the
|
||||
# result will tell us whether to include this value
|
||||
try:
|
||||
hasProp = obj._hasattr(attr)
|
||||
except:
|
||||
pass
|
||||
except KeyError:
|
||||
pass
|
||||
return hasProp
|
||||
|
||||
if __name__ == '__main__':
|
||||
from xmlmarshallertests import Person, marshalledint, marshalledlist
|
||||
|
||||
l = [1, 2, 3]
|
||||
d = {'1': 1, '2': 2}
|
||||
outerlist = [l]
|
||||
xmlstr = marshal(d, "d", prettyPrint=True)
|
||||
print xmlstr
|
||||
|
||||
person = Person()
|
||||
person.firstName = "Albert"
|
||||
person.lastName = "Camus"
|
||||
person.addressLine1 = "23 Absurd St."
|
||||
person.city = "Ennui"
|
||||
person.state = "MO"
|
||||
person.zip = "54321"
|
||||
person._phoneNumber = "808-303-2323"
|
||||
person.favoriteWords = ['angst', 'ennui', 'existence']
|
||||
person.weight = 150
|
||||
|
||||
xmlstring = marshal(person, 'person', prettyPrint=True)
|
||||
print xmlstring
|
||||
|
||||
obj = unmarshal(marshalledlist)
|
||||
print "obj has type %s and value %s" % (type(obj), str(obj))
|
||||
for item in obj:
|
||||
print "item: %s" % str(item)
|
||||
|
@@ -1,183 +0,0 @@
|
||||
#----------------------------------------------------------------------------
|
||||
# Name: xmlmarshallertests.py
|
||||
# Purpose:
|
||||
#
|
||||
# Author: John Spurling
|
||||
#
|
||||
# Created: 8/16/04
|
||||
# CVS-ID: $Id$
|
||||
# Copyright: (c) 2004-2005 ActiveGrid, Inc.
|
||||
# License: wxWindows License
|
||||
#----------------------------------------------------------------------------
|
||||
|
||||
import unittest
|
||||
import xmlmarshaller
|
||||
from xmlprettyprinter import xmlprettyprint
|
||||
|
||||
marshalledPersonObject = """
|
||||
<person objtype="Person">
|
||||
<firstName>Albert</firstName>
|
||||
<lastName>Camus</lastName>
|
||||
<address>23 Absurd St.</address>
|
||||
<city>Ennui</city>
|
||||
<state>MO</state>
|
||||
<zip>54321</zip>
|
||||
<_phoneNumber>808-303-2323</_phoneNumber>
|
||||
<favoriteWords objtype="list">
|
||||
<item>angst</item>
|
||||
<item>ennui</item>
|
||||
<item>existence</item>
|
||||
</favoriteWords>
|
||||
<weight objtype="float">150</weight>
|
||||
</person>
|
||||
"""
|
||||
|
||||
marshalledint = '''
|
||||
<item objtype="int">23</item>
|
||||
'''
|
||||
|
||||
marshalledlist = '''
|
||||
<mylist objtype="list">
|
||||
<item>foo</item>
|
||||
<item>bar</item>
|
||||
</mylist>
|
||||
'''
|
||||
|
||||
## a dummy class taken from the old XmlMarshaller module.
|
||||
## class Person:
|
||||
## def __init__(self):
|
||||
## # These are not necessary but are nice if you want to tailor
|
||||
## # the Python object <-> XML binding
|
||||
|
||||
## # The xml element name to use for this object, otherwise it
|
||||
## # will use a fully qualified Python name like __main__.Person
|
||||
## # which can be ugly.
|
||||
## self.__xmlname__ = "person"
|
||||
## self.firstName = None
|
||||
## self.lastName = None
|
||||
## self.addressLine1 = None
|
||||
## self.addressLine2 = None
|
||||
## self.city = None
|
||||
## self.state = None
|
||||
## self.zip = None
|
||||
## self._phoneNumber = None
|
||||
## self.favoriteWords = None
|
||||
## self.weight = None
|
||||
class Person:
|
||||
__xmlflattensequence__ = {'asequence': ('the_earth_is_flat',)}
|
||||
|
||||
class XmlMarshallerTestFunctions(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
'''common setup code goes here.'''
|
||||
pass
|
||||
|
||||
def testInt(self):
|
||||
xml = xmlmarshaller.marshal(1)
|
||||
print "\n#########################################"
|
||||
print "# testString test case #"
|
||||
print "#########################################"
|
||||
print "marshalled int object:\n"
|
||||
print xmlprettyprint(xml)
|
||||
|
||||
def testDict(self):
|
||||
xml = xmlmarshaller.marshal({'one': 1,
|
||||
'two': 2,
|
||||
'three': 3})
|
||||
print "\n#########################################"
|
||||
print "# testString test case #"
|
||||
print "#########################################"
|
||||
print "marshalled dict object:\n"
|
||||
print xmlprettyprint(xml)
|
||||
|
||||
def testBool(self):
|
||||
xmltrue = xmlmarshaller.marshal(True)
|
||||
xmlfalse = xmlmarshaller.marshal(False)
|
||||
print "\n#########################################"
|
||||
print "# testBool test case #"
|
||||
print "#########################################"
|
||||
print "marshalled boolean true object:\n"
|
||||
print xmlprettyprint(xmltrue)
|
||||
print "\nmarshalled boolean false object:\n"
|
||||
print xmlprettyprint(xmlfalse)
|
||||
pytrue = xmlmarshaller.unmarshal(xmltrue)
|
||||
assert pytrue is True
|
||||
pyfalse = xmlmarshaller.unmarshal(xmlfalse)
|
||||
assert pyfalse is False
|
||||
|
||||
def testString(self):
|
||||
xml = xmlmarshaller.marshal(
|
||||
"all your marshalled objects are belong to us")
|
||||
print "\n#########################################"
|
||||
print "# testString test case #"
|
||||
print "#########################################"
|
||||
print xmlprettyprint(xml)
|
||||
|
||||
def testEmptyElement(self):
|
||||
person = Person()
|
||||
person.firstName = "Albert"
|
||||
person.__xmlattributes__ = ('firstName',)
|
||||
xml = xmlmarshaller.marshal(person, 'person')
|
||||
print "\n#########################################"
|
||||
print "# testEmptyElement test case #"
|
||||
print "#########################################"
|
||||
print xml
|
||||
assert (xml == """<person objtype="__main__.Person" firstName="Albert"/>""")
|
||||
|
||||
def testXMLFlattenSequence(self):
|
||||
person = Person()
|
||||
person.asequence = ('one', 'two')
|
||||
xml = xmlmarshaller.marshal(person, 'person')
|
||||
print "\n#########################################"
|
||||
print "# testXMLFlattenSequence test case #"
|
||||
print "#########################################"
|
||||
print xml
|
||||
assert (xml == """<person objtype="__main__.Person"><the_earth_is_flat>one</the_earth_is_flat><the_earth_is_flat>two</the_earth_is_flat></person>""")
|
||||
unmarshalledperson = xmlmarshaller.unmarshal(xml)
|
||||
assert(hasattr(unmarshalledperson, 'asequence'))
|
||||
assert(len(unmarshalledperson.asequence) == 2)
|
||||
|
||||
def testInstance(self):
|
||||
print "\n#########################################"
|
||||
print "# testInstance test case #"
|
||||
print "#########################################"
|
||||
class Foo:
|
||||
def __init__(self):
|
||||
self.alist = [1,2]
|
||||
self.astring = 'f00'
|
||||
f = Foo()
|
||||
xml = xmlmarshaller.marshal(f, 'foo')
|
||||
print xml
|
||||
|
||||
def testPerson(self):
|
||||
person = Person()
|
||||
person.firstName = "Albert"
|
||||
person.lastName = "Camus"
|
||||
person.addressLine1 = "23 Absurd St."
|
||||
person.city = "Ennui"
|
||||
person.state = "MO"
|
||||
person.zip = "54321"
|
||||
person._phoneNumber = "808-303-2323"
|
||||
person.favoriteWords = ['angst', 'ennui', 'existence']
|
||||
person.weight = 150
|
||||
# __xmlattributes__ = ('fabulousness',)
|
||||
person.fabulousness = "tres tres"
|
||||
xml = xmlmarshaller.marshal(person)
|
||||
print "\n#########################################"
|
||||
print "# testPerson test case #"
|
||||
print "#########################################"
|
||||
print "Person object marshalled into XML:\n"
|
||||
print xml
|
||||
# When encountering a "person" element, use the Person class
|
||||
## elementMappings = { "person" : Person }
|
||||
## obj = unmarshal(xml, elementMappings = elementMappings)
|
||||
## print "Person object recreated from XML with attribute types indicated:"
|
||||
## print obj.person.__class__
|
||||
## for (attr, value) in obj.person.__dict__.items():
|
||||
## if not attr.startswith("__"):
|
||||
## print attr, "=", value, type(value)
|
||||
## print
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
Reference in New Issue
Block a user