Commit b01a1e00 by Chris Jerdonek

Merge branch 'issue_31' into development: closing issue #31.

parents 22fbaa2b 235df11b
......@@ -6,6 +6,7 @@ Next Release (version TBD)
* Bugfix: Whitespace surrounding sections is no longer altered, in
accordance with the mustache spec. [heliodor]
* A custom template loader can now be passed to a View. [cjerdonek]
* Added a command-line interface. [vrde, cjerdonek]
* Added some docstrings. [kennethreitz]
0.4.0 (2011-01-12)
......
# coding: utf-8
"""
This module provides command-line access to pystache.
Run this script using the -h option for command-line help.
"""
import json
# The optparse module is deprecated in Python 2.7 in favor of argparse.
# However, argparse is not available in Python 2.6 and earlier.
from optparse import OptionParser
import sys
# We use absolute imports here to allow use of this script from its
# location in source control (e.g. for development purposes).
# Otherwise, the following error occurs:
#
# ValueError: Attempted relative import in non-package
#
from pystache.loader import Loader
from pystache.template import Template
USAGE = """\
%prog [-h] template context
Render a mustache template with the given context.
positional arguments:
template A filename or template string.
context A filename or JSON string."""
def parse_args(sys_argv, usage):
"""
Return an OptionParser for the script.
"""
args = sys_argv[1:]
parser = OptionParser(usage=usage)
options, args = parser.parse_args(args)
template, context = args
return template, context
def main(sys_argv):
template, context = parse_args(sys_argv, USAGE)
if template.endswith('.mustache'):
template = template[:-9]
try:
template = Loader().load_template(template)
except IOError:
pass
try:
context = json.load(open(context))
except IOError:
context = json.loads(context)
print(Template(template, context).render())
if __name__=='__main__':
main(sys.argv)
......@@ -19,19 +19,34 @@ def publish():
os.system('python setup.py sdist upload')
def make_long_description():
"""
Return the long description for the package.
"""
long_description = open('README.rst').read() + '\n\n' + open('HISTORY.rst').read()
return long_description
if sys.argv[-1] == 'publish':
publish()
sys.exit()
long_description = make_long_description()
setup(name='pystache',
version='0.3.1',
description='Mustache for Python',
long_description=open('README.rst').read() + '\n\n' + open('HISTORY.rst').read(),
long_description=long_description,
author='Chris Wanstrath',
author_email='chris@ozmm.org',
url='http://github.com/defunkt/pystache',
packages=['pystache'],
license='MIT',
entry_points = {
'console_scripts': ['pystache=pystache.commands:main'],
},
classifiers = (
'Development Status :: 4 - Beta',
'License :: OSI Approved :: MIT License',
......
# coding: utf-8
"""
Unit tests of commands.py.
"""
import sys
import unittest
from pystache.commands import main
ORIGINAL_STDOUT = sys.stdout
class MockStdout(object):
def __init__(self):
self.output = ""
def write(self, str):
self.output += str
class CommandsTestCase(unittest.TestCase):
def setUp(self):
sys.stdout = MockStdout()
def callScript(self, template, context):
argv = ['pystache', template, context]
main(argv)
return sys.stdout.output
def testMainSimple(self):
"""
Test a simple command-line case.
"""
actual = self.callScript("Hi {{thing}}", '{"thing": "world"}')
self.assertEquals(actual, u"Hi world\n")
def tearDown(self):
sys.stdout = ORIGINAL_STDOUT
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment