def test_file_extension(self): """ Check that the file_encoding attribute is set correctly. """ renderer = Renderer(file_extension='foo') self.assertEqual(renderer.file_extension, 'foo')
def test_file_encoding__default(self): """ Check the file_encoding default. """ renderer = Renderer() self.assertEqual(renderer.file_encoding, renderer.string_encoding)
def test_template_path_for_partials(self): """ Test that View.template_rel_path is respected for partials. """ spec = TemplateSpec() spec.template = "Partial: {{>tagless}}" renderer1 = Renderer() renderer2 = Renderer(search_dirs=EXAMPLES_DIR) actual = renderer1.render(spec) self.assertString(actual, "Partial: ") actual = renderer2.render(spec) self.assertEqual(actual, "Partial: No tags...")
def export(self, template_file_name, output_file_name, sort="public", data=None, limit=0): """Export ranking to a file. Args: template_file_name (str): where is the template (moustache template) output_file_name (str): where create the file with the ranking sort (str): field to sort the users """ exportedData = {} exportedUsers = self.__exportUsers(sort, limit) exportedData["users"] = exportedUsers exportedData["extraData"] = data with open(template_file_name) as template_file: template_raw = template_file.read() template = parse(template_raw) renderer = Renderer() output = renderer.render(template, exportedData) with open(output_file_name, "w") as text_file: text_file.write(output)
def test_higher_order_rot13(self): view = Lambdas() view.template = '{{#rot13}}abcdefghijklm{{/rot13}}' renderer = Renderer() actual = renderer.render(view) self.assertString(actual, 'nopqrstuvwxyz')
def test_partials_with_lambda(self): view = Lambdas() view.template = '{{>partial_with_lambda}}' renderer = Renderer(search_dirs=EXAMPLES_DIR) actual = renderer.render(view) self.assertEqual(actual, 'nopqrstuvwxyz')
def test_string_encoding__default(self): """ Check the default value. """ renderer = Renderer() self.assertEqual(renderer.string_encoding, sys.getdefaultencoding())
def test_looping_and_negation_context(self): template = '{{#item}}{{header}}: {{name}} {{/item}}{{^item}} Shouldnt see me{{/item}}' context = Complex() renderer = Renderer() actual = renderer.render(template, context) self.assertEqual(actual, "Colors: red Colors: green Colors: blue ")
def test_search_dirs__string(self): """ Check that the search_dirs attribute is set correctly when a string. """ renderer = Renderer(search_dirs='foo') self.assertEqual(renderer.search_dirs, ['foo'])
def test_search_dirs__list(self): """ Check that the search_dirs attribute is set correctly when a list. """ renderer = Renderer(search_dirs=['foo']) self.assertEqual(renderer.search_dirs, ['foo'])
def test_search_dirs__default(self): """ Check the search_dirs default. """ renderer = Renderer() self.assertEqual(renderer.search_dirs, [os.curdir])
def test_missing_tags__default(self): """ Check the missing_tags default. """ renderer = Renderer() self.assertEqual(renderer.missing_tags, 'ignore')
def test_missing_tags(self): """ Check that the missing_tags attribute is set correctly. """ renderer = Renderer(missing_tags='foo') self.assertEqual(renderer.missing_tags, 'foo')
def recursive_render(src_path, dest_path, context, renderer=Renderer(missing_tags='strict'), debug=True): if (os.path.isdir(src_path)): os.makedirs(dest_path, exist_ok=True) if debug: print(f'Enter directory {dest_path}') for name in os.listdir(src_path): child_src_path = os.path.join(src_path, name) child_dest_path = os.path.join(dest_path, renderer.render(name, context)) recursive_render(child_src_path, child_dest_path, context, renderer, debug) elif dest_path[-TEMPLATE_SUFFIX_LEN:] == TEMPLATE_SUFFIX: dest_path = dest_path[:-TEMPLATE_SUFFIX_LEN] with open(src_path, 'r') as src, open(dest_path, 'w') as dest: dest.write(renderer.render(src.read(), context)) if debug: print(f'Parse file {dest_path}') else: shutil.copyfile(src_path, dest_path) if debug: print(f'Copy file {dest_path}')
def test_string_encoding(self): """ Check that the constructor sets the attribute correctly. """ renderer = Renderer(string_encoding="foo") self.assertEqual(renderer.string_encoding, "foo")
def test_nested_context(self): renderer = Renderer() view = NestedContext(renderer) view.template = '{{#foo}}{{thing1}} and {{thing2}} and {{outer_thing}}{{/foo}}{{^foo}}Not foo!{{/foo}}' actual = renderer.render(view) self.assertString(actual, u"one and foo and two")
def _make_renderer(): """ Return a default Renderer instance for testing purposes. """ renderer = Renderer(string_encoding='ascii', file_encoding='ascii') return renderer
def test_callables(self): view = Lambdas() view.template = '{{#replace_foo_with_bar}}foo != bar. oh, it does!{{/replace_foo_with_bar}}' renderer = Renderer() actual = renderer.render(view) self.assertString(actual, u'bar != bar. oh, it does!')
def test_partials__default(self): """ Test the default value. """ renderer = Renderer() self.assertTrue(renderer.partials is None)
def test_higher_order_lambda(self): view = Lambdas() view.template = '{{#sort}}zyxwvutsrqponmlkjihgfedcba{{/sort}}' renderer = Renderer() actual = renderer.render(view) self.assertString(actual, 'abcdefghijklmnopqrstuvwxyz')
def test_partials(self): """ Test that the attribute is set correctly. """ renderer = Renderer(partials={'foo': 'bar'}) self.assertEqual(renderer.partials, {'foo': 'bar'})
def test_hierarchical_partials_with_lambdas(self): view = Lambdas() view.template = '{{>partial_with_partial_and_lambda}}' renderer = Renderer(search_dirs=EXAMPLES_DIR) actual = renderer.render(view) self.assertString(actual, 'nopqrstuvwxyznopqrstuvwxyz')
def test_decode_errors__default(self): """ Check the default value. """ renderer = Renderer() self.assertEqual(renderer.decode_errors, 'strict')
def test_non_callable_attributes(self): view = Simple() view.thing = 'Chris' renderer = Renderer() actual = renderer.render(view) self.assertEqual(actual, "Hi Chris!")
def test_decode_errors(self): """ Check that the constructor sets the attribute correctly. """ renderer = Renderer(decode_errors="foo") self.assertEqual(renderer.decode_errors, "foo")
def test_file_extension__default(self): """ Check the file_extension default. """ renderer = Renderer() self.assertEqual(renderer.file_extension, 'mustache')
def write_log(self): from pystache import Renderer extended_data = self._add_branch_comparison(self.data) with open(self.template_file, 'r') as fh: template = fh.read() with open(self.file_name + '.md', 'w') as fh: fh.write(Renderer().render(template, extended_data))
def export(self, template_file_name, output_file_name, sort="public", data=None, limit=0): """Export ranking to a file. Args: template_file_name (str): where is the template (moustache template) output_file_name (str): where create the file with the ranking sort (str): field to sort the users """ exportedData = {} exportedUsers = self.getSortedUsers() template = self.__getTemplate(template_file_name) position = 1 if not limit: exportedData["users"] = exportedUsers else: exportedData["users"] = exportedUsers[:limit] for u in exportedData["users"]: u["position"] = position u["comma"] = position < len(exportedData["users"]) position += 1 exportedData["extraData"] = data renderer = Renderer() output = renderer.render(template, exportedData) with open(output_file_name, "w") as text_file: text_file.write(output)
def __init__(self, template_dir, template_file): """Create a new PystacheMessageBuilder that uses the specified template file. :param template_dir: The directory to load template files from :param template_file: The template file to populate with values. """ self._renderer = Renderer(search_dirs=template_dir) raw_template = self._renderer.load_template(template_file) self._parsed_template = pystache.parse(raw_template)
def test_partial_in_partial_has_access_to_grand_parent_context(self): renderer = Renderer(search_dirs=EXAMPLES_DIR) view = TemplatePartial(renderer=renderer) view.template = '''{{>partial_in_partial}}''' actual = renderer.render(view, {'prop': 'derp'}) self.assertEqual(actual, 'Hi derp!')