Skip to content

Commit

Permalink
Merge pull request #5 from plone/python3
Browse files Browse the repository at this point in the history
Prepare for Python 2 / 3 compatibility
  • Loading branch information
jensens authored Jan 28, 2018
2 parents 23a7752 + ce639bb commit f74e8fe
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 152 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ New features:

Bug fixes:

- Prepare for Python 2 / 3 compatibility
[pbauer]

- Fix test isolation issue due to incomplete teardown.
[pbauer]

Expand Down
9 changes: 4 additions & 5 deletions Products/Marshall/Extensions/Install.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
"""
$Id$
"""

from cStringIO import StringIO
# -*- coding: utf-8 -*-
from Products.CMFCore.utils import getToolByName
from Products.Marshall import registry
from Products.Marshall.config import TOOL_ID as tool_id
from six.moves import cStringIO as StringIO

add_registry = registry.manage_addRegistry


def install_tool(self, out):
tool = getToolByName(self, tool_id, None)
if tool is not None:
Expand All @@ -17,6 +15,7 @@ def install_tool(self, out):
add_registry(self)
out.write('Registry installed sucessfully.\n')


def install(self, out=None):
if out is None:
out = StringIO()
Expand Down
2 changes: 2 additions & 0 deletions Products/Marshall/Extensions/export.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
from Products.CMFCore.utils import getToolByName


def export(self):
ct = getToolByName(self, 'portal_catalog')
ut = getToolByName(self, 'portal_url')
Expand Down
21 changes: 12 additions & 9 deletions Products/Marshall/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
"""
"""

import os
import zipfile
from cStringIO import StringIO
from App.class_init import InitializeClass
from ExtensionClass import Base
from AccessControl import ClassSecurityInfo
from Acquisition import aq_base
from Products.CMFCore.permissions import ManagePortal
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from six.moves import cStringIO as StringIO

import os
import six
import zipfile


class Export(Base):
Expand All @@ -37,19 +39,19 @@ class Export(Base):
atxml_template = PageTemplateFile('www/atxml', globals(),
__name__='atxml_template')

security.declareProtected(ManagePortal, 'marshall_data')
@security.protected(ManagePortal)
def marshall_data(self, obj):
from Products.Marshall.registry import getComponent
marshaller = getComponent('primary_field')
return self.marshall(obj, marshaller)

security.declareProtected(ManagePortal, 'marshall_metadata')
@security.protected(ManagePortal)
def marshall_metadata(self, obj):
from Products.Marshall.registry import getComponent
marshaller = getComponent('atxml')
return self.marshall(obj, marshaller)

security.declareProtected(ManagePortal, 'marshall')
@security.protected(ManagePortal)
def marshall(self, obj, marshaller):
REQUEST = obj.REQUEST
RESPONSE = REQUEST.RESPONSE
Expand All @@ -61,7 +63,7 @@ def marshall(self, obj, marshaller):

content_type, length, data = ddata

if type(data) is type(''):
if isinstance(data, six.string_types):
return StringIO(data)

s = StringIO()
Expand All @@ -71,7 +73,7 @@ def marshall(self, obj, marshaller):
s.seek(0)
return s

security.declareProtected(ManagePortal, 'export')
@security.protected(ManagePortal)
def export(self, context, paths):
data = StringIO()
out = zipfile.ZipFile(data, 'w')
Expand All @@ -95,7 +97,7 @@ def export(self, context, paths):
data.seek(0)
return data

security.declareProtected(ManagePortal, 'export_info')
@security.protected(ManagePortal)
def export_info(self, context, info):
data = StringIO()
out = zipfile.ZipFile(data, 'w')
Expand Down Expand Up @@ -124,4 +126,5 @@ def export_info(self, context, info):
data.seek(0)
return data


InitializeClass(Export)
2 changes: 1 addition & 1 deletion Products/Marshall/handlers/atxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def deserialize(self, instance, ns_data, options):
for attribute in self.attributes:
try:
attribute.deserialize(instance, ns_data)
except Exception, e:
except Exception as e:
ec, e, tb = sys.exc_info()
ftb = traceback.format_tb(tb,)
msg = "failure while demarshalling schema attribute %s\n" % \
Expand Down
70 changes: 35 additions & 35 deletions Products/Marshall/namespaces/atns.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##################################################################
from plone.uuid.interfaces import IUUID

