summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2012-07-23 08:00:11 -0700
committerChris Jerdonek <chris.jerdonek@gmail.com>2012-07-23 08:00:11 -0700
commit2f3864f9e338d3acb8741bca843a391c5fbb1a1a (patch)
tree2ecdf33b386b226dd78a6c5146a421890bf53fb5
parent3d995628ec8cfce38f3746643d343bb246d150e1 (diff)
parenta1623d7cb64e110d3132835b55cd159c6f96fd46 (diff)
downloadpystache-2f3864f9e338d3acb8741bca843a391c5fbb1a1a.tar.gz
Merge branch 'issue-127' into development: address issue #127
This merge addresses issue #127 by adding support for finding and loading templates by file name in addition to by template name. Thanks again, @xgecko.
-rw-r--r--HISTORY.md2
-rw-r--r--pystache/defaults.py2
-rw-r--r--pystache/loader.py26
-rw-r--r--pystache/locator.py25
-rw-r--r--pystache/tests/data/locator/template.txt1
-rw-r--r--pystache/tests/test_loader.py17
-rw-r--r--pystache/tests/test_locator.py12
7 files changed, 72 insertions, 13 deletions
diff --git a/HISTORY.md b/HISTORY.md
index 5516595..c97a2f7 100644
--- a/HISTORY.md
+++ b/HISTORY.md
@@ -6,6 +6,8 @@ History
- Added option of raising errors on missing tags/partials:
`Renderer(missing_tags='strict')` (issue \#110).
+- Added support for finding and loading templates by file name in
+ addition to by template name (issue \#127). [xgecko]
- Added a `parse()` function that yields a printable, pre-compiled
parse tree.
- Added support for rendering pre-compiled templates.
diff --git a/pystache/defaults.py b/pystache/defaults.py
index 0a20328..bcfdf4c 100644
--- a/pystache/defaults.py
+++ b/pystache/defaults.py
@@ -61,5 +61,5 @@ SEARCH_DIRS = [os.curdir] # i.e. ['.']
#
TAG_ESCAPE = lambda u: escape(u, quote=True)
-# The default template extension.
+# The default template extension, without the leading dot.
TEMPLATE_EXTENSION = 'mustache'
diff --git a/pystache/loader.py b/pystache/loader.py
index 0fdadc5..5855392 100644
--- a/pystache/loader.py
+++ b/pystache/loader.py
@@ -42,9 +42,9 @@ class Loader(object):
Arguments:
- extension: the template file extension. Pass False for no
- extension (i.e. to use extensionless template files).
- Defaults to the package default.
+ extension: the template file extension, without the leading dot.
+ Pass False for no extension (e.g. to use extensionless template
+ files). Defaults to the package default.
file_encoding: the name of the encoding to use when converting file
contents to unicode. Defaults to the package default.
@@ -119,17 +119,29 @@ class Loader(object):
return self.unicode(b, encoding)
- # TODO: unit-test this method.
+ def load_file(self, file_name):
+ """
+ Find and return the template with the given file name.
+
+ Arguments:
+
+ file_name: the file name of the template.
+
+ """
+ locator = self._make_locator()
+
+ path = locator.find_file(file_name, self.search_dirs)
+
+ return self.read(path)
+
def load_name(self, name):
"""
- Find and return the template with the given name.
+ Find and return the template with the given template name.
Arguments:
name: the name of the template.
- search_dirs: the list of directories in which to search.
-
"""
locator = self._make_locator()
diff --git a/pystache/locator.py b/pystache/locator.py
index 2189cf2..30c5b01 100644
--- a/pystache/locator.py
+++ b/pystache/locator.py
@@ -21,9 +21,9 @@ class Locator(object):
Arguments:
- extension: the template file extension. Pass False for no
- extension (i.e. to use extensionless template files).
- Defaults to the package default.
+ extension: the template file extension, without the leading dot.
+ Pass False for no extension (e.g. to use extensionless template
+ files). Defaults to the package default.
"""
if extension is None:
@@ -123,10 +123,29 @@ class Locator(object):
return path
+ def find_file(self, file_name, search_dirs):
+ """
+ Return the path to a template with the given file name.
+
+ Arguments:
+
+ file_name: the file name of the template.
+
+ search_dirs: the list of directories in which to search.
+
+ """
+ return self._find_path_required(search_dirs, file_name)
+
def find_name(self, template_name, search_dirs):
"""
Return the path to a template with the given name.
+ Arguments:
+
+ template_name: the name of the template.
+
+ search_dirs: the list of directories in which to search.
+
"""
file_name = self.make_file_name(template_name)
diff --git a/pystache/tests/data/locator/template.txt b/pystache/tests/data/locator/template.txt
new file mode 100644
index 0000000..bef8160
--- /dev/null
+++ b/pystache/tests/data/locator/template.txt
@@ -0,0 +1 @@
+Test template file
diff --git a/pystache/tests/test_loader.py b/pystache/tests/test_loader.py
index c47239c..f2c2187 100644
--- a/pystache/tests/test_loader.py
+++ b/pystache/tests/test_loader.py
@@ -14,6 +14,10 @@ from pystache import defaults
from pystache.loader import Loader
+# We use the same directory as the locator tests for now.
+LOADER_DATA_DIR = os.path.join(DATA_DIR, 'locator')
+
+
class LoaderTests(unittest.TestCase, AssertStringMixin, SetupDefaults):
def setUp(self):
@@ -178,7 +182,7 @@ class LoaderTests(unittest.TestCase, AssertStringMixin, SetupDefaults):
actual = loader.read(path, encoding='utf-8')
self.assertString(actual, u'non-ascii: é')
- def test_loader__to_unicode__attribute(self):
+ def test_read__to_unicode__attribute(self):
"""
Test read(): to_unicode attribute respected.
@@ -192,3 +196,14 @@ class LoaderTests(unittest.TestCase, AssertStringMixin, SetupDefaults):
#actual = loader.read(path)
#self.assertString(actual, u'non-ascii: ')
+ def test_load_file(self):
+ loader = Loader(search_dirs=[DATA_DIR, LOADER_DATA_DIR])
+ template = loader.load_file('template.txt')
+ self.assertEqual(template, 'Test template file\n')
+
+ def test_load_name(self):
+ loader = Loader(search_dirs=[DATA_DIR, LOADER_DATA_DIR],
+ extension='txt')
+ template = loader.load_name('template')
+ self.assertEqual(template, 'Test template file\n')
+
diff --git a/pystache/tests/test_locator.py b/pystache/tests/test_locator.py
index 8d4ec9b..ee1c2ff 100644
--- a/pystache/tests/test_locator.py
+++ b/pystache/tests/test_locator.py
@@ -19,6 +19,9 @@ from pystache.tests.common import DATA_DIR, EXAMPLES_DIR, AssertExceptionMixin
from pystache.tests.data.views import SayHello
+LOCATOR_DATA_DIR = os.path.join(DATA_DIR, 'locator')
+
+
class LocatorTests(unittest.TestCase, AssertExceptionMixin):
def _locator(self):
@@ -87,6 +90,13 @@ class LocatorTests(unittest.TestCase, AssertExceptionMixin):
self.assertEqual(locator.make_file_name('foo', template_extension='bar'), 'foo.bar')
+ def test_find_file(self):
+ locator = Locator()
+ path = locator.find_file('template.txt', [LOCATOR_DATA_DIR])
+
+ expected_path = os.path.join(LOCATOR_DATA_DIR, 'template.txt')
+ self.assertEqual(path, expected_path)
+
def test_find_name(self):
locator = Locator()
path = locator.find_name(search_dirs=[EXAMPLES_DIR], template_name='simple')
@@ -107,7 +117,7 @@ class LocatorTests(unittest.TestCase, AssertExceptionMixin):
locator = Locator()
dir1 = DATA_DIR
- dir2 = os.path.join(DATA_DIR, 'locator')
+ dir2 = LOCATOR_DATA_DIR
self.assertTrue(locator.find_name(search_dirs=[dir1], template_name='duplicate'))
self.assertTrue(locator.find_name(search_dirs=[dir2], template_name='duplicate'))