]> Devi Nivas Git - chevron.git/commitdiff
Add option to hide the file system
authorNoah Morrison <noah@morrison.ph>
Sat, 2 Jan 2021 21:35:20 +0000 (16:35 -0500)
committerNoah Morrison <noah@morrison.ph>
Sat, 2 Jan 2021 21:35:20 +0000 (16:35 -0500)
When partials_path is set to None or '', don't try loading from the
file system

chevron/renderer.py
test_spec.py
tests/test-partials-disabled.rendered [new file with mode: 0644]

index 50d2e32641fd5cb58ca91f1a8f4ea6fd93ea3d1b..1a96fa0b014fa13f0b987a6fc6a8105be3071f5f 100644 (file)
@@ -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
index 08ee2ecce6a65a8714f81c079f4c85fa5c05230c..73e7301567a0e09d52649748de144aa18807f09e 100755 (executable)
@@ -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 (file)
index 0000000..ce48bd0
--- /dev/null
@@ -0,0 +1,131 @@
+
+variable test
+===
+test
+===
+test
+===
+
+comment test
+===
+===
+===
+
+html escape test (triple brackets)
+===
+< > & "
+===
+< > & "
+===
+
+html escape test (ampersand)
+===
+< > & "
+===
+< > & "
+===
+
+html escape test (normal)
+===
+&lt; &gt; &amp; &quot;
+===
+&lt; &gt; &amp; &quot;
+===
+
+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)
+===
+<a>(╯°□°)╯︵ ┻━┻
+===
+<a>(╯°□°)╯︵ ┻━┻
+===