"""
Serialize AT Schema Attributes
Expand All @@ -28,22 +27,24 @@
$Id: $
"""
import re
from sets import Set

from Products.CMFCore.utils import getToolByName
from DateTime import DateTime
from plone.uuid.interfaces import IUUID
from Products.Archetypes import config as atcfg
from Products.Archetypes import public as atapi
from Products.Archetypes.debug import log
from Products.Archetypes.interfaces import IBaseUnit
from Products.Archetypes.interfaces import IObjectField
from Products.Archetypes import public as atapi
from Products.CMFCore.utils import getToolByName
from Products.Marshall import config
from Products.Marshall.handlers.atxml import XmlNamespace
from Products.Marshall.handlers.atxml import SchemaAttribute
from Products.Marshall.handlers.atxml import getRegisteredNamespaces
from Products.Marshall.exceptions import MarshallingException
from Products.Marshall import utils
from DateTime import DateTime
from Products.Marshall.exceptions import MarshallingException
from Products.Marshall.handlers.atxml import getRegisteredNamespaces
from Products.Marshall.handlers.atxml import SchemaAttribute
from Products.Marshall.handlers.atxml import XmlNamespace
from sets import Set

import re
import six
import transaction

_marker = object()
Expand Down Expand Up @@ -101,7 +102,7 @@ def serialize(self, dom, parent_node, instance, options={}):
node.setAttributeNode(name_attr)

# try to get 'utf-8' encoded string
if isinstance(value, unicode):
if isinstance(value, six.text_type):
value = value.encode('utf-8')
elif IBaseUnit.providedBy(value):
value = value.getRaw(encoding='utf-8')
Expand All @@ -111,9 +112,9 @@ def serialize(self, dom, parent_node, instance, options={}):
if self.isReference(instance):
if config.HANDLE_REFS:
ref_node = dom.createElementNS(self.namespace.xmlns,
'reference')
'reference')
uid_node = dom.createElementNS(self.namespace.xmlns,
'uid')
'uid')
value = dom.createTextNode(value)
uid_node.append(value)
ref_node.append(uid_node)
Expand Down Expand Up @@ -163,7 +164,7 @@ def processXmlValue(self, context, value):
if mimetype is not None:
data['mimetype'] = mimetype

if data.has_key('value'):
if 'value' in data:
svalues = data['value']
if not isinstance(svalues, list):
data['value'] = svalues = [svalues]
Expand All @@ -175,26 +176,26 @@ def processXmlValue(self, context, value):
def deserialize(self, instance, ns_data, options={}):
if not ns_data:
return
data = ns_data.get( self.name )
data = ns_data.get(self.name)
if data is None:
return
values = data.get('value', None)
if not values:
return

# check if we are a schema attribute
if self.isReference( instance ):
values = self.resolveReferences( instance, values)
if not config.HANDLE_REFS :
# check if we are a schema attribute
if self.isReference(instance):
values = self.resolveReferences(instance, values)
if not config.HANDLE_REFS:
return

field = instance.Schema()[self.name]
mutator = field.getMutator(instance)
if not mutator:
# read only field no mutator, but try to set value still
# since it might reflect object state (like ATCriteria)
field = instance.getField( self.name ).set( instance, values )
#raise AttributeError("No Mutator for %s"%self.name)
field = instance.getField(self.name).set(instance, values)
# raise AttributeError("No Mutator for %s"%self.name)
return
if self.name == "id":
transaction.savepoint()
Expand All @@ -218,7 +219,7 @@ def resolveReferences(self, instance, values):
ref_values.append(value)
continue
ref = value.resolve(instance)
if ref is None: #just for dup behavior
if ref is None: #just for dup behavior
raise MarshallingException(
"Could not resolve reference %r" % value)
ref_values.append(ref)
Expand Down Expand Up @@ -262,7 +263,7 @@ def deserialize(self, instance, ns_data):
def resolveUID(self, instance, values):
assert not isinstance(values, (list, tuple))
at_uid = values
#existing = getattr(instance, atcfg.UUID_ATTR, _marker)
# existing = getattr(instance, atcfg.UUID_ATTR, _marker)
existing = IUUID(instance, _marker)
if existing is _marker or existing != at_uid:
ref = Reference(uid=at_uid)
Expand All @@ -272,7 +273,7 @@ def resolveUID(self, instance, values):
"Trying to set uid of "
"%s to an already existing uid "
"clashed with %s" % (
instance.absolute_url(), target.absolute_url()))
instance.absolute_url(), target.absolute_url()))
instance._setUID(at_uid)


Expand Down Expand Up @@ -335,9 +336,8 @@ def __init__(self):

def getAttributeByName(self, schema_name, context=None):
if context is not None and schema_name not in self.at_fields:
if not schema_name in context.instance.Schema():
if schema_name not in context.instance.Schema():
return
raise AssertionError("invalid attribute %s" % (schema_name))

if schema_name in self.at_fields:
return self.at_fields[schema_name]
Expand All @@ -359,7 +359,7 @@ def getAttributes(self, instance, exclude_attrs=()):

field_keys = [k for k in instance.Schema().keys()
if k not in exclude_attrs and k not in fields]
#Set(instance.Schema().keys())-mset
# Set(instance.Schema().keys())-mset

# remove primary field if still present
# XXX: we dont want to remove the PF, but want to be backward
Expand All @@ -382,7 +382,7 @@ def serialize(self, dom, parent_node, instance, options):

for attribute in self.getAttributes(instance, exclude_attrs):
if (hasattr(attribute, 'isReference') and
attribute.isReference(instance)):
attribute.isReference(instance)):
continue
attribute.serialize(dom, parent_node, instance, options)

Expand All @@ -392,8 +392,8 @@ def deserialize(self, instance, ns_data, options):

for attribute in self.getAttributes(instance):
if (not config.HANDLE_REFS and
hasattr(attribute, 'isReference') and
attribute.isReference(instance)):
hasattr(attribute, 'isReference') and
attribute.isReference(instance)):
# simply skip it then... Gogo
continue
attribute.deserialize(instance, ns_data)
Expand Down Expand Up @@ -426,11 +426,11 @@ def processXml(self, context, data_node):
"use 'name' instead")
schema_name = data_node.attrib.get('id')
assert schema_name, "No field name specified in at:field element"
#print "field", schema_name
# print "field", schema_name
self.last_schema_id = schema_name
attribute = self.getAttributeByName(schema_name, context)
if attribute is None:
#print "na", schema_name
# print "na", schema_name
return False
data_node.set('attribute', attribute)
return True
Expand Down Expand Up @@ -470,7 +470,7 @@ def processXmlEnd(self, name, context):
if name == 'reference':
context.setNamespaceDelegate(None)
self.in_reference_mode = False
self.last_schema_id = None # guard against bad xml
self.last_schema_id = None # guard against bad xml

def getSchemaInfo(self):
return [("ArchetypesFields", "zeroOrMore", RNGSchemaFragment)]
Expand All @@ -497,7 +497,7 @@ def resolve(self, context):
if path is not None:
return context.restrictedTraverse(path, None)
catalog = getToolByName(context, 'portal_catalog')
params = [(k, v) for k, v in self.items() \
params = [(k, v) for k, v in self.items()
if k not in ('uid', 'path')]
kw = [(self.index_map.get(k), v) for k, v in params]
kw = dict(filter(lambda x: x[0] is not None and x, kw))
Expand Down
8 changes: 4 additions & 4 deletions Products/Marshall/namespaces/cmfns.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,25 +281,25 @@ def processXml(self, context, node):
data = context.getDataFor(self.namespace.xmlns)
nsprefix = node.tag[:node.tag.find('}') + 1]

#iworkflow
# iworkflow
wf_node = node.find(nsprefix + 'workflow')
wf_id = (wf_node.attrib.get(nsprefix + 'id') or
wf_node.attrib.get('id'))
#be tolerant with namespace sloppyness;)
# be tolerant with namespace sloppyness;)
assert wf_id

wf_data = data.setdefault(self.name, {})
wf_data.setdefault(wf_id, [])
wf_pstate = data.setdefault('_wf_pstate', wf_id)

#history
# history
hist_nodes = wf_node.findall(nsprefix + 'history')
wf_pstate = data['_wf_pstate']
for hist_node in hist_nodes:
record = {}
data[self.name][wf_pstate].append(record)

#var
# var
var_nodes = hist_node.findall(nsprefix + 'var')
vid = vtype = value = None

Expand Down
Loading

0 comments on commit f74e8fe

Please sign in to comment.