diff options
author | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-03-28 20:42:23 -0700 |
---|---|---|
committer | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-03-28 20:42:23 -0700 |
commit | 80dd7698985949c0bb9e7995bfb5389f0328531d (patch) | |
tree | db20c1817e644ebc06c108ffe642075d13310938 /pystache | |
parent | c756f24366f4365d4ee6a7c39d00423f4f188d8e (diff) | |
download | pystache-80dd7698985949c0bb9e7995bfb5389f0328531d.tar.gz |
Simplified Renderer class: Renderer now uses new Loader class.
Diffstat (limited to 'pystache')
-rw-r--r-- | pystache/loader.py | 24 | ||||
-rw-r--r-- | pystache/renderer.py | 67 |
2 files changed, 42 insertions, 49 deletions
diff --git a/pystache/loader.py b/pystache/loader.py index 968f1bd..706a2f7 100644 --- a/pystache/loader.py +++ b/pystache/loader.py @@ -11,6 +11,7 @@ import os import sys from . import defaults +from .locator import Locator class Loader(object): @@ -78,3 +79,26 @@ class Loader(object): text = f.read() return self.unicode(text, encoding) + + def load(self, obj, search_dirs): + """ + Find and return the template associated to the given object. + + Arguments: + + obj: a string or object instance. If obj is a string, then obj + will be interpreted as the template name. Otherwise, obj will + be interpreted as an instance of a user-defined class. + + search_dirs: the list of directories in which to search for + templates when loading a template by name. + + """ + locator = Locator(extension=self.extension) + + if isinstance(obj, basestring): + path = locator.find_path_by_name(search_dirs, obj) + else: + path = locator.find_path_by_object(search_dirs, obj) + + return self.read(path) diff --git a/pystache/renderer.py b/pystache/renderer.py index ece6d0e..adbac3e 100644 --- a/pystache/renderer.py +++ b/pystache/renderer.py @@ -11,9 +11,7 @@ import sys from . import defaults from .context import Context -# TODO: remove this alias. -from .loader import Loader as Reader -from .locator import Locator +from .loader import Loader from .renderengine import RenderEngine @@ -43,7 +41,7 @@ class Renderer(object): """ def __init__(self, file_encoding=None, default_encoding=None, - decode_errors='strict', search_dirs=None, file_extension=None, + decode_errors=None, search_dirs=None, file_extension=None, escape=None, partials=None): """ Construct an instance. @@ -87,9 +85,8 @@ class Renderer(object): encoding name returned by sys.getdefaultencoding(). decode_errors: the string to pass as the errors argument to the - built-in function unicode() when converting to unicode any - strings of type str encountered during the rendering process. - Defaults to "strict". + built-in function unicode() when converting str strings to + unicode. Defaults to the package default. search_dirs: the list of directories in which to search for templates when loading a template by name. Defaults to the @@ -101,6 +98,9 @@ class Renderer(object): Defaults to the package default. """ + if decode_errors is None: + decode_errors = defaults.DECODE_ERRORS + if default_encoding is None: default_encoding = sys.getdefaultencoding() @@ -169,32 +169,23 @@ class Renderer(object): # the default_encoding and decode_errors attributes. return unicode(s, self.default_encoding, self.decode_errors) - def _make_reader(self): - """ - Create a Reader instance using current attributes. - - """ - return Reader(encoding=self.file_encoding, decode_errors=self.decode_errors) - - def make_locator(self): + def _make_loader(self): """ - Create a Locator instance using current attributes. + Create a Loader instance using current attributes. """ - return Locator(extension=self.file_extension) + return Loader(encoding=self.file_encoding, decode_errors=self.decode_errors, + extension=self.file_extension) def _make_load_template(self): """ Return a function that loads a template by name. """ - reader = self._make_reader() - locator = self.make_locator() + loader = self._make_loader() def load_template(template_name): - template_path = locator.find_path_by_name(self.search_dirs, template_name) - - return reader.read(template_path) + return loader.load(template_name, self.search_dirs) return load_template @@ -237,18 +228,6 @@ class Renderer(object): escape=self._escape_to_unicode) return engine - def read(self, path): - """ - Read and return as a unicode string the file contents at path. - - This class uses this method whenever it needs to read a template - file. This method uses the file_encoding and decode_errors - attributes. - - """ - reader = self._make_reader() - return reader.read(path) - # TODO: add unit tests for this method. def load_template(self, template_name): """ @@ -258,19 +237,6 @@ class Renderer(object): load_template = self._make_load_template() return load_template(template_name) - def get_associated_template(self, obj): - """ - Find and return the template associated with an object. - - The function first searches the directory containing the object's - class definition. - - """ - locator = self.make_locator() - template_path = locator.find_path_by_object(self.search_dirs, obj) - - return self.read(template_path) - def _render_string(self, template, *context, **kwargs): """ Render the given template string using the given context. @@ -291,8 +257,10 @@ class Renderer(object): Render the template associated with the given object. """ + loader = self._make_loader() + template = loader.load(obj, self.search_dirs) + context = [obj] + list(context) - template = self.get_associated_template(obj) return self._render_string(template, *context, **kwargs) @@ -303,7 +271,8 @@ class Renderer(object): Read the render() docstring for more information. """ - template = self.read(template_path) + loader = self._make_loader() + template = loader.read(template_path) return self._render_string(template, *context, **kwargs) |