appy.shared: allow to marshall Plone/Zope LazyMap instances.
This commit is contained in:
parent
0b6d2d2165
commit
57c481f05e
|
@ -469,6 +469,28 @@ class XmlMarshaller:
|
||||||
if prefix: return '%s:%s' % (prefix, name)
|
if prefix: return '%s:%s' % (prefix, name)
|
||||||
return name
|
return name
|
||||||
|
|
||||||
|
def isAnObject(self, instance):
|
||||||
|
'''Returns True if p_instance is a class instance, False if it is a
|
||||||
|
basic type, or tuple, sequence, etc.'''
|
||||||
|
if instance.__class__.__name__ == 'LazyMap': return False
|
||||||
|
iType = type(instance)
|
||||||
|
if iType == types.InstanceType:
|
||||||
|
return True
|
||||||
|
elif iType.__name__ == 'ImplicitAcquirerWrapper':
|
||||||
|
# This is the case with Archetype instances
|
||||||
|
return True
|
||||||
|
elif iType.__class__.__name__ == 'ExtensionClass':
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def isAList(self, value):
|
||||||
|
'''Is p_value a list?'''
|
||||||
|
return value.__class__.__name__ in ('list', 'PersistentList', 'LazyMap')
|
||||||
|
|
||||||
|
def isADict(self, value):
|
||||||
|
'''Is p_value a dict?'''
|
||||||
|
return value.__class__.__name__ in ('dict', 'PersistentMapping')
|
||||||
|
|
||||||
def dumpRootTag(self, res, instance):
|
def dumpRootTag(self, res, instance):
|
||||||
'''Dumps the root tag.'''
|
'''Dumps the root tag.'''
|
||||||
# Dumps the name of the tag.
|
# Dumps the name of the tag.
|
||||||
|
@ -554,7 +576,7 @@ class XmlMarshaller:
|
||||||
self.dumpField(res, 'url', elem.absolute_url())
|
self.dumpField(res, 'url', elem.absolute_url())
|
||||||
else:
|
else:
|
||||||
self.dumpField(res, 'url', value.absolute_url())
|
self.dumpField(res, 'url', value.absolute_url())
|
||||||
elif type(value) in sequenceTypes:
|
elif fieldType in ('list', 'tuple'):
|
||||||
# The previous condition must be checked before this one because
|
# The previous condition must be checked before this one because
|
||||||
# referred objects may be stored in lists or tuples, too.
|
# referred objects may be stored in lists or tuples, too.
|
||||||
for elem in value: self.dumpField(res, 'e', elem)
|
for elem in value: self.dumpField(res, 'e', elem)
|
||||||
|
@ -595,15 +617,14 @@ class XmlMarshaller:
|
||||||
elif isinstance(fieldValue, float): fType = 'float'
|
elif isinstance(fieldValue, float): fType = 'float'
|
||||||
elif isinstance(fieldValue, long): fType = 'long'
|
elif isinstance(fieldValue, long): fType = 'long'
|
||||||
elif isinstance(fieldValue, tuple): fType = 'tuple'
|
elif isinstance(fieldValue, tuple): fType = 'tuple'
|
||||||
elif isinstance(fieldValue, list): fType = 'list'
|
elif self.isAList(fieldValue): fType = 'list'
|
||||||
elif isinstance(fieldValue, dict) or \
|
elif self.isADict(fieldValue): fType = 'dict'
|
||||||
fieldValue.__class__.__name__ == 'PersistentMapping':fType = 'dict'
|
|
||||||
elif fieldValue.__class__.__name__ == 'DateTime': fType = 'DateTime'
|
elif fieldValue.__class__.__name__ == 'DateTime': fType = 'DateTime'
|
||||||
elif self.isAnObject(fieldValue): fType = 'object'
|
elif self.isAnObject(fieldValue): fType = 'object'
|
||||||
if self.objectType != 'popo':
|
if self.objectType != 'popo':
|
||||||
if fType: res.write(' type="%s"' % fType)
|
if fType: res.write(' type="%s"' % fType)
|
||||||
# Dump other attributes if needed
|
# Dump other attributes if needed
|
||||||
if type(fieldValue) in sequenceTypes:
|
if fType in ('list', 'tuple'):
|
||||||
res.write(' count="%d"' % len(fieldValue))
|
res.write(' count="%d"' % len(fieldValue))
|
||||||
if fType == 'file':
|
if fType == 'file':
|
||||||
if hasattr(fieldValue, 'content_type'):
|
if hasattr(fieldValue, 'content_type'):
|
||||||
|
@ -617,20 +638,6 @@ class XmlMarshaller:
|
||||||
self.dumpValue(res, fieldValue, fType, isRef=(fieldType=='ref'))
|
self.dumpValue(res, fieldValue, fType, isRef=(fieldType=='ref'))
|
||||||
res.write('</'); res.write(fieldTag); res.write('>')
|
res.write('</'); res.write(fieldTag); res.write('>')
|
||||||
|
|
||||||
def isAnObject(self, instance):
|
|
||||||
'''Returns True if p_instance is a class instance, False if it is a
|
|
||||||
basic type, or tuple, sequence, etc.'''
|
|
||||||
iType = type(instance)
|
|
||||||
if iType == types.InstanceType:
|
|
||||||
return True
|
|
||||||
elif iType.__name__ == 'ImplicitAcquirerWrapper':
|
|
||||||
# This is the case with Archetype instances
|
|
||||||
return True
|
|
||||||
elif iType.__class__.__name__ == 'ExtensionClass':
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
def marshall(self, instance, objectType='popo', conversionFunctions={}):
|
def marshall(self, instance, objectType='popo', conversionFunctions={}):
|
||||||
'''Returns in a UnicodeBuffer the XML version of p_instance. If
|
'''Returns in a UnicodeBuffer the XML version of p_instance. If
|
||||||
p_instance corresponds to a Plain Old Python Object, specify 'popo'
|
p_instance corresponds to a Plain Old Python Object, specify 'popo'
|
||||||
|
|
Loading…
Reference in a new issue