Skip to content

Commit

Permalink
Resolves Issue #88
Browse files Browse the repository at this point in the history
  • Loading branch information
BJ Dierkes committed Jan 5, 2012
1 parent 3296024 commit a2b13a7
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 19 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ include =
src/cement2.ext.configobj/cement2/*.py
src/cement2.ext.json/cement2/*.py
src/cement2.ext.yaml/cement2/*.py
src/cement2.ext.genshi/cement2/*.py

[report]
ignore_errors = True
Expand Down
10 changes: 9 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ is available online at:
1.9.5 - development version (will be released as 1.9.6)
------------------------------------------------------------------------------

None
Bug Fixes:

None

Feature Enhancements:

* :issue:`88` - Add cement2.ext.genshi extension, provides Genshi Text
Templating Launguage support.



1.9.4 - Wed Dec 21, 2011
Expand Down
61 changes: 58 additions & 3 deletions doc/source/api/extensions/genshi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ The configurations can be passed as defaults:
from cement2.core import foundation, backend
defaults = backend.defaults('myapp')
defaults['genshi'] = dict()
defaults['genshi']['template_module'] = 'myapp.cli.templates'
defaults['genshi'] = dict(
template_module='myapp.cli.templates'
)
app = foundation.lay_cement('myapp', defaults=defaults)
Expand All @@ -40,7 +41,61 @@ following:
Example Usage
-------------

FIX ME
The following is an example application within a python package.

*myapp/appmain.py*

.. code-block:: python
from cement2.core import foundation, controller, handler, backend
# create the application
defaults = backend.defaults('myapp')
defaults['base']['extensions'].append('genshi')
defaults['base']['output_handler'] = 'genshi'
app = foundation.lay_cement('myapp', defaults=defaults)
# define application controllers
class MyAppBaseController(controller.CementBaseController):
class Meta:
label = 'base'
interface = controller.IController
description = "My Application Does Amazing Things"
defaults = dict()
arguments = []
@controller.expose(help="base controller default command", hide=True)
def default(self):
data = dict(
controller='base',
command='default'
)
print self.render(data, 'default.txt')
# register the controllers
handler.register(MyAppBaseController)
# setup the application
app.setup()
try:
app.run()
finally:
app.close()
*myapp/templates/default.txt*

.. code-block:: text
Inside the ${controller}.${command} function!
And the output:

>>> from myapp import appmain
Inside the base.default function!


Genshi Syntax Basics
Expand Down
16 changes: 12 additions & 4 deletions src/cement2.ext.genshi/cement2/lib/ext_genshi.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,23 @@ def render(self, data_dict, template):
The data dictionary to render.
template
This option is completely ignored.
The file system path to the template file, *after* within the
template_module. For example, if the template module were
'myapp.templates' (a.k.a. 'myapp/templates/__init__.py') then
and the full template file path were
'myapp/templates/users/display.txt' then you would only pass
'users/display.txt'.
Returns: string
"""
tmpl_module = self.config.get('genshi', 'template_module')
Log.debug("genshi template module is '%s'" % tmpl_module)
Log.debug("rendering output using '%s' as a template." % template)

if template is None:
raise exc.CementRuntimeError("Invalid template 'None'.")

template = template.lstrip('/')

# get the template content
tmpl_content = pkgutil.get_data(tmpl_module, template)
Expand All @@ -66,10 +75,9 @@ def render(self, data_dict, template):
raise exc.CementRuntimeError(
"Template file '%s' does not exist in module '%s'." % \
(template, tmpl_module))
res = ''
else:
Log.debug("rendering output using '%s' as a template." % template)
tmpl = NewTextTemplate(tmpl_content)
res = tmpl.generate(**data_dict).render()
return res

return res
44 changes: 35 additions & 9 deletions src/cement2.ext.genshi/tests/ext/test_ext_genshi.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
"""Tests for cement2.ext.ext_genshi."""

import random
from nose.tools import with_setup, ok_, eq_, raises
from nose import SkipTest

from cement2.core import handler, backend, log
from cement2.core import exc, foundation, handler, backend, controller
from cement2 import test_helper as _t

# create the application
defaults = backend.defaults('myapp')
defaults['base']['extensions'].append('cement2.ext.ext_genshi')
defaults['base']['extensions'].append('genshi')
defaults['base']['output_handler'] = 'genshi'
defaults['genshi'] = dict(
template_module='tests.templates'
)
#app = foundation.lay_cement('myapp', defaults=defaults)
app = _t.prep('myapp', defaults=defaults)
app.setup()
app.setup()
app.argv = []
app.run()


def test_genshi():
app.argv = ['default']

app.run()
res = app.render(dict(foo='bar'))
genshi_res = "foo equals bar"
rando = random.random()
res = app.render(dict(foo=rando), 'test_template.txt')
genshi_res = "foo equals %s" % rando
eq_(res, genshi_res)

def test_genshi_bad_template():
res = app.render(dict(foo='bar'), 'bad_template.txt')

@raises(IOError)
def test_genshi_nonexistent_template():
res = app.render(dict(foo='bar'), 'missing_template.txt')

@raises(exc.CementRuntimeError)
def test_genshi_none_template():
try:
res = app.render(dict(foo='bar'), None)
except exc.CementRuntimeError as e:
eq_(e.msg, "Invalid template 'None'.")
raise

@raises(exc.CementRuntimeError)
def test_genshi_bad_module():
app.config.set('genshi', 'template_module', 'this_is_a_bogus_module')
res = app.render(dict(foo='bar'), 'bad_template.txt')
Empty file.
Empty file.
1 change: 1 addition & 0 deletions src/cement2.ext.genshi/tests/templates/test_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo equals ${foo}
6 changes: 4 additions & 2 deletions utils/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ SOURCES=" \
src/cement2 \
src/cement2.ext.configobj \
src/cement2.ext.json \
src/cement2.ext.yaml
"
src/cement2.ext.yaml \
src/cement2.ext.genshi"

pip install nose coverage

for path in $SOURCES; do
pushd $path
Expand Down

0 comments on commit a2b13a7

Please sign in to comment.