Skip to content

Commit

Permalink
Merge pull request #35 from plone/python3
Browse files Browse the repository at this point in the history
Python 3
  • Loading branch information
jensens authored Aug 3, 2018
2 parents 7333f46 + 9dcd6fe commit ae7d3f3
Show file tree
Hide file tree
Showing 31 changed files with 358 additions and 446 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*
- fix test for python 3
[petschki]


3.1.3 (2018-06-21)
Expand Down
2 changes: 1 addition & 1 deletion Products/PortalTransforms/Transform.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from AccessControl import ClassSecurityInfo
from App.class_init import InitializeClass
from AccessControl.class_init import InitializeClass
from logging import ERROR
from OFS.SimpleItem import SimpleItem
from Persistence import PersistentMapping
Expand Down
20 changes: 11 additions & 9 deletions Products/PortalTransforms/TransformEngine.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
# -*- coding: utf-8 -*-
import six

from AccessControl import ClassSecurityInfo
from AccessControl.class_init import InitializeClass
from Acquisition import aq_base
from App.class_init import InitializeClass
from logging import DEBUG
from OFS.Folder import Folder
from Persistence import PersistentMapping
from persistent.list import PersistentList
from Products.CMFCore.ActionProviderBase import ActionProviderBase
from Products.CMFCore.permissions import ManagePortal
from Products.CMFCore.permissions import View
from Products.CMFCore.utils import UniqueObject
from Products.CMFCore.utils import getToolByName
from Products.CMFCore.utils import registerToolInterface
from Products.CMFCore.utils import UniqueObject
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from Products.PortalTransforms.Transform import Transform
from Products.PortalTransforms.cache import Cache
from Products.PortalTransforms.chain import chain
from Products.PortalTransforms.chain import TransformsChain
from Products.PortalTransforms.chain import chain
from Products.PortalTransforms.data import datastream
from Products.PortalTransforms.interfaces import IDataStream
from Products.PortalTransforms.interfaces import IEngine
from Products.PortalTransforms.interfaces import IPortalTransformsTool
from Products.PortalTransforms.interfaces import ITransform
from Products.PortalTransforms.libtransforms.utils import MissingBinary
from Products.PortalTransforms.Transform import Transform
from Products.PortalTransforms.transforms import initialize
from Products.PortalTransforms.utils import TransformException
from Products.PortalTransforms.utils import _www
from Products.PortalTransforms.utils import log
from Products.PortalTransforms.utils import TransformException
from logging import DEBUG
from persistent.list import PersistentList
from zope.interface import implementer


