Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'c2compat'

  • Loading branch information...
commit d55e8dc28e2c86433c3ceccf11e67111a5128699 2 parents 01497a9 + e758054
@mcdonc mcdonc authored
View
1  .gitignore
@@ -14,6 +14,7 @@ env24/
env27/
jyenv/
pypyenv/
+2xenv/
build/
dist/
deform/coverage.xml
View
5 CHANGES.txt
@@ -4,15 +4,14 @@ Changes
Next release
------------
+- Chameleon 2 compatibility.
+
- Use default widgets for a schema's baseclass if known instead of always
falling back to a text widget.
- Deform now includes a ``beautify.css`` (contributed by Ergo^) in its static
directory, which can be used to make form element styling prettier.
-- Deformdemo application now uses the ``beautify.css`` contributed by Ergo^,
- which makes form element styling and behavior prettier.
-
- Moved ``deformdemo`` into its own package and Github repository
(https://github.com/Pylons/deformdemo).
View
112 deform/template.py
@@ -1,55 +1,70 @@
import os
from pkg_resources import resource_filename
-from chameleon.zpt import language
-from chameleon.zpt.template import PageTemplateFile
-
-from translationstring import ChameleonTranslate
-
from deform.exception import TemplateError
+from translationstring import ChameleonTranslate
-def cache(func):
- def load(self, *args):
- template = self.registry.get(args)
- if template is None:
- self.registry[args] = template = func(self, *args)
- return template
- return load
-
-class ZPTTemplateLoader(object):
- """ A Chameleon ZPT template loader """
- parser = language.Parser()
-
- def __init__(self, search_path=None, auto_reload=True, debug=True,
- encoding='utf-8', translate=None):
- if search_path is None:
- search_path = []
- if isinstance(search_path, basestring):
- search_path = [search_path]
- self.search_path = search_path
- self.auto_reload = auto_reload
- self.debug = debug
- self.encoding = encoding
- self.translate = translate
- self.registry = {}
- self.notexists = {}
-
- @cache
- def load(self, filename):
- for path in self.search_path:
- path = os.path.join(path, filename)
- if (path in self.notexists) and (not self.auto_reload):
- raise TemplateError("Can not find template %s" % filename)
+try:
+ # Chameleon 1
+ from chameleon.zpt import language # will raise exception for c2
+ from chameleon.zpt.template import PageTemplateFile
+
+ def cache(func):
+ def load(self, *args):
+ template = self.registry.get(args)
+ if template is None:
+ self.registry[args] = template = func(self, *args)
+ return template
+ return load
+
+ class ZPTTemplateLoader(object):
+ """ A Chameleon ZPT template loader """
+ parser = language.Parser()
+
+ def __init__(self, search_path=None, auto_reload=True, debug=True,
+ encoding='utf-8', translate=None):
+ if search_path is None:
+ search_path = []
+ if isinstance(search_path, basestring):
+ search_path = [search_path]
+ self.search_path = search_path
+ self.auto_reload = auto_reload
+ self.debug = debug
+ self.encoding = encoding
+ self.translate = translate
+ self.registry = {}
+ self.notexists = {}
+
+ @cache
+ def load(self, filename):
+ for path in self.search_path:
+ path = os.path.join(path, filename)
+ if (path in self.notexists) and (not self.auto_reload):
+ raise TemplateError("Can not find template %s" % filename)
+ try:
+ return PageTemplateFile(path, parser=self.parser,
+ auto_reload=self.auto_reload,
+ debug = self.debug,
+ encoding=self.encoding,
+ translate=self.translate)
+ except OSError:
+ self.notexists[path] = True
+
+ raise TemplateError("Can not find template %s" % filename)
+except ImportError:
+ # Chameleon 2
+ from chameleon.zpt.loader import TemplateLoader
+ class ZPTTemplateLoader(TemplateLoader):
+ def __init__(self, *args, **kwargs):
+ kwargs.setdefault('encoding', 'utf-8')
+ super(ZPTTemplateLoader, self).__init__(*args, **kwargs)
+
+ def load(self, filename, *args, **kwargs):
try:
- return PageTemplateFile(path, parser=self.parser,
- auto_reload=self.auto_reload,
- debug = self.debug,
- encoding=self.encoding,
- translate=self.translate)
- except OSError:
- self.notexists[path] = True
-
- raise TemplateError("Can not find template %s" % filename)
+ return super(ZPTTemplateLoader, self).load(
+ filename, *args, **kwargs)
+ except ValueError:
+ raise TemplateError(filename)
class ZPTRendererFactory(object):
"""
@@ -100,7 +115,10 @@ def __init__(self, search_path, auto_reload=True, debug=True,
self.loader = loader
def __call__(self, template_name, **kw):
- return self.loader.load(template_name + '.pt')(**kw)
+ return self.load(template_name)(**kw)
+
+ def load(self, template_name):
+ return self.loader.load(template_name + '.pt')
default_dir = resource_filename('deform', 'templates/')
View
39 deform/tests/test_template.py
@@ -47,17 +47,19 @@ def test_load_notexists(self):
fixtures = os.path.join(os.path.dirname(__file__), 'fixtures')
loader = self._makeOne(search_path=[fixtures])
self.assertRaises(TemplateError, loader.load, 'doesnt')
- self.failUnless(
- os.path.join(fixtures, 'doesnt') in loader.notexists)
+ if hasattr(loader, 'notexists'): # Chameleon 1
+ self.failUnless(
+ os.path.join(fixtures, 'doesnt') in loader.notexists)
def test_load_negative_cache(self):
import os
fixtures = os.path.join(os.path.dirname(__file__), 'fixtures')
path = os.path.join(fixtures, 'test.pt')
loader = self._makeOne(search_path=[fixtures], auto_reload=True)
- loader.notexists[path] = True
- result = loader.load('test.pt')
- self.failUnless(result)
+ if hasattr(loader, 'notexists'):
+ loader.notexists[path] = True
+ result = loader.load('test.pt')
+ self.failUnless(result)
def test_load_negative_cache2(self):
import os
@@ -65,8 +67,9 @@ def test_load_negative_cache2(self):
fixtures = os.path.join(os.path.dirname(__file__), 'fixtures')
path = os.path.join(fixtures, 'test.pt')
loader = self._makeOne(search_path=[fixtures], auto_reload=False)
- loader.notexists[path] = True
- self.assertRaises(TemplateError, loader.load, 'test.pt')
+ if hasattr(loader, 'notexists'):
+ loader.notexists[path] = True
+ self.assertRaises(TemplateError, loader.load, 'test.pt')
class TestZPTRendererFactory(unittest.TestCase):
def _makeOne(self, dirs, **kw):
@@ -78,21 +81,23 @@ def test_functional(self):
default_dir = resource_filename('deform', 'tests/fixtures/')
renderer = self._makeOne((default_dir,))
result = renderer('test')
- self.assertEqual(result, u'<div>Test</div>')
+ 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(
- ('dir',),
+ (path,),
auto_reload=True,
debug=True,
encoding='utf-16',
translator=lambda *arg: 'translation',
)
- self.assertEqual(renderer.loader.auto_reload, True)
- self.assertEqual(renderer.loader.debug, True)
- self.assertEqual(renderer.loader.search_path, ('dir',))
- self.assertEqual(renderer.loader.encoding, 'utf-16')
- self.assertEqual(renderer.loader.translate('a'), '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 Test_default_renderer(unittest.TestCase):
def _callFUT(self, template, **kw):
@@ -100,11 +105,15 @@ def _callFUT(self, template, **kw):
return default_renderer(template, **kw)
def test_call_defaultdir(self):
+ import re
result = self._callFUT('checkbox',
**{'cstruct':None, 'field':DummyField()})
+ result = re.sub('[ \n]+', ' ', result)
+ result = re.sub(' />', '/>', result)
+ result = result.strip()
self.assertEqual(result,
u'<input type="checkbox" name="name" value="true" '
- 'id="oid" />')
+ 'id="oid"/>')
class DummyWidget(object):
name = 'name'
View
2  setup.py
@@ -28,7 +28,7 @@
CHANGES = ''
requires = [
- 'Chameleon>=1.2.3,<1.999', # debug arg, c2 doesnt yet work
+ 'Chameleon>=1.2.3', # debug arg
'colander>=0.8', # Bindings-providing
'peppercorn>=0.3', # rename operation type
'translationstring',
View
4 tox.ini
@@ -1,8 +1,8 @@
[tox]
envlist =
- py24,py25,py26,py27,cover
+ py25,py26,py27,pypy,cover
-# chameleon fails on pypy and jython
+# chameleon fails on jython
[testenv]
commands =
Please sign in to comment.
Something went wrong with that request. Please try again.