summaryrefslogtreecommitdiff
path: root/pystache/loader.py
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2011-12-28 17:41:17 -0800
committerChris Jerdonek <chris.jerdonek@gmail.com>2011-12-28 17:41:17 -0800
commit7dc18cb1016228ece342e6e0ad0f35a1d4cd0325 (patch)
tree6c5426d6a624a23c42d09081a4b1b3cac05cd24c /pystache/loader.py
parent031186757b8926be2f5b86dff1bab8ffff7efc9f (diff)
downloadpystache-7dc18cb1016228ece342e6e0ad0f35a1d4cd0325.tar.gz
Renamed loader.py to locator.py.
Diffstat (limited to 'pystache/loader.py')
-rw-r--r--pystache/loader.py93
1 files changed, 0 insertions, 93 deletions
diff --git a/pystache/loader.py b/pystache/loader.py
deleted file mode 100644
index ee041df..0000000
--- a/pystache/loader.py
+++ /dev/null
@@ -1,93 +0,0 @@
-# coding: utf-8
-
-"""
-This module provides a Locator class.
-
-"""
-
-import os
-import re
-import sys
-
-
-DEFAULT_EXTENSION = 'mustache'
-
-
-def make_template_name(obj):
- """
- Return the canonical template name for an object instance.
-
- This method converts Python-style class names (PEP 8's recommended
- CamelCase, aka CapWords) to lower_case_with_underscords. Here
- is an example with code:
-
- >>> class HelloWorld(object):
- ... pass
- >>> hi = HelloWorld()
- >>> make_template_name(hi)
- 'hello_world'
-
- """
- template_name = obj.__class__.__name__
-
- def repl(match):
- return '_' + match.group(0).lower()
-
- return re.sub('[A-Z]', repl, template_name)[1:]
-
-
-class Locator(object):
-
- def __init__(self, search_dirs=None, extension=None):
- """
- Construct a template locator.
-
- Arguments:
-
- search_dirs: the list of directories in which to search for templates,
- for example when looking for partials. Defaults to the current
- working directory. If given a string, the string is interpreted
- as a single directory.
-
- extension: the template file extension. Defaults to "mustache".
- Pass False for no extension (i.e. extensionless template files).
-
- """
- if extension is None:
- extension = DEFAULT_EXTENSION
-
- if search_dirs is None:
- search_dirs = os.curdir # i.e. "."
-
- if isinstance(search_dirs, basestring):
- search_dirs = [search_dirs]
-
- self.search_dirs = search_dirs
- self.template_extension = extension
-
- def make_file_name(self, template_name):
- file_name = template_name
- if self.template_extension is not False:
- file_name += os.path.extsep + self.template_extension
-
- return file_name
-
- def locate_path(self, template_name):
- """
- Find and return the path to the template with the given name.
-
- Raises an IOError if the template cannot be found.
-
- """
- search_dirs = self.search_dirs
-
- file_name = self.make_file_name(template_name)
-
- for dir_path in search_dirs:
- file_path = os.path.join(dir_path, file_name)
- if os.path.exists(file_path):
- return file_path
-
- # TODO: we should probably raise an exception of our own type.
- raise IOError('"%s" not found in "%s"' % (template_name, ':'.join(search_dirs),))
-