Expand Down Expand Up @@ -391,7 +393,7 @@ def typesWithPathOfLength(length):
if outputs:
for reachedType, transforms in outputs.items():
# Does this lead to a type we never reached before ?
if reachedType not in pathToType.keys() and transforms:
if reachedType not in six.iterkeys(pathToType) and transforms: # noqa
# Yes, we did not know any path reaching this type
# Let's remember the path to here
pathToType[reachedType] = (
Expand Down Expand Up @@ -553,7 +555,7 @@ def listPolicies(self):
# XXXFIXME: backward compat, should be removed latter
if not hasattr(self, '_policies'):
self._policies = PersistentMapping()
return self._policies.items()
return list(self._policies.items())

# mimetype oriented conversions (iengine interface)

Expand Down
2 changes: 1 addition & 1 deletion Products/PortalTransforms/chain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from AccessControl import ClassSecurityInfo
from Acquisition import Implicit
from App.class_init import InitializeClass
from AccessControl.class_init import InitializeClass
from OFS.role import RoleManager
from OFS.SimpleItem import Item
from Persistence import Persistent
Expand Down
47 changes: 14 additions & 33 deletions Products/PortalTransforms/libtransforms/commandtransform.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# -*- coding: utf-8 -*-
from os.path import basename
from os.path import join
from Products.PortalTransforms.interfaces import ITransform
from Products.PortalTransforms.libtransforms.utils import bin_search
from Products.PortalTransforms.libtransforms.utils import getShortPathName
from os.path import basename
from os.path import join
from zope.interface import implementer

import os
import re
import shutil
import six
import subprocess
import tempfile


Expand Down Expand Up @@ -67,10 +68,8 @@ class popentransform(object):

binaryName = ""
binaryArgs = ""
useStdin = True

def __init__(self, name=None, binary=None, binaryArgs=None, useStdin=None,
**kwargs):
def __init__(self, name=None, binary=None, binaryArgs=None, **kwargs):
if name is not None:
self.__name__ = name
if binary is not None:
Expand All @@ -79,42 +78,24 @@ def __init__(self, name=None, binary=None, binaryArgs=None, useStdin=None,
self.binary = bin_search(self.binaryName)
if binaryArgs is not None:
self.binaryArgs = binaryArgs
if useStdin is not None:
self.useStdin = useStdin

def name(self):
return self.__name__

def getData(self, couterr):
return couterr.read()

def convert(self, data, cache, **kwargs):
command = "%s %s" % (self.binary, self.binaryArgs)
tmpname = None
try:
if not self.useStdin:
# create tmp
tmpfile, tmpname = tempfile.mkstemp(text=False)
# write data to tmp using a file descriptor
os.write(tmpfile, data)
# close it so the other process can read it
os.close(tmpfile)
# apply tmp name to command
command = command % {'infile': tmpname}

if six.PY2:
cin, couterr = os.popen4(command, 'b')

if self.useStdin:
cin.write(data)

cin.write(data)
cin.close()

out = self.getData(couterr)
out = couterr.read()
couterr.close()
else:
process = subprocess.run(
command, shell=True, input=data, stdout=subprocess.PIPE)
out = process.stdout

cache.setData(out)
return cache
finally:
if not self.useStdin and tmpname is not None:
# remove tmp file
os.unlink(tmpname)
cache.setData(out)
return cache
12 changes: 5 additions & 7 deletions Products/PortalTransforms/libtransforms/piltransform.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# -*- coding: utf-8 -*-
from Products.PortalTransforms.interfaces import ITransform
from zope.interface import implementer

import PIL.Image


from six import StringIO
from Products.PortalTransforms.interfaces import ITransform
from six import BytesIO
from zope.interface import implementer


@implementer(ITransform)
Expand All @@ -20,8 +18,8 @@ def name(self):
return self.__name__

def convert(self, orig, data, **kwargs):
imgio = StringIO()
orig = StringIO(orig)
imgio = BytesIO()
orig = BytesIO(orig)
newwidth = kwargs.get('width', None)
newheight = kwargs.get('height', None)
pil_img = PIL.Image.open(orig)
Expand Down
2 changes: 2 additions & 0 deletions Products/PortalTransforms/libtransforms/retransform.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from Products.CMFPlone.utils import safe_unicode
from Products.PortalTransforms.interfaces import ITransform
from zope.interface import implementer

Expand All @@ -25,6 +26,7 @@ def addRegex(self, pat, repl):
self.regexes.append((r, repl))

def convert(self, orig, data, **kwargs):
orig = safe_unicode(orig)
for r, repl in self.regexes:
orig = r.sub(repl, orig)
data.setData(orig)
Expand Down
3 changes: 3 additions & 0 deletions Products/PortalTransforms/libtransforms/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from Products.CMFPlone.utils import safe_unicode
from Products.PortalTransforms.utils import log

import os
Expand Down Expand Up @@ -76,7 +77,9 @@ def bodyfinder(text):
""" Return body or unchanged text if no body tags found.
Always use html_headcheck() first.
Accepts bytes or text. Returns text.
"""
text = safe_unicode(text)
lowertext = text.lower()
bodystart = lowertext.find('<body')
if bodystart == -1:
Expand Down
1 change: 0 additions & 1 deletion Products/PortalTransforms/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
# -*- coding: utf-8 -*-
22 changes: 22 additions & 0 deletions Products/PortalTransforms/tests/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
import unittest

from Products.CMFPlone.utils import safe_unicode
from Products.PortalTransforms.testing import PRODUCTS_PORTALTRANSFORMS_INTEGRATION_TESTING # noqa


class TransformTestCase(unittest.TestCase):

layer = PRODUCTS_PORTALTRANSFORMS_INTEGRATION_TESTING

def setUp(self):
self.portal = self.layer['portal']
self.transforms = self.portal.portal_transforms

def _baseAssertEqual(self, first, second, msg=None):
return unittest.TestCase._baseAssertEqual(
self, safe_unicode(first), safe_unicode(second), msg)

def assertMultiLineEqual(self, first, second, msg=None):
return unittest.TestCase.assertMultiLineEqual(
self, safe_unicode(first), safe_unicode(second), msg)
2 changes: 1 addition & 1 deletion Products/PortalTransforms/tests/output/demo1.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

14 changes: 6 additions & 8 deletions Products/PortalTransforms/tests/test_engine.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# -*- coding: utf-8 -*-
import re

from Products.CMFPlone.utils import _createObjectByType
from Products.PortalTransforms.chain import chain
from Products.PortalTransforms.interfaces import ITransform
from Products.PortalTransforms.testing import PRODUCTS_PORTALTRANSFORMS_INTEGRATION_TESTING
from Products.PortalTransforms.tests.base import TransformTestCase
from Products.PortalTransforms.utils import TransformException
from six.moves import urllib
from zope.interface import implementer
import re
import unittest


class BaseTransform:
Expand Down Expand Up @@ -114,17 +114,15 @@ class BadTransformWildcardOutput(BaseTransform):
output = 'text/*'


class TestEngine(unittest.TestCase):

layer = PRODUCTS_PORTALTRANSFORMS_INTEGRATION_TESTING
class TestEngine(TransformTestCase):

def setUp(self):
self.portal = self.layer['portal']
super(TestEngine, self).setUp()
_createObjectByType('Folder', self.portal, id='folder')
self.folder = self.portal.folder
self.engine = self.portal.portal_transforms
self.data = '<b>foo</b>'
for mt in self.engine._policies.keys():
for mt in list(self.engine._policies.keys()):
self.engine.manage_delPolicies([mt])

def register(self):
Expand Down
34 changes: 14 additions & 20 deletions Products/PortalTransforms/tests/test_graph.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
# -*- coding: utf-8 -*-
from Products.PortalTransforms.testing import PRODUCTS_PORTALTRANSFORMS_INTEGRATION_TESTING
from .utils import input_file_path
import unittest
from Products.PortalTransforms.tests.base import TransformTestCase
from Products.PortalTransforms.tests.utils import input_file_path

FILE_PATH = input_file_path("demo1.pdf")


class TestGraph(unittest.TestCase):

layer = PRODUCTS_PORTALTRANSFORMS_INTEGRATION_TESTING

def setUp(self):
self.portal = self.layer['portal']
self.engine = self.portal.portal_transforms
class TestGraph(TransformTestCase):

def testGraph(self):
data = open(FILE_PATH, 'r').read()
requirements = self.engine._policies.get('text/plain', [])
with open(FILE_PATH, 'rb') as fd:
data = fd.read()
requirements = self.transforms._policies.get('text/plain', [])
if requirements:
out = self.engine.convertTo('text/plain', data, filename=FILE_PATH)
out = self.transforms.convertTo('text/plain', data, filename=FILE_PATH)
self.assertTrue(out.getData())

def testFindPath(self):
originalMap = self.engine._mtmap
originalMap = self.transforms._mtmap
"""
The dummy map used for this test corresponds to a graph
depicted in ASCII art below :
Expand Down Expand Up @@ -78,7 +72,7 @@ def name(self):
'4-2': ['transform4-5', 'transform5-3', 'transform3-2'],
'5-3': ['transform5-3'],
}
self.engine._mtmap = dummyMap1
self.transforms._mtmap = dummyMap1
for orig in ['1', '2', '3', '4', '5', '6', '7']:
for target in ['1', '2', '3', '4', '5', '6', '7']:
# build the name of the path
Expand All @@ -88,24 +82,24 @@ def name(self):
# we do. Here is the expected shortest path
expectedPath = expectedPathes[pathName]
# what's the shortest path according to the engine ?
gotPath = self.engine._findPath(orig, target)
gotPath = self.transforms._findPath(orig, target)
# just keep the name of the transforms, please
if gotPath is not None:
gotPath = [transform.name() for transform in gotPath]
# this must be the same as in our expectation
self.assertEqual(expectedPath, gotPath)
self.engine._mtmap = originalMap
self.transforms._mtmap = originalMap

def testFindPathWithEmptyTransform(self):
""" _findPath should not throw "index out of range" when dealing with
empty transforms list
"""
dummyMap = {'1': {'2': []}}
self.engine._mtmap = dummyMap
self.engine._findPath('1', '2')
self.transforms._mtmap = dummyMap
self.transforms._findPath('1', '2')

def testIdentity(self):
orig = 'Some text'
converted = self.engine.convertTo(
converted = self.transforms.convertTo(
'text/plain', 'Some text', mimetype='text/plain')
self.assertEqual(orig, str(converted))
12 changes: 1 addition & 11 deletions Products/PortalTransforms/tests/test_intelligenttext.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
# -*- coding: utf-8 -*-
from Products.PortalTransforms.testing import PRODUCTS_PORTALTRANSFORMS_INTEGRATION_TESTING
import unittest


class TransformTestCase(unittest.TestCase):

layer = PRODUCTS_PORTALTRANSFORMS_INTEGRATION_TESTING

def setUp(self):
self.portal = self.layer['portal']
self.transforms = self.portal.portal_transforms
from Products.PortalTransforms.tests.base import TransformTestCase


class TestIntelligentTextToHtml(TransformTestCase):
Expand Down
Loading

0 comments on commit ae7d3f3

Please sign in to comment.