Andriy Kornatskyy avatar Andriy Kornatskyy committed a72c4c9

Introduced FileLoader.

Comments (0)

Files changed (5)

src/wheezy/template/loader.py

+
+"""
+"""
+
+import os
+import os.path
+
+
+class FileLoader(object):
+
+    def __init__(self, directories, encoding='UTF-8'):
+        searchpath = []
+        for path in directories:
+            abspath = os.path.abspath(path)
+            assert os.path.exists(abspath)
+            assert os.path.isdir(abspath)
+            searchpath.append(abspath)
+        self.searchpath = searchpath
+        self.encoding = encoding
+
+    def list_names(self):
+        names = []
+        for path in self.searchpath:
+            pathlen = len(path) + 1
+            for dirpath, dirnames, filenames in os.walk(path):
+                for filename in filenames:
+                    name = os.path.join(dirpath, filename)[pathlen:]
+                    name = name.replace('\\', '/')
+                    names.append(name)
+        return names
+
+    def load(self, name):
+        for path in self.searchpath:
+            filename = os.path.join(path, name)
+            if not os.path.exists(filename):
+                continue
+            if not os.path.isfile(filename):
+                continue
+            f = open(filename, 'rb')
+            try:
+                return f.read().decode(self.encoding)
+            finally:
+                f.close()
+        else:
+            return None

src/wheezy/template/tests/templates/shared/master.html

+
Add a comment to this file

src/wheezy/template/tests/templates/shared/snippet/script.html

Empty file added.

src/wheezy/template/tests/templates/tmpl1.html

+

src/wheezy/template/tests/test_loader.py

+
+""" Unit tests for ``wheezy.templates.loader``.
+"""
+
+import unittest
+
+
+class FileLoaderTestCase(unittest.TestCase):
+    """ Test the ``FileLoader``.
+    """
+
+    def setUp(self):
+        import os.path
+        from wheezy.template.loader import FileLoader
+        curdir = os.path.dirname(__file__)
+        tmpldir = os.path.join(curdir, 'templates')
+        self.loader = FileLoader(directories=[tmpldir])
+
+    def test_list_names(self):
+        """ Tests list_names.
+        """
+        assert ['tmpl1.html',
+                'shared/master.html',
+                'shared/snippet/script.html'
+        ] == self.loader.list_names()
+
+    def test_load_existing(self):
+        """ Tests load.
+        """
+        assert self.loader.load('tmpl1.html')
+
+    def test_load_not_found(self):
+        """ Tests load if the name is not found.
+        """
+        assert not self.loader.load('tmpl-x.html')
+
+    def test_load_not_a_file(self):
+        """ Tests load if the name is not a file.
+        """
+        assert not self.loader.load('shared/snippet')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.