Skip to content

Commit

Permalink
add support for py3
Browse files Browse the repository at this point in the history
  • Loading branch information
pbauer committed Aug 20, 2018
1 parent 5868d3e commit d045998
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ CHANGES
2.0 (unreleased)
----------------

- Add support for Python 3
[pbauer]

- Drop support for Zope versions older than 4.0

- Fix test to work with Zope 4, where ``makeClassForTemplate`` was removed and replaced with ``SimpleViewClass``.
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.6',
'Topic :: Internet :: WWW/HTTP :: Site Management',
],
)
23 changes: 13 additions & 10 deletions src/five/customerize/browser.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
from os.path import sep, isabs, split, basename
# -*- coding: utf-8 -*-
from Acquisition import aq_inner

from Products.Five.component.interfaces import IObjectManagerSite
from Products.Five.component import findSite
from Products.Five.browser import BrowserView

from zope.interface import providedBy, Interface
from Products.Five.component import findSite
from Products.Five.component.interfaces import IObjectManagerSite
from five.customerize.interfaces import IViewTemplateContainer
from five.customerize.zpt import TTWViewTemplate
from os.path import sep, isabs, split, basename
from zope.component import getGlobalSiteManager
from zope.component import getMultiAdapter, getSiteManager
from zope.component import getUtility, queryUtility
from zope.dottedname.resolve import resolve
from zope.interface import providedBy, Interface
from zope.interface.interfaces import IInterface
from zope.schema.interfaces import IVocabularyFactory
from zope.publisher.interfaces import IRequest
from zope.publisher.interfaces.browser import IBrowserRequest
from zope.schema.interfaces import IVocabularyFactory
from zope.traversing.browser import absoluteURL

from five.customerize.zpt import TTWViewTemplate
from five.customerize.interfaces import IViewTemplateContainer
import six


# This method was copied from zope.app.apidoc.presentation
Expand Down Expand Up @@ -128,7 +128,10 @@ def templateCodeFromViewName(self, viewname):
template = self.templateFromViewName(viewname)
#XXX: we can't do template.read() here because of a bug in
# Zope 3's ZPT implementation.
return open(template.filename, 'rb').read()
data = open(template.filename, 'rb').read()
if six.PY2:
return data
return data.decode('utf-8')

def permissionFromViewName(self, viewname):
view = getMultiAdapter((self.context, self.request), name=viewname)
Expand Down
10 changes: 5 additions & 5 deletions src/five/customerize/browser.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Let's see if we can view it:

>>> browser.handleErrors = False
>>> browser.open('http://localhost/folder/myttwtemplate.html')
>>> print browser.contents
>>> print(browser.contents)
hello

Now we edit our view template TTW:
Expand All @@ -71,7 +71,7 @@ Now we edit our view template TTW:
... <span tal:replace="python:repr(view)"/>'''
>>> browser.getControl('Save Changes').click()
>>> browser.open('http://localhost/folder/myttwtemplate.html?foo=bar')
>>> print browser.contents
>>> print(browser.contents)
folder
bar
None
Expand All @@ -92,7 +92,7 @@ Make and register a view that we can customize with a TTWViewTemplate:
>>> provideAdapter(TestView, (IObjectManager, IDefaultBrowserLayer),
... Interface, name='mystaticview.html')
>>> browser.open('http://localhost/folder/mystaticview.html')
>>> print browser.contents
>>> print(browser.contents)
Original View

Pass that view to the constructor for a new TTWViewTemplate, and register
Expand All @@ -108,7 +108,7 @@ it locally to override the static view:
Now we browse the view to ensure that is has changed:

>>> browser.open('http://localhost/folder/mystaticview.html')
>>> print browser.contents
>>> print(browser.contents)
Not so static

Edit the template to make it dynamic and see if we have access to the
Expand All @@ -120,7 +120,7 @@ view methods:
... <span tal:replace="view/foo_method"/>'''
>>> browser.getControl('Save Changes').click()
>>> browser.open('http://localhost/folder/mystaticview.html')
>>> print browser.contents
>>> print(browser.contents)
Customized
baz

Expand Down
23 changes: 11 additions & 12 deletions src/five/customerize/customerize.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Now the site provides ``IObjectManagerSite``:

And it has a site manager (component registry):

>>> site.getSiteManager() #doctest: +ELLIPSIS
>>> site.getSiteManager() # doctest: +ELLIPSIS
<PersistentComponents ...>


Expand Down Expand Up @@ -107,7 +107,7 @@ the source of its template:
>>> os.path.basename(template.filename)
'customize.pt'

>>> print view.templateCodeFromViewName(u'customizezpt.html') #doctest: +ELLIPSIS
>>> print(view.templateCodeFromViewName(u'customizezpt.html')) # doctest: +ELLIPSIS
<html metal:use-macro="context/@@standard_macros/view"
i18n:domain="zope">
...
Expand All @@ -124,7 +124,7 @@ That actually creates a TTWViewTemplate object in the nearest site
above us should be targeted)

>>> zpt = getattr(site, 'customize.pt')
>>> print zpt.read() #doctest: +ELLIPSIS
>>> print(zpt.read()) # doctest: +ELLIPSIS
<html metal:use-macro="context/@@standard_macros/view"
i18n:domain="zope">
...
Expand Down Expand Up @@ -158,25 +158,24 @@ so calling it will fail unless logged in:

>>> view = zope.component.getMultiAdapter((item, request),
... name=u"customizezpt.html")
>>> print view()
>>> print(view())
Traceback (most recent call last):
...
Unauthorized: The current user does not have the required "Manage Five local sites" permission
AccessControl.unauthorized.Unauthorized: The current user does not have the required "Manage Five local sites" permission

Now look it up as manager and compare its output:

>>> self.setRoles(['Manager',])
>>> view = zope.component.getMultiAdapter((item, request),
... name=u"customizezpt.html")
>>> print view() #doctest: +ELLIPSIS
>>> print(view()) # doctest: +ELLIPSIS
context: <SimpleContent at item>
template: <TTWViewTemplate at customize.pt>
request: <HTTPRequest, ...>
view: <five.customerize.zpt.TTWView ...>
view: <five.customerize.zpt.TTWView...>
modules: <Products.PageTemplates.ZRPythonExpr._SecureModuleImporter object at ...>
options: {'args': ()}
nothing:
<BLANKLINE>
nothing:...

5. Deleting view templates
--------------------------
Expand All @@ -191,7 +190,7 @@ Now the view look-up is back to the old way:

>>> view = zope.component.getMultiAdapter((item, request),
... name=u"customizezpt.html")
>>> print open(view.index.filename, 'rb').read() #doctest: +ELLIPSIS
>>> print(open(view.index.filename, 'rb').read().decode('utf8')) #doctest: +ELLIPSIS
<html metal:use-macro="context/@@standard_macros/view"
i18n:domain="zope">
...
Expand Down Expand Up @@ -256,7 +255,7 @@ Now we retrieve the view and make sure it does what it should:

>>> view = zope.component.getMultiAdapter((item, request),
... name=u"simpleview.html")
>>> print view()
>>> print(view())
<html>
...
A simple view template with a class
Expand All @@ -277,7 +276,7 @@ And render it again:

>>> view = zope.component.getMultiAdapter((item, request),
... name=u"simpleview.html")
>>> print view()
>>> print(view())
A customized view
baz

Expand Down
20 changes: 18 additions & 2 deletions src/five/customerize/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
from zope.component.hooks import setHooks
from Zope2.App.zcml import load_config

import doctest
import re
import six


class Py23DocChecker(doctest.OutputChecker):
def check_output(self, want, got, optionflags):
if six.PY2:
got = re.sub('Unauthorized', 'AccessControl.unauthorized.Unauthorized', got)
got = re.sub("u'(.*?)'", "'\\1'", got)
return doctest.OutputChecker.check_output(self, want, got, optionflags)


def setUp(test):
testing.setUp(test)
Expand All @@ -27,8 +39,12 @@ def test_suite():
return TestSuite([
ZopeDocFileSuite('zpt.txt', package="five.customerize",
setUp=setUp, tearDown=testing.tearDown),
ZopeDocFileSuite('customerize.txt', package="five.customerize",
setUp=setUp),
ZopeDocFileSuite(
'customerize.txt',
package="five.customerize",
setUp=setUp,
checker=Py23DocChecker(),
),
FunctionalDocFileSuite('browser.txt', package="five.customerize",
setUp=setUp)
])
Expand Down
4 changes: 2 additions & 2 deletions src/five/customerize/zpt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ renderer by calling as if it were a view factory:
>>> template = TTWViewTemplate('test_template', '<html></html>')
>>> template = template.__of__(app)
>>> renderer = template(self.folder, None)
>>> print renderer()
>>> print(renderer())
<html></html>

We now add some more complex TAL expressions to our template, and
Expand All @@ -22,7 +22,7 @@ ensure that it obtains the passed in request and context for rendering:
>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest(environ={'foo': 'bar'})
>>> renderer = template(self.folder, request)
>>> print renderer()
>>> print(renderer())
test_folder_1_
bar
None

0 comments on commit d045998

Please sign in to comment.