diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f07d49e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: python +python: + - "2.6" + - "2.7" + - "3.3" +install: "python setup.py develop" +script: "python setup.py test" diff --git a/secretary.py b/secretary.py index 7c2cd01..d7b2620 100644 --- a/secretary.py +++ b/secretary.py @@ -32,11 +32,12 @@ the jinja2 template engine. To render a template: engine = Render(template_file) result = engine.render(template_var1=...) """ +from __future__ import unicode_literals, print_function import re import sys import zipfile -import StringIO +import io from xml.dom.minidom import parseString from jinja2 import Environment, Undefined @@ -45,7 +46,7 @@ class UndefinedSilently(Undefined): # Silently undefined, # see http://stackoverflow.com/questions/6182498/jinja2-how-to-make-it-fail-silently-like-djangotemplate def silently_undefined(*args, **kwargs): - return u'' + return '' return_new = lambda *args, **kwargs: UndefinedSilently() @@ -118,13 +119,14 @@ class Render(object): + def pack_document(self): """ Make an archive from _unpacked_template """ # Save rendered content and headers - self.rendered = StringIO.StringIO() + self.rendered = io.BytesIO() with zipfile.ZipFile(self.rendered, 'a') as packed_template: for filename, content in self.file_list.items(): @@ -141,6 +143,7 @@ class Render(object): + def render(self, **kwargs): """ Unpack and render the internal template and @@ -230,6 +233,7 @@ class Render(object): parent.removeChild(field) + def render_template(template, **kwargs): """ Render a ODF template file @@ -250,7 +254,7 @@ if __name__ == "__main__": {'country': 'United States', 'capital': 'Washington', 'cities': ['miami', 'new york', 'california', 'texas', 'atlanta']}, {'country': 'England', 'capital': 'London', 'cities': ['gales']}, {'country': 'Japan', 'capital': 'Tokio', 'cities': ['hiroshima', 'nagazaki']}, - {'country': 'Nicaragua', 'capital': 'Managua', 'cities': [u'león', 'granada', 'masaya']}, + {'country': 'Nicaragua', 'capital': 'Managua', 'cities': ['león', 'granada', 'masaya']}, {'country': 'Argentina', 'capital': 'Buenos aires'}, {'country': 'Chile', 'capital': 'Santiago'}, {'country': 'Mexico', 'capital': 'MExico City', 'cities': ['puebla', 'cancun']}, @@ -263,4 +267,4 @@ if __name__ == "__main__": output = open('rendered.odt', 'w') output.write(result) - print "Template rendering finished! Check rendered.odt file." + print("Template rendering finished! Check rendered.odt file.") diff --git a/test_secretary.py b/test_secretary.py index 759b5c7..2c9d4a1 100644 --- a/test_secretary.py +++ b/test_secretary.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +from __future__ import unicode_literals + import os from xml.dom.minidom import getDOMImplementation from unittest import TestCase @@ -9,7 +11,6 @@ def test_undefined_silently(): assert isinstance(undefined(), UndefinedSilently) assert isinstance(undefined.attribute, UndefinedSilently) - assert unicode(undefined) == u'' assert str(undefined) == '' def test_pad_string():