Skip to content

Commit

Permalink
get rid of custom deform renderer code in favor of depending on defor…
Browse files Browse the repository at this point in the history
…m 2.0a2, which allows template specs to be assets specs
  • Loading branch information
mcdonc committed Oct 18, 2013
1 parent 83991b4 commit bb28f03
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 91 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
'ZODB3',
'hypatia>=0.1a6', # check_query method of text index
'venusian>=1.0a3', # pyramid wants this too (prefer_finals...)
'deform>=2.0a1', # bootstrap 3
'deform>=2.0a2', # asset spec in ZPTRendererFactory
'colander>=1.0a1', # subclassable schemanodes
'pyramid_zodbconn',
'pyramid_chameleon',
Expand Down
76 changes: 6 additions & 70 deletions substanced/form/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
import os
from pkg_resources import resource_filename

from translationstring import ChameleonTranslate

from pyramid.i18n import get_localizer
from pyramid.renderers import get_renderer
from pyramid.threadlocal import get_current_request

import deform
import deform.form
import deform.exception
from deform.template import ZPTTemplateLoader
from deform.template import ZPTRendererFactory
import deform.widget

from pyramid.exceptions import ConfigurationError
Expand Down Expand Up @@ -206,74 +203,13 @@ def __getitem__(self, name):
raise KeyError(name)
return data

class DeformRendererFactory(object):
"""
Construct a custom Chameleon ZPT :term:`renderer` for Deform/Substance D.
If the template name is an asset spec (ends with a concrete filename
extension), use the Pyramid rendering machinery to resolve it.
Otherwise, fall back to the Deform rendering (search-path-based)
machinery to resolve it.
This allows users to specify templates without the trouble of needing to
add search paths to the deform rendering machinery.
**Arguments**
search_path
A sequence of strings representing fully qualified filesystem
directories containing Deform Chameleon template sources. The
order in which the directories are listed within ``search_path``
is the order in which they are checked for the template provided
to the renderer.
auto_reload
If true, automatically reload templates when they change (slows
rendering). Default: ``True``.
debug
If true, show nicer tracebacks during Chameleon template rendering
errors (slows rendering). Default: ``True``.
encoding
The encoding that the on-disk representation of the templates
and all non-ASCII values passed to the template should be
expected to adhere to. Default: ``utf-8``.
translator
A translation function used for internationalization when the
``i18n:translate`` attribute syntax is used in the Chameleon
template is active or a
:class:`translationstring.TranslationString` is encountered
during output. It must accept a translation string and return
an interpolated translation. Default: ``None`` (no translation
performed).
"""
def __init__(self, search_path, auto_reload=True, debug=False,
encoding='utf-8', translator=None):
self.translate = translator
loader = ZPTTemplateLoader(search_path=search_path,
auto_reload=auto_reload,
debug=debug,
encoding=encoding,
translate=ChameleonTranslate(translator))
self.loader = loader

def __call__(self, template_name, **kw):
return self.load(template_name)(**kw)

def load(self, template_name):
name, ext = os.path.splitext(template_name)
if ext:
return get_renderer(template_name).implementation()
else:
return self.loader.load(template_name + '.pt')

def translator(term): # pragma: no cover
return get_localizer(get_current_request()).translate(term)

def get_deform_renderer(search_paths):
return ZPTRendererFactory(search_paths, translator=translator)

def includeme(config): # pragma: no cover
deform_dir = resource_filename('deform', 'templates/')
search_path = (deform_dir,)
default_renderer = DeformRendererFactory(search_path, translator=translator)
deform.Form.set_default_renderer(default_renderer)
deform_renderer = get_deform_renderer((deform_dir,))
deform.Form.set_default_renderer(deform_renderer)
24 changes: 4 additions & 20 deletions substanced/form/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,17 @@ def test_clear_doesntexist(self):
inst['a'] = {'randid':'abc'}
inst.clear() # doesn't choke

class TestDeformRendererFactory(unittest.TestCase):
class TestDeformRenderer(unittest.TestCase):
def setUp(self):
config = testing.setUp()
config.include('pyramid_chameleon')

def tearDown(self):
testing.tearDown()

def _makeOne(self, dirs, **kw):
from . import DeformRendererFactory
return DeformRendererFactory(dirs, **kw)
def _makeOne(self, dirs):
from . import get_deform_renderer
return get_deform_renderer(dirs)

def test_functional_using_searchpath(self):
from pkg_resources import resource_filename
Expand All @@ -271,22 +271,6 @@ def test_functional_using_assetspec(self):
result = renderer('substanced.form:fixtures/test.pt')
self.assertEqual(result.strip(), u('<div>Test</div>'))

def test_it(self):
import os
path = os.path.join(os.path.dirname(__file__), 'fixtures')
renderer = self._makeOne(
(path,),
auto_reload=True,
debug=True,
encoding='utf-16',
translator=lambda *arg: 'translation',
)
template = renderer.load('test')
self.assertEqual(template.auto_reload, True)
self.assertEqual(template.debug, True)
self.assertEqual(template.encoding, 'utf-16')
self.assertEqual(template.translate('a'), 'translation')

class DummyWidget(object):
pass

Expand Down

0 comments on commit bb28f03

Please sign in to comment.