diff options
author | Lars Wirzenius <liw@liw.fi> | 2016-02-22 18:57:38 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2016-02-22 20:54:28 +0200 |
commit | 1478c2bae0fac4069b94fb8c9daf6145e629e8b6 (patch) | |
tree | 96a2d745e587fc3ebeff004d85fd5456490aad92 | |
parent | 7011da4db5ba68865cf54ee75cc0425f98a6b91a (diff) | |
download | distix-1478c2bae0fac4069b94fb8c9daf6145e629e8b6.tar.gz |
Add TextRenderer class
-rw-r--r-- | distixlib/__init__.py | 1 | ||||
-rw-r--r-- | distixlib/templates/ascii.j2 | 1 | ||||
-rw-r--r-- | distixlib/templates/empty.j2 | 0 | ||||
-rw-r--r-- | distixlib/templates/unicode.j2 | 1 | ||||
-rw-r--r-- | distixlib/text_renderer.py | 39 | ||||
-rw-r--r-- | distixlib/text_renderer_tests.py | 48 |
6 files changed, 90 insertions, 0 deletions
diff --git a/distixlib/__init__.py b/distixlib/__init__.py index 85d69f1..6661f31 100644 --- a/distixlib/__init__.py +++ b/distixlib/__init__.py @@ -40,6 +40,7 @@ from .message_thread import MessageThread from .message_renderer import MessageRenderer from .repo import Repository from .git import Git +from .text_renderer import TextRenderer from .util import get_ticket_ids diff --git a/distixlib/templates/ascii.j2 b/distixlib/templates/ascii.j2 new file mode 100644 index 0000000..91e1784 --- /dev/null +++ b/distixlib/templates/ascii.j2 @@ -0,0 +1 @@ +just ascii, please diff --git a/distixlib/templates/empty.j2 b/distixlib/templates/empty.j2 new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/distixlib/templates/empty.j2 diff --git a/distixlib/templates/unicode.j2 b/distixlib/templates/unicode.j2 new file mode 100644 index 0000000..d2553a0 --- /dev/null +++ b/distixlib/templates/unicode.j2 @@ -0,0 +1 @@ +porridge, gröt
\ No newline at end of file diff --git a/distixlib/text_renderer.py b/distixlib/text_renderer.py new file mode 100644 index 0000000..d221fee --- /dev/null +++ b/distixlib/text_renderer.py @@ -0,0 +1,39 @@ +# Copyright 2016 Lars Wirzenius +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +# =*= License: GPL-3+ =*= + + +import jinja2 + + +class TextRenderer(object): + + def __init__(self): + self._template_text = None + self._encoding = None + + def set_output_encoding(self, encoding): + self._encoding = encoding + + def render(self, template_name, variables): + env = self._get_env() + template = env.get_template(template_name) + rendered = template.render(**variables) + return rendered.encode(self._encoding, errors='ignore') + + def _get_env(self): + loader = jinja2.PackageLoader('distixlib') + return jinja2.Environment(loader=loader, trim_blocks=True) diff --git a/distixlib/text_renderer_tests.py b/distixlib/text_renderer_tests.py new file mode 100644 index 0000000..db6df81 --- /dev/null +++ b/distixlib/text_renderer_tests.py @@ -0,0 +1,48 @@ +# Copyright 2016 Lars Wirzenius +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +# =*= License: GPL-3+ =*= + + +import unittest + +import distixlib + + +class TextRendererTests(unittest.TestCase): + + def test_renders_empty_template_to_empty_output(self): + renderer = distixlib.TextRenderer() + renderer.set_output_encoding('us-ascii') + output = renderer.render('empty.j2', {}) + self.assertEqual(output, '') + + def test_renders_usascii_template_to_usascii_output(self): + renderer = distixlib.TextRenderer() + renderer.set_output_encoding('us-ascii') + output = renderer.render('ascii.j2', {}) + self.assertEqual(output, 'just ascii, please') + + def test_renders_non_ascii_template_to_usascii_output(self): + renderer = distixlib.TextRenderer() + renderer.set_output_encoding('us-ascii') + output = renderer.render('unicode.j2', {}) + self.assertEqual(output, 'porridge, grt') + + def test_renders_non_ascii_template_to_utf8_output(self): + renderer = distixlib.TextRenderer() + renderer.set_output_encoding('utf8') + output = renderer.render('unicode.j2', {}) + self.assertEqual(output, u'porridge, gr\u00f6t'.encode('utf8')) |