From 784bbb71761ac097573fd94a3416dda4e01d4202 Mon Sep 17 00:00:00 2001 From: Noah Morrison Date: Sat, 2 Jan 2021 16:35:20 -0500 Subject: [PATCH] Add option to hide the file system When partials_path is set to None or '', don't try loading from the file system --- chevron/renderer.py | 10 +- test_spec.py | 17 ++++ tests/test-partials-disabled.rendered | 131 ++++++++++++++++++++++++++ 3 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 tests/test-partials-disabled.rendered diff --git a/chevron/renderer.py b/chevron/renderer.py index 50d2e32..1a96fa0 100644 --- a/chevron/renderer.py +++ b/chevron/renderer.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import io -from os import linesep +from os import linesep, path try: from collections.abc import Sequence, Iterator, Callable @@ -103,12 +103,15 @@ def _get_partial(name, partials_dict, partials_path, partials_ext): # Maybe the partial is in the dictionary return partials_dict[name] except KeyError: + # Don't try loading from the file system if the partials_path is None or empty + if partials_path is None or partials_path == '': return '' + # Nope... try: # Maybe it's in the file system path_ext = ('.' + partials_ext if partials_ext else '') - path = partials_path + '/' + name + path_ext - with io.open(path, 'r', encoding='utf-8') as partial: + partial_path = path.join(partials_path, name + path_ext) + with io.open(partial_path, 'r', encoding='utf-8') as partial: return partial.read() except IOError: @@ -147,6 +150,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache', data -- A python dictionary with your data scope partials_path -- The path to where your partials are stored + If set to None, then partials won't be loaded from the file system (defaults to '.') partials_ext -- The extension that you want the parser to look for diff --git a/test_spec.py b/test_spec.py index 08ee2ec..73e7301 100755 --- a/test_spec.py +++ b/test_spec.py @@ -482,6 +482,23 @@ class ExpandedCoverage(unittest.TestCase): self.assertEqual(result, expected) + def test_disabled_partials(self): + os.chdir('tests') + resultNone = chevron.main('test.mustache', 'data.json', + partials_path=None) + + resultEmpty = chevron.main('test.mustache', 'data.json', + partials_path='') + + with io.open('test-partials-disabled.rendered', 'r', encoding='utf-8') as f: + expected = f.read() + if not python3: + expected = expected.encode('utf-8') + + self.assertEqual(resultNone, expected) + self.assertEqual(resultEmpty, expected) + os.chdir('..') + # Run unit tests from command line if __name__ == "__main__": diff --git a/tests/test-partials-disabled.rendered b/tests/test-partials-disabled.rendered new file mode 100644 index 0000000..ce48bd0 --- /dev/null +++ b/tests/test-partials-disabled.rendered @@ -0,0 +1,131 @@ + +variable test +=== +test +=== +test +=== + +comment test +=== +=== +=== + +html escape test (triple brackets) +=== +< > & " +=== +< > & " +=== + +html escape test (ampersand) +=== +< > & " +=== +< > & " +=== + +html escape test (normal) +=== +< > & " +=== +< > & " +=== + +section test (truthy) +=== +true +=== +true +=== + +section test (falsy) +=== +=== +=== + +section test (list) +=== +number: 1 +name: one +--- +number: 2 +name: two +--- +number: 3 +name: three +--- +=== +number: 1 +name: one +--- +number: 2 +name: two +--- +number: 3 +name: three +--- +=== + +section test (scope) +=== +test +new test +=== +test +new test +=== + +inverted section test (truthy) +=== +=== +=== + +inverted section test (falsy) +=== +false +=== +false +=== + +partial test +=== +=== +this is a partial +=== + +delimiter test +=== +test +test +=== +test +test +=== + +unicode test (basic) +=== +(╯°□°)╯︵ ┻━┻ +=== +(╯°□°)╯︵ ┻━┻ +=== + +unicode test (variable) +=== +(╯°□°)╯︵ ┻━┻ +=== +(╯°□°)╯︵ ┻━┻ +=== + +unicode test (partial) +=== +=== +(╯°□°)╯︵ ┻━┻ +=== + +unicode test (no-escape) +=== +(╯°□°)╯︵ ┻━┻ +=== +(╯°□°)╯︵ ┻━┻ +=== -- 2.47.3