Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d45945b
Make the tests easily runnable, add Travis.
gsnedders Oct 30, 2015
78b1485
first fix
rokatyy Mar 13, 2019
4e196e4
first fix
rokatyy Mar 13, 2019
48aedb7
fix syntax
rokatyy Mar 13, 2019
6e69cf2
fix syntax
rokatyy Mar 13, 2019
d6ee7e4
fix for some tests
rokatyy Mar 13, 2019
48e3777
Delete Template-Python.iml
rokatyy Mar 13, 2019
f8a61b7
Delete rogate.xml
rokatyy Mar 13, 2019
345fdcb
Delete encodings.xml
rokatyy Mar 13, 2019
bf081c7
Delete misc.xml
rokatyy Mar 13, 2019
2e968f4
Delete modules.xml
rokatyy Mar 13, 2019
e98ffa0
Delete vcs.xml
rokatyy Mar 13, 2019
8877c4a
Merge remote-tracking branch 'origin/testing' into python3
Mar 13, 2019
9225930
added python 3.7 to .travis.yml in align with existing items.
Mar 13, 2019
e78ee5b
added brackets to print calls in tests
Mar 13, 2019
7029bb5
replaced `Exception, e` with `Exception as e` syntax
Mar 13, 2019
936ea4e
added brackets to more prints in non-.py files
Mar 13, 2019
e53ae3c
Merge remote-tracking branch 'gsned/master' into python3
Mar 14, 2019
d0a0909
* added 3.7 support to `tox.ini` and `.travis.yml`
Mar 14, 2019
bc7878d
cStringIO to StringIO from io
rokatyy Mar 14, 2019
5737655
cStringIO to StringIO from io
rokatyy Mar 14, 2019
3714d73
Update filter_test.py
rokatyy Mar 14, 2019
301d1c2
fix for python3 support
rokatyy Mar 14, 2019
4abce5c
Update provider_test.py
rokatyy Mar 14, 2019
ac0502a
Update provider.py
rokatyy Mar 14, 2019
16531d4
assert changes and some syntax
rokatyy Mar 18, 2019
764058a
Update provider.py
rokatyy Mar 19, 2019
01a9a35
Merge branch 'python3' into python3_except
rokatyy Mar 19, 2019
aac29cc
Merge pull request #1 from rokatyy/python3_except
rokatyy Mar 19, 2019
324809e
assert changes and some syntax
rokatyy Mar 19, 2019
4af47ed
Merge remote-tracking branch 'origin/python3' into python3_v1
rokatyy Mar 19, 2019
78849be
fix exception
rokatyy Mar 19, 2019
0288f2e
fix exception
rokatyy Mar 19, 2019
8f66211
delete strange pycharm changes
rokatyy Mar 19, 2019
928dcba
getting CACHE_SIZE fix
rokatyy Mar 19, 2019
725af1f
Merge pull request #2 from rokatyy/python
rokatyy Mar 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ matrix:
include:
- python: 2.7
env: TOXENV=py27
- python: 3.7
env: TOXENV=py37
- python: pypy2.7-6.0
env: TOXENV=pypy

Expand Down
30 changes: 15 additions & 15 deletions README.python
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,20 @@ Python:
from template import Template

template = Template()
print template.process("someblock")
print(template.process("someblock"))

Or just:

print Template().process("someblock")
print(Template().process("someblock"))

You can, of course, catch a raised exception to take special action.

from template import Template, TemplateException

try:
print Template().process("someblock")
except TemplateException, e:
print >>sys.stderr, "Got exception: ", e
print(Template().process("someblock"))
except TemplateException as e:
print("Got exception: ", e, file=sys.stderr)

The OUTPUT and OUTPUT_PATH options are honored, but do not otherwise
change the behavior of process().
Expand Down Expand Up @@ -380,7 +380,7 @@ I have tried to follow a principle of least surprise here. Dict
lookup is attempted up to three times, first using the given key, then
using the stringified key if possible, then using the key converted to
an integer if possible. Therefore the previous template snippet would
print "number", and the other cases are exhibited thusly:
print("number"), and the other cases are exhibited thusly:

mydict = { 1: "one", "2": "two" }

Expand Down Expand Up @@ -473,9 +473,9 @@ Perl version:
Python version:

[% PYTHON %]
print context.include('myfile'),
print(context.include('myfile')),
stash.set('foo', 'bar')
print 'foo value:', stash.get('foo'),
print('foo value:', stash.get('foo')),
[% END %]

Note that Python's print statement has semantics that may be
Expand Down Expand Up @@ -540,15 +540,15 @@ syntax error, and should be avoided.
An example of a block that will cause a syntax error:

[% PYTHON %]
print "line 1"
print "line 2"
print("line 1")
print("line 2")
[% END %]

Another block that will cause a syntax error:

[% PYTHON %]
print "line 1"
print "line 2"
print("line 1")
print("line 2")
[% END %]

Note that leading whitespace is stripped indiscriminately; tab and
Expand All @@ -562,9 +562,9 @@ built-in Python function repr(). Example:

[% x = 1; y = 2 %]
[% PYTHON %]
print "x =", [% x | repr %], "; y =", [% y | repr %]
print("x =", [% x | repr %], "; y =", [% y | repr %])
# Or:
print "x = %s; y = %s" % ([% x | repr %], [% y | repr %])
print("x = %s; y = %s" % ([% x | repr %], [% y | repr %]))
[% END %]

Finally, there is a standard filter "python", but it is more limited
Expand All @@ -586,7 +586,7 @@ as described above.
[% FILTER python %]
x = f()
y = g()
print "hello world"
print("hello world")
[% END %]


Expand Down
6 changes: 3 additions & 3 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
t = Template()

try:
print t.processString(TEMPLATE_TEXT, { "thing": "world" })
except TemplateException, e:
print "ERROR: %s" % e
print(t.processString(TEMPLATE_TEXT, { "thing": "world" }))
except TemplateException as e:
print("ERROR: %s" % e)
4 changes: 2 additions & 2 deletions examples/hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
template = Template()
source = Literal("Hello [% name or 'World' %]!")

print template.process(source);
print template.process(source, {'name':'Badger'});
print(template.process(source))
print(template.process(source, {'name':'Badger'}))

1 change: 1 addition & 0 deletions t/args_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ def testArgs(self):

"""

main()
1 change: 1 addition & 0 deletions t/binop_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,4 @@ def reset(): counter[0] = 0; return counter[0]
one is less than two
"""

main()
1 change: 1 addition & 0 deletions t/block_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@ def testBlock(self):

"""

main()

1 change: 1 addition & 0 deletions t/blocks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ def testBlocks(self):
this is block two, b is brazen
"""

main()

1 change: 1 addition & 0 deletions t/capture_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ def testCapture(self):

"""

main()
1 change: 1 addition & 0 deletions t/case_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ def testCase(self):
1 1
"""

main()
1 change: 1 addition & 0 deletions t/chomp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,4 @@ def testChomp(self):

"""

main()
1 change: 1 addition & 0 deletions t/compile1_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ def testCompile(self):

"""

main()
1 change: 1 addition & 0 deletions t/compile2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ def testCompile(self):

"""

main()

3 changes: 2 additions & 1 deletion t/compile3_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def testCompile(self):
try:
Template(ttcfg).process("evalpython", {})
self.fail("did not raise exception")
except TemplateException, e:
except TemplateException as e:
self.assertEquals("python", e.type())
self.assertEquals("EVAL_PYTHON not set", e.info())

Expand Down Expand Up @@ -71,4 +71,5 @@ def testCompile(self):

"""

main()

1 change: 1 addition & 0 deletions t/compile4_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ def testCompile(self):
This is the blam file
"""

main()

4 changes: 1 addition & 3 deletions t/compile5_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
import re
import shutil

import pytest

from template.test import TestCase, main


class CompileTest(TestCase):
@pytest.mark.xfail
def testCompile(self):
dir = os.path.abspath("test")
cdir = os.path.join(dir, "tmp", "cache")
Expand Down Expand Up @@ -73,3 +70,4 @@ def twiddle(*args):
This is the wam-bam file
"""

main()
51 changes: 26 additions & 25 deletions t/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,86 +10,86 @@ def testConfig(self):
# Parser:
parser = factory.parser({ 'PRE_CHOMP': 1, 'INTERPOLATE': True })
self.assertTrue(parser)
self.assertEquals(1, parser.pre_chomp)
self.assertTrue(parser.interpolate)
self.assertEqual(1, parser.pre_chomp)
self.failUnless(parser.interpolate)
parser = factory.parser({ 'POST_CHOMP': 1 })
self.assertTrue(parser)
self.failUnless(parser)
self.assertEquals(1, parser.post_chomp)

# Provider:
provider = factory.provider({ 'INCLUDE_PATH': 'here:there',
'PARSER': parser })
self.assertTrue(provider)
self.failUnless(provider)
self.assertEquals(['here', 'there'], provider.include_path())
self.assertEquals(1, provider.parser().post_chomp)
provider = factory.provider({ 'INCLUDE_PATH': 'cat:mat',
'ANYCASE': True,
'INTERPOLATE': True })
self.assertTrue(provider)
self.failUnless(provider)
self.assertEquals(['cat', 'mat'], provider.include_path())
# Force the provider to instantiate a parser and check it uses the
# correct parameters.
text = 'The cat sat on the mat'
self.assertTrue(provider.fetch(Literal(text)))
self.assertTrue(provider.parser().anycase)
self.assertTrue(provider.parser().interpolate)
self.failUnless(provider.fetch(Literal(text)))
self.failUnless(provider.parser().anycase)
self.failUnless(provider.parser().interpolate)

# Plugins:
plugins = factory.plugins({ 'PLUGIN_BASE': ('my.plugins', 'MyPlugins') })
self.assertTrue(plugins)
self.failUnless(plugins)
self.assertEquals([('my.plugins', 'MyPlugins'), 'template.plugin'],
plugins.plugin_base())
plugins = factory.plugins({ 'LOAD_PYTHON': True,
'PLUGIN_BASE': ('my.plugins', 'NewPlugins') })
self.assertTrue(plugins)
self.assertTrue(plugins.load_python())
self.failUnless(plugins)
self.failUnless(plugins.load_python())
self.assertEquals([('my.plugins', 'NewPlugins'), 'template.plugin'],
plugins.plugin_base())

# Filters:
filters = factory.filters({ 'TOLERANT': True })
self.assertTrue(filters)
self.assertTrue(filters.tolerant())
self.failUnless(filters)
self.failUnless(filters.tolerant())
filters = factory.filters({ 'TOLERANT': True })
self.assertTrue(filters)
self.assertTrue(filters.tolerant())
self.failUnless(filters)
self.failUnless(filters.tolerant())

# Stash:
stash = factory.stash({ 'foo': 10, 'bar': 20 })
self.assertTrue(stash)
self.failUnless(stash)
self.assertEquals(10, stash.get('foo').value())
self.assertEquals(20, stash.get('bar').value())
stash = factory.stash({ 'foo': 30, 'bar': lambda *_: 'forty' })
self.assertTrue(stash)
self.failUnless(stash)
self.assertEquals(30, stash.get('foo').value())
self.assertEquals('forty', stash.get('bar').value())

# Context:
context = factory.context({})
self.assertTrue(context)
self.failUnless(context)
context = factory.context({ 'INCLUDE_PATH': 'anywhere' })
self.assertTrue(context)
self.failUnless(context)
self.assertEquals('anywhere',
context.load_templates()[0].include_path()[0])
context = factory.context({ 'LOAD_TEMPLATES': [ provider ],
'LOAD_PLUGINS': [ plugins ],
'LOAD_FILTERS': [ filters ],
'STASH': stash })
self.assertTrue(context)
self.failUnless(context)
self.assertEquals(30, context.stash().get('foo').value())
self.assertTrue(context.load_templates()[0].parser().interpolate)
self.assertTrue(context.load_plugins()[0].load_python())
self.assertTrue(context.load_filters()[0].tolerant())
self.failUnless(context.load_templates()[0].parser().interpolate)
self.failUnless(context.load_plugins()[0].load_python())
self.failUnless(context.load_filters()[0].tolerant())

# Service:
service = factory.service({ 'INCLUDE_PATH': 'amsterdam' })
self.assertTrue(service)
self.failUnless(service)
self.assertEquals(['amsterdam'],
service.context().load_templates()[0].include_path())

# Iterator:
iterator = factory.iterator(['foo', 'bar', 'baz'])
self.assertTrue(iterator)
self.failUnless(iterator)
self.assertEquals('foo', iterator.get_first())
self.assertEquals('bar', iterator.get_next())
self.assertEquals('baz', iterator.get_next())
Expand All @@ -98,3 +98,4 @@ def testConfig(self):
# (later)


main()
1 change: 1 addition & 0 deletions t/constants_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,5 @@ def counter():
c: abw
"""

main()

5 changes: 3 additions & 2 deletions t/context_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def testContext(self):
error = None
try:
tmpl = ctx.template('no_such_template')
except Exception, e:
except Exception as e:
error = e
self.assertTrue(error)
self.assertEquals('file error - no_such_template: not found', str(error))
Expand Down Expand Up @@ -102,7 +102,7 @@ def __init__(self, text):
try:
plugin = ctx.plugin('no_such_plugin')
self.fail('Exception not raised')
except Exception, e:
except Exception as e:
self.assertEquals('plugin error - no_such_plugin: plugin not found',
str(e))

Expand Down Expand Up @@ -147,3 +147,4 @@ def __init__(self, text):
self.assertEquals('charlie', stash.get('a').value())


main()
1 change: 1 addition & 0 deletions t/datafile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ def testDatafile(self):

"""

main()
1 change: 1 addition & 0 deletions t/date_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,4 @@ def testDate(self):

"""

main()
1 change: 1 addition & 0 deletions t/directive_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,4 @@ def testDirectives(self):

"""

main()
1 change: 1 addition & 0 deletions t/directory_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,4 @@ def testDirectory(self):
#
"""

main()
1 change: 1 addition & 0 deletions t/document_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,4 @@ def testDocument(self):
two, three
"""

main()
7 changes: 4 additions & 3 deletions t/error_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ def testError(self):
try:
tmpl.process("badinc")
self.fail("Failed to raise exception")
except TemplateException, e:
self.assertEquals('file', e.type())
self.assertEquals('nosuchfile: not found', e.info())
except TemplateException as e:
self.assertEqual('file', e.type())
self.assertEqual('nosuchfile: not found', e.info())


main()
Loading