Skip to content

Commit

Permalink
Apply black formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgross committed Aug 28, 2021
1 parent 2f69bb1 commit 4fa3e06
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 172 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ Changelog
----------------

- Python 3 / Plone 5.2 compatibility
[adrianschulz, tomgross]

- Update dependencies
- Move to Github CI
- Apply black formatting
[tomgross]


1.4.1 (2018-10-22)
Expand Down
2 changes: 1 addition & 1 deletion src/collective/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# -*- coding: utf-8 -*-
__import__('pkg_resources').declare_namespace(__name__)
__import__("pkg_resources").declare_namespace(__name__)
2 changes: 1 addition & 1 deletion src/collective/handlebars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

from zope.i18nmessageid import MessageFactory

_ = MessageFactory('collective.handlebars')
_ = MessageFactory("collective.handlebars")
75 changes: 44 additions & 31 deletions src/collective/handlebars/browser/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
import sys

try:
get_distribution('plone.tiles')
except DistributionNotFound: # pragma: no cover
get_distribution("plone.tiles")
except DistributionNotFound: # pragma: no cover

class Tile(BrowserView):
"""Fake Tile which is only a BrowserView"""

class PersistentTile(BrowserView):
"""Fake persistent Tile which is only a BrowserView"""
else: # pragma: no cover


else: # pragma: no cover
from plone.tiles.tile import Tile
from plone.tiles.tile import PersistentTile

Expand All @@ -35,9 +39,8 @@ def package_home(gdict):


class HandlebarsMixin:

def get_contents(self):
""" Get CMS data and put it in a JSON format for hbs inclusion
"""Get CMS data and put it in a JSON format for hbs inclusion
:return: dictonary (must not be a JSON structure!
The conversion is handled by the templating engine)
Expand Down Expand Up @@ -70,7 +73,7 @@ def _get_hbs_template(self, hbs_filename):
return compiled_template

def get_partials(self, hbs_dir):
""" Get partials for rendering the master hbs_template
"""Get partials for rendering the master hbs_template
:param hbs_dir: directory of master template
(/home/vagrant/templates/)
Expand All @@ -80,7 +83,7 @@ def get_partials(self, hbs_dir):
return {}

def get_helpers(self):
""" Get helpers for rendering the master hbs_template
"""Get helpers for rendering the master hbs_template
:return: dictonary with compiled partials templates
({'list': self.list_items()})
Expand All @@ -93,25 +96,26 @@ def hbs_snippet(self, filename=None, _prefix=None):
path = self.get_path_from_prefix(_prefix)
hbs_file = os.path.join(path, filename)
if not os.path.isfile(hbs_file):
raise ValueError('No such file', hbs_file)
elif hasattr(self, 'index'): # noqa
raise ValueError("No such file", hbs_file)
elif hasattr(self, "index"): # noqa
# second scenario: get snippet from zcml
# reuse filename from tile definition in zcml but read file here
# otherwise it is interpreted as XML/PT
hbs_file = self.index.filename
else:
raise ValueError('No template found!')
raise ValueError("No template found!")

hbs_template = self._get_hbs_template(hbs_file)
hbs_dir = os.path.dirname(hbs_file)
partials = self.get_partials(hbs_dir)
helpers = self.get_helpers()

self.request.response.setHeader("Content-type",
"text/html; charset=utf-8")
return hbs_template(self.get_contents(),
helpers=helpers,
partials=partials)
self.request.response.setHeader(
"Content-type", "text/html; charset=utf-8"
)
return hbs_template(
self.get_contents(), helpers=helpers, partials=partials
)

def get_path_from_prefix(self, _prefix):
if isinstance(_prefix, str):
Expand All @@ -122,42 +126,51 @@ def get_path_from_prefix(self, _prefix):
path = package_home(_prefix)
return path

def translate(self, msgid, domain=None, mapping=None, context=None,
target_language=None, default=None):
def translate(
self,
msgid,
domain=None,
mapping=None,
context=None,
target_language=None,
default=None,
):
if not target_language:
target_language = api.portal.get_current_language(context)
return translate(msgid=msgid,
domain=domain,
mapping=mapping,
context=context,
default=default,
target_language=target_language)
return translate(
msgid=msgid,
domain=domain,
mapping=mapping,
context=context,
default=default,
target_language=target_language,
)

def _wrap_widget(self, render):
if render and render.startswith(u'<html><body>'):
if render and render.startswith(u"<html><body>"):
return render
return ''.join([u'<html><body><div>', render, u'</div></body></html>'])
return "".join([u"<html><body><div>", render, u"</div></body></html>"])


class HandlebarsBrowserView(BrowserView, HandlebarsMixin):
""" A simple browserview using hbs as templating engine"""
"""A simple browserview using hbs as templating engine"""

def __call__(self, *args, **kwargs):
return self.hbs_snippet()


class HandlebarsPloneView(BrowserView, HandlebarsMixin):
""" A hbs view rendered in the content slot of the main template of Plone
"""
"""A hbs view rendered in the content slot of the main template of Plone"""

main_template = ViewPageTemplateFile('templates/plone_standard_template.pt') # noqa
main_template = ViewPageTemplateFile(
"templates/plone_standard_template.pt"
)

def __call__(self, *args, **kwargs):
return self.main_template(*args, **kwargs)


class HandlebarsTile(Tile, HandlebarsMixin):

def __call__(self, *args, **kwargs):
return self._wrap_widget(self.hbs_snippet())

Expand All @@ -167,8 +180,8 @@ def __call__(self, *args, **kwargs):


class HandlebarsPersistentTile(PersistentTile, HandlebarsMixin):

def __call__(self, *args, **kwargs):
return self._wrap_widget(self.hbs_snippet())


# EOF
5 changes: 2 additions & 3 deletions src/collective/handlebars/setuphandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

@implementer(INonInstallable)
class HiddenProfiles(object):

def getNonInstallableProfiles(self):
"""Hide uninstall profile from site-creation and quickinstaller"""
return [
'collective.handlebars:default',
'collective.handlebars:uninstall',
"collective.handlebars:default",
"collective.handlebars:uninstall",
]


Expand Down
30 changes: 9 additions & 21 deletions src/collective/handlebars/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
from collective.handlebars.browser.views import HandlebarsBrowserView
from collective.handlebars.browser.views import HandlebarsPloneView
from plone.app.contenttypes.testing import PLONE_APP_CONTENTTYPES_FIXTURE
from plone.app.robotframework.testing import REMOTE_LIBRARY_BUNDLE_FIXTURE
from plone.app.testing import applyProfile
from plone.app.testing import FunctionalTesting
from plone.app.testing import IntegrationTesting
from plone.app.testing import PloneSandboxLayer
from plone.testing import z2

import collective.handlebars

Expand All @@ -21,45 +19,35 @@ def setUpZope(self, app, configurationContext):
# The z3c.autoinclude feature is disabled in the Plone fixture base
# layer.
self.loadZCML(package=collective.handlebars)
self.loadZCML(package=collective.handlebars, name='testing.zcml')
self.loadZCML(package=collective.handlebars, name="testing.zcml")

def setUpPloneSite(self, portal):
applyProfile(portal, 'collective.handlebars:default')
applyProfile(portal, "collective.handlebars:default")


class HBSTestView(HandlebarsBrowserView):

def get_contents(self):
return {'title': u'Fäncy Title', 'body': u'This is the body'}
return {"title": u"Fäncy Title", "body": u"This is the body"}


class HBSTestPloneView(HandlebarsPloneView):

def get_contents(self):
return {'title': u'Fäncy Title',
'body': u'This is the Plone View body'}
return {
"title": u"Fäncy Title",
"body": u"This is the Plone View body",
}


COLLECTIVE_HANDLEBARS_FIXTURE = CollectiveHandlebarsLayer()


COLLECTIVE_HANDLEBARS_INTEGRATION_TESTING = IntegrationTesting(
bases=(COLLECTIVE_HANDLEBARS_FIXTURE,),
name='CollectiveHandlebarsLayer:IntegrationTesting'
name="CollectiveHandlebarsLayer:IntegrationTesting",
)


COLLECTIVE_HANDLEBARS_FUNCTIONAL_TESTING = FunctionalTesting(
bases=(COLLECTIVE_HANDLEBARS_FIXTURE,),
name='CollectiveHandlebarsLayer:FunctionalTesting'
)


COLLECTIVE_HANDLEBARS_ACCEPTANCE_TESTING = FunctionalTesting(
bases=(
COLLECTIVE_HANDLEBARS_FIXTURE,
REMOTE_LIBRARY_BUNDLE_FIXTURE,
z2.ZSERVER_FIXTURE
),
name='CollectiveHandlebarsLayer:AcceptanceTesting'
name="CollectiveHandlebarsLayer:FunctionalTesting",
)
46 changes: 28 additions & 18 deletions src/collective/handlebars/tests/test_setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
"""Setup tests for this package."""
from collective.handlebars.testing import COLLECTIVE_HANDLEBARS_INTEGRATION_TESTING # noqa
from collective.handlebars.testing import (
COLLECTIVE_HANDLEBARS_INTEGRATION_TESTING,
)
from plone import api

import unittest
Expand All @@ -11,9 +13,9 @@
except ImportError: # pragma: no cover
# Quick shim for 5.1 api change

class get_installer(object): # noqa
def __init__(self, portal, request): # noqa
self.installer = api.portal.get_tool(name='portal_quickinstaller')
class get_installer(object): # noqa
def __init__(self, portal, request): # noqa
self.installer = api.portal.get_tool(name="portal_quickinstaller")

def is_product_installed(self, name):
return self.installer.isProductInstalled(name)
Expand All @@ -29,19 +31,20 @@ class TestSetup(unittest.TestCase):

def setUp(self):
"""Custom shared utility setup for tests."""
self.portal = self.layer['portal']
self.portal = self.layer["portal"]
self.installer = get_installer(self.portal, self.layer["request"])

def test_product_installed(self):
"""Test if collective.handlebars is installed."""
self.assertTrue(self.installer.is_product_installed(
'collective.handlebars'))
self.assertTrue(
self.installer.is_product_installed("collective.handlebars")
)

def test_browserlayer(self):
"""Test that ICollectiveHandlebarsLayer is registered."""
from collective.handlebars.interfaces import (
ICollectiveHandlebarsLayer)
from collective.handlebars.interfaces import ICollectiveHandlebarsLayer
from plone.browserlayer import utils

self.assertIn(ICollectiveHandlebarsLayer, utils.registered_layers())


Expand All @@ -50,27 +53,34 @@ class TestUninstall(unittest.TestCase):
layer = COLLECTIVE_HANDLEBARS_INTEGRATION_TESTING

def setUp(self):
self.portal = self.layer['portal']
self.portal = self.layer["portal"]
self.installer = get_installer(self.portal, self.layer["request"])
self.installer.uninstall_product('collective.handlebars')
self.installer.uninstall_product("collective.handlebars")

def test_product_uninstalled(self):
"""Test if collective.handlebars is cleanly uninstalled."""
self.assertFalse(self.installer.is_product_installed(
'collective.handlebars'))
self.assertFalse(
self.installer.is_product_installed("collective.handlebars")
)

def test_browserlayer_removed(self):
"""Test that ICollectiveHandlebarsLayer is removed."""
from collective.handlebars.interfaces import ICollectiveHandlebarsLayer
from plone.browserlayer import utils

self.assertNotIn(ICollectiveHandlebarsLayer, utils.registered_layers())

def test_hiddenprofiles(self):
""" Test uninstall profile is hidden
"""Test uninstall profile is hidden
:return:None
"""
from collective.handlebars.setuphandlers import HiddenProfiles
self.assertIn('collective.handlebars:default',
HiddenProfiles().getNonInstallableProfiles())
self.assertIn('collective.handlebars:uninstall',
HiddenProfiles().getNonInstallableProfiles())

self.assertIn(
"collective.handlebars:default",
HiddenProfiles().getNonInstallableProfiles(),
)
self.assertIn(
"collective.handlebars:uninstall",
HiddenProfiles().getNonInstallableProfiles(),
)
Loading

0 comments on commit 4fa3e06

Please sign in to comment.