Skip to content

Commit

Permalink
Add Python 2 / 3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
davilima6 committed Jan 26, 2018
1 parent 8b74466 commit b1746ce
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Breaking changes:

New features:

- *add item here*
- Add Python 2 / 3 compatibility [davilima6]

Bug fixes:

Expand Down
17 changes: 10 additions & 7 deletions Products/CMFPlacefulWorkflow/DefaultWorkflowPolicy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@
from AccessControl.requestmethod import postonly
from Acquisition import aq_base
from App.class_init import InitializeClass
from os.path import join as path_join
from Persistence import PersistentMapping
from Products.CMFCore.utils import SimpleItemWithProperties
from Products.CMFCore.utils import getToolByName
from Products.CMFPlacefulWorkflow.PlacefulWorkflowTool import addWorkflowPolicyFactory
from Products.CMFCore.utils import SimpleItemWithProperties
from Products.CMFPlacefulWorkflow.global_symbols import Log
from Products.CMFPlacefulWorkflow.interfaces import IWorkflowPolicyDefinition
from Products.CMFPlacefulWorkflow.permissions import ManageWorkflowPolicies
from Products.CMFPlacefulWorkflow.PlacefulWorkflowTool import addWorkflowPolicyFactory
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from os.path import join as path_join
from zope.interface import implementer

import six


DEFAULT_CHAIN = '(Default)'
_MARKER = '_MARKER'

Expand Down Expand Up @@ -197,7 +200,7 @@ def getChainFor(self, ob, managescreen=False):
"""

cbt = self._chains_by_type
if isinstance(ob, basestring):
if isinstance(ob, six.string_types):
pt = ob
elif hasattr(aq_base(ob), '_getPortalTypeName'):
pt = ob._getPortalTypeName()
Expand All @@ -212,7 +215,7 @@ def getChainFor(self, ob, managescreen=False):
chain = cbt.get(pt, _MARKER)

# Backwards compatibility: before chain was a string, not a list
if chain is not _MARKER and isinstance(chain, basestring):
if chain is not _MARKER and isinstance(chain, six.string_types):
chain = map(lambda x: x.strip(), chain.split(','))

Log.debug('Chain founded in policy %s', chain)
Expand All @@ -236,7 +239,7 @@ def setDefaultChain(self, default_chain, REQUEST=None):
""" Sets the default chain for this tool. """
wftool = getToolByName(self, 'portal_workflow')

if isinstance(default_chain, basestring):
if isinstance(default_chain, six.string_types):
default_chain = map(lambda x: x.strip(), default_chain.split(','))
ids = []
for wf_id in default_chain:
Expand Down Expand Up @@ -276,7 +279,7 @@ def setChain(self, portal_type, chain, REQUEST=None):
if portal_type not in [pt.id for pt in self._listTypeInfo()]:
raise ValueError("'%s' is not a valid portal type." % portal_type)

if isinstance(chain, basestring):
if isinstance(chain, six.string_types):
chain = map(lambda x: x.strip(), chain.split(','))

wftool = getToolByName(self, 'portal_workflow')
Expand Down
24 changes: 14 additions & 10 deletions Products/CMFPlacefulWorkflow/WorkflowPolicyConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@
Workflow Policy config
"""

from os.path import join as path_join

from AccessControl import ClassSecurityInfo
from Acquisition import aq_base
from Acquisition import aq_inner
from Acquisition import aq_parent
from App.class_init import InitializeClass
from OFS.SimpleItem import SimpleItem
from AccessControl import ClassSecurityInfo
from Acquisition import aq_base, aq_inner, aq_parent

from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from Products.CMFCore.utils import getToolByName
from os.path import join as path_join

from PlacefulWorkflowTool import WorkflowPolicyConfig_id
from Products.CMFCore.utils import getToolByName
from Products.CMFPlacefulWorkflow.global_symbols import Log
from Products.CMFPlacefulWorkflow.permissions import ManageWorkflowPolicies
from Products.PageTemplates.PageTemplateFile import PageTemplateFile

import six


manage_addWorkflowPolicyConfigForm = PageTemplateFile(
path_join('www', 'add_workflow_policy_config_form'), globals())
Expand Down Expand Up @@ -107,8 +110,8 @@ def getPolicyBelow(self):
security.declareProtected(ManageWorkflowPolicies, 'setPolicyIn')

def setPolicyIn(self, policy, update_security=False):
if not isinstance(policy, basestring):
raise ValueError("Policy must be a basestring")
if not isinstance(policy, six.string_types):
raise ValueError("Policy must be a six.string_types")
self.workflow_policy_in = policy
if update_security:
wtool = getToolByName(self, 'portal_workflow')
Expand All @@ -129,7 +132,7 @@ def setPolicyIn(self, policy, update_security=False):
security.declareProtected(ManageWorkflowPolicies, 'setPolicyBelow')

def setPolicyBelow(self, policy, update_security=False):
if not isinstance(policy, basestring):
if not isinstance(policy, six.string_types):
raise ValueError("Policy must be a string")
self.workflow_policy_below = policy
if update_security:
Expand Down Expand Up @@ -178,4 +181,5 @@ def getPlacefulChainFor(self, portal_type, start_here=False):

return chain


InitializeClass(WorkflowPolicyConfig)
8 changes: 5 additions & 3 deletions Products/CMFPlacefulWorkflow/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
from Acquisition import aq_inner
from Acquisition import aq_parent
from Products.CMFCore.utils import getToolByName
from Products.CMFPlacefulWorkflow.PlacefulWorkflowTool import WorkflowPolicyConfig_id
from Products.CMFPlacefulWorkflow.interfaces import IPlacefulMarker
from Products.CMFPlacefulWorkflow.PlacefulWorkflowTool import WorkflowPolicyConfig_id
from Products.CMFPlone.interfaces import IWorkflowChain
from Products.CMFPlone.utils import base_hasattr
from Products.CMFPlone.workflow import ToolWorkflowChain
from zope.component import adapter
from zope.interface import Interface
from zope.interface import implementer
from zope.interface import Interface

import six


@adapter(Interface, IPlacefulMarker)
Expand All @@ -43,7 +45,7 @@ def PlacefulWorkflowChain(ob, tool):
4. if the parent is the portal object or None we stop and ask
portal_workflow
"""
if isinstance(ob, basestring):
if isinstance(ob, six.string_types):
# We are not in an object, then we can only get default from
# portal_workflow
return ToolWorkflowChain(ob, tool)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
),
install_requires=[
'setuptools',
'six',
'zope.component',
'zope.interface',
'zope.i18nmessageid',
Expand Down

2 comments on commit b1746ce

@pbauer
Copy link
Member

@pbauer pbauer commented on b1746ce Jan 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davilima6 this package also has doctests that need to be converted:

$ sixer all Products/CMFPlacefulWorkflow/tests/*.txt

Patch Products/CMFPlacefulWorkflow/tests/exportimport.txt with print
WARNING: [stringio] Products/CMFPlacefulWorkflow/tests/exportimport.txt: ...     fileobj=StringIO.StringIO(tarball))
Patch Products/CMFPlacefulWorkflow/tests/policy_form.txt with print

Scanned 2 files
Applied operations (1): print

Warnings:
WARNING: [stringio] Products/CMFPlacefulWorkflow/tests/exportimport.txt: ...     fileobj=StringIO.StringIO(tarball))

@davilima6
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pbauer, done in #18

Please sign in to comment.