Skip to content

Commit

Permalink
Merge pull request #265 from plone/python3
Browse files Browse the repository at this point in the history
Python 2 / 3 compatible imports
  • Loading branch information
pbauer authored Jan 26, 2018
2 parents 9f75849 + 6452b40 commit 097bce5
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 18 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ New features:

Bug fixes:

- *add item here*
- Python 2 / 3 compatible imports.
[vincero]


2.4.7 (2017-10-17)
Expand Down
4 changes: 3 additions & 1 deletion plone/app/dexterity/behaviors/filename.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from zope.interface import implementer
from zope.interface import Interface

import six


class INameFromFileName(Interface):
"""Marker interface to enable name from filename behavior"""
Expand All @@ -20,7 +22,7 @@ def __new__(cls, context):
if info is None:
return None
filename = getattr(info.value, 'filename', None)
if not isinstance(filename, basestring) or not filename:
if not isinstance(filename, six.string_types) or not filename:
return None
instance = super(NameFromFileName, cls).__new__(cls)
instance.title = filename
Expand Down
6 changes: 4 additions & 2 deletions plone/app/dexterity/behaviors/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from zope.schema.interfaces import ISequence
from zope.schema.interfaces import IText

import six


# Behavior interfaces to display Dublin Core metadata fields on Dexterity
# content edit forms.
Expand Down Expand Up @@ -354,7 +356,7 @@ def _get_title(self):

def _set_title(self, value):
if isinstance(value, str):
raise ValueError('Title must be unicode.')
raise ValueError('Title must be six.text_type.')
self.context.title = value
title = property(_get_title, _set_title)

Expand All @@ -363,7 +365,7 @@ def _get_description(self):

def _set_description(self, value):
if isinstance(value, str):
raise ValueError('Description must be unicode.')
raise ValueError('Description must be six.text_type.')

self.context.description = value

Expand Down
5 changes: 4 additions & 1 deletion plone/app/dexterity/browser/behaviors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
from zope.lifecycleevent import modified


import six


TTW_BEHAVIOR_BLACKLIST = [
# skip deprecated behavior
'plone.app.dexterity.behaviors.related.IRelatedItems',
Expand Down Expand Up @@ -103,7 +106,7 @@ def fields(self):
if with_name and reg.name != name:
continue
fname = reg.name if reg.name else name
if isinstance(fname, unicode):
if isinstance(fname, six.text_type):
fname = fname.encode('utf8')
fields.append(
schema.Bool(
Expand Down
2 changes: 1 addition & 1 deletion plone/app/dexterity/browser/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from Products.Five.browser import BrowserView
from Products.GenericSetup.context import BaseContext
from Products.GenericSetup.context import TarballExportContext
from StringIO import StringIO
from six import StringIO
from zipfile import ZipFile

import time
Expand Down
2 changes: 1 addition & 1 deletion plone/app/dexterity/browser/import_types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
""" Support for importing Dexterity types from GS zip file.
"""
from cStringIO import StringIO
# XXX: need to make exceptions more specific, shorten messages
from DateTime.DateTime import DateTime
from lxml import etree
Expand All @@ -11,6 +10,7 @@
from Products.CMFCore.utils import getToolByName
from Products.GenericSetup.context import BaseContext
from Products.GenericSetup.interfaces import IImportContext
from six.moves import cStringIO as StringIO
from z3c.form import field
from z3c.form import form
from zipfile import BadZipfile
Expand Down
4 changes: 2 additions & 2 deletions plone/app/dexterity/browser/modeleditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __call__(self):
# Is it valid XML?
try:
root = etree.fromstring(source)
except etree.XMLSyntaxError, e:
except etree.XMLSyntaxError as e:
return json.dumps({
'success': False,
'message': 'XMLSyntaxError: {0}'.format(
Expand All @@ -70,7 +70,7 @@ def __call__(self):
# This is mainly good for catching bad dotted names.
try:
plone.supermodel.loadString(source, policy=u'dexterity')
except SupermodelParseError, e:
except SupermodelParseError as e:
message = e.args[0].replace('\n File "<unknown>"', '')
return json.dumps({
'success': False,
Expand Down
9 changes: 4 additions & 5 deletions plone/app/dexterity/browser/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from plone.z3cform.layout import FormWrapper
from Products.CMFCore.utils import getToolByName
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile as FiveViewPageTemplateFile # noqa
from six.moves import urllib
from z3c.form import button
from z3c.form import field
from zope.browserpage.viewpagetemplatefile import ViewPageTemplateFile
Expand All @@ -26,8 +27,6 @@
from zope.publisher.interfaces.browser import IBrowserPublisher
from ZPublisher.BaseRequest import DefaultPublishTraverse

import urllib


ALLOWED_FIELDS = [
u'plone.app.textfield.RichText',
Expand Down Expand Up @@ -93,7 +92,7 @@ def handleExport(self, action):
elif len(selected) > 0:
url = '{0}/@@types-export?selected={1}'.format(
self.context.context.absolute_url(),
urllib.quote(selected),
urllib.parse.quote(selected),
)
self.request.response.redirect(url)

Expand All @@ -106,7 +105,7 @@ def handleExportModels(self, action):
elif len(selected) > 0:
url = '{0}/@@models-export?selected={1}'.format(
self.context.context.absolute_url(),
urllib.quote(selected)
urllib.parse.quote(selected)
)
self.request.response.redirect(url)

Expand Down Expand Up @@ -233,7 +232,7 @@ def link(self, item, field):
if field == 'title':
return '{0}/{1}'.format(
self.context.absolute_url(),
urllib.quote(item.__name__)
urllib.parse.quote(item.__name__)
)

# Create a form wrapper so the form gets layout.
Expand Down
3 changes: 2 additions & 1 deletion plone/app/dexterity/browser/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import six


class UTF8Property(object):
Expand All @@ -13,6 +14,6 @@ def __get__(self, inst, type=None):
return value

def __set__(self, inst, value):
if isinstance(value, unicode):
if isinstance(value, six.text_type):
value = value.encode('utf8')
setattr(inst.context, self.name, value)
5 changes: 4 additions & 1 deletion plone/app/dexterity/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
from zope.security.interfaces import IPermission


import six


@implementer(IWidgetsLayer)
class MockRequest(TestRequest):
pass
Expand Down Expand Up @@ -50,7 +53,7 @@ def _validate_vocabulary_name(self, schema, field, vocabulary_name):
widgets = mergedTaggedValueDict(schema, WIDGETS_KEY)
widget = widgets.get(field.getName())
if widget:
if isinstance(widget, basestring):
if isinstance(widget, six.string_types):
widget = resolveDottedName(widget)
if widget:
widget = widget(field, self._request)
Expand Down
4 changes: 2 additions & 2 deletions plone/app/dexterity/tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from xml.parsers.expat import ExpatError
from zope.component import getMultiAdapter

import StringIO
import six
import unittest
import zipfile

Expand Down Expand Up @@ -34,7 +34,7 @@ def test_exported_XML_valid_for_GS(self):
(dexterity_control_panel, self.request), name='types-export')

# export the 'item' type and try to parse all XMLs
fd = StringIO.StringIO(types_export_view.__call__())
fd = six.StringIO(types_export_view.__call__())
archive = zipfile.ZipFile(fd, mode='r')
filenames = archive.namelist()
for filename in filenames:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
'Products.CMFPlone>=4.0b1',
'Products.GenericSetup',
'setuptools',
'six',
'Zope2',
'zope.browserpage',
'zope.interface',
Expand Down

0 comments on commit 097bce5

Please sign in to comment.