Commit ed896eb9 by Chris Jerdonek

Renamed Locator methods to find_name and find_object.

parent 8f8fd6c4
......@@ -107,6 +107,7 @@ class Loader(object):
return self.unicode(text, encoding)
# TODO: consider passing search_dirs in the constructor.
# TODO: unit-test this method.
def load_name(self, name, search_dirs):
"""
......@@ -121,10 +122,11 @@ class Loader(object):
"""
locator = self._make_locator()
path = locator.find_path_by_name(search_dirs, name)
path = locator.find_name(search_dirs, name)
return self.read(path)
# TODO: consider passing search_dirs in the constructor.
# TODO: unit-test this method.
def load_object(self, obj, search_dirs):
"""
......@@ -139,6 +141,6 @@ class Loader(object):
"""
locator = self._make_locator()
path = locator.find_path_by_object(search_dirs, obj)
path = locator.find_object(search_dirs, obj)
return self.read(path)
......@@ -123,7 +123,7 @@ class Locator(object):
return path
def find_path_by_name(self, search_dirs, template_name):
def find_name(self, search_dirs, template_name):
"""
Return the path to a template with the given name.
......@@ -132,7 +132,7 @@ class Locator(object):
return self._find_path_required(search_dirs, file_name)
def find_path_by_object(self, search_dirs, obj, file_name=None):
def find_object(self, search_dirs, obj, file_name=None):
"""
Return the path to a template associated with the given object.
......
# coding: utf-8
"""
This module supports customized (or special/specified) template loading.
This module supports customized (aka special or specified) template loading.
"""
......@@ -164,22 +164,21 @@ class SpecLoader(object):
return (template_dir, file_name)
# TODO: make this private.
def get_template_path(self, view):
def _find(self, spec):
"""
Return the path to the view's associated template.
Find and return the path to the template associated to the instance.
"""
dir_path, file_name = self.get_relative_template_location(view)
dir_path, file_name = self.get_relative_template_location(spec)
# TODO: share code with the loader attribute here.
locator = Locator(extension=self.loader.extension)
if dir_path is None:
# Then we need to search for the path.
path = locator.find_path_by_object(self.search_dirs, view, file_name=file_name)
path = locator.find_object(self.search_dirs, spec, file_name=file_name)
else:
obj_dir = locator.get_object_directory(view)
obj_dir = locator.get_object_directory(spec)
path = os.path.join(obj_dir, dir_path, file_name)
return path
......@@ -198,6 +197,6 @@ class SpecLoader(object):
if spec.template is not None:
return self.loader.unicode(spec.template, spec.template_encoding)
path = self.get_template_path(spec)
path = self._find(spec)
return self.loader.read(path, spec.template_encoding)
......@@ -69,21 +69,21 @@ class LocatorTests(unittest.TestCase):
self.assertEquals(locator.make_file_name('foo', template_extension='bar'), 'foo.bar')
def test_find_path_by_name(self):
def test_find_name(self):
locator = Locator()
path = locator.find_path_by_name(search_dirs=['examples'], template_name='simple')
path = locator.find_name(search_dirs=['examples'], template_name='simple')
self.assertEquals(os.path.basename(path), 'simple.mustache')
def test_find_path_by_name__using_list_of_paths(self):
def test_find_name__using_list_of_paths(self):
locator = Locator()
path = locator.find_path_by_name(search_dirs=['doesnt_exist', 'examples'], template_name='simple')
path = locator.find_name(search_dirs=['doesnt_exist', 'examples'], template_name='simple')
self.assertTrue(path)
def test_find_path_by_name__precedence(self):
def test_find_name__precedence(self):
"""
Test the order in which find_path_by_name() searches directories.
Test the order in which find_name() searches directories.
"""
locator = Locator()
......@@ -91,47 +91,47 @@ class LocatorTests(unittest.TestCase):
dir1 = DATA_DIR
dir2 = os.path.join(DATA_DIR, 'locator')
self.assertTrue(locator.find_path_by_name(search_dirs=[dir1], template_name='duplicate'))
self.assertTrue(locator.find_path_by_name(search_dirs=[dir2], template_name='duplicate'))
self.assertTrue(locator.find_name(search_dirs=[dir1], template_name='duplicate'))
self.assertTrue(locator.find_name(search_dirs=[dir2], template_name='duplicate'))
path = locator.find_path_by_name(search_dirs=[dir2, dir1], template_name='duplicate')
path = locator.find_name(search_dirs=[dir2, dir1], template_name='duplicate')
dirpath = os.path.dirname(path)
dirname = os.path.split(dirpath)[-1]
self.assertEquals(dirname, 'locator')
def test_find_path_by_name__non_existent_template_fails(self):
def test_find_name__non_existent_template_fails(self):
locator = Locator()
self.assertRaises(IOError, locator.find_path_by_name, search_dirs=[], template_name='doesnt_exist')
self.assertRaises(IOError, locator.find_name, search_dirs=[], template_name='doesnt_exist')
def test_find_path_by_object(self):
def test_find_object(self):
locator = Locator()
obj = SayHello()
actual = locator.find_path_by_object(search_dirs=[], obj=obj, file_name='sample_view.mustache')
actual = locator.find_object(search_dirs=[], obj=obj, file_name='sample_view.mustache')
expected = os.path.abspath(os.path.join(DATA_DIR, 'sample_view.mustache'))
self.assertEquals(actual, expected)
def test_find_path_by_object__none_file_name(self):
def test_find_object__none_file_name(self):
locator = Locator()
obj = SayHello()
actual = locator.find_path_by_object(search_dirs=[], obj=obj)
actual = locator.find_object(search_dirs=[], obj=obj)
expected = os.path.abspath(os.path.join(DATA_DIR, 'say_hello.mustache'))
self.assertEquals(actual, expected)
def test_find_path_by_object__none_object_directory(self):
def test_find_object__none_object_directory(self):
locator = Locator()
obj = None
self.assertEquals(None, locator.get_object_directory(obj))
actual = locator.find_path_by_object(search_dirs=[DATA_DIR], obj=obj, file_name='say_hello.mustache')
actual = locator.find_object(search_dirs=[DATA_DIR], obj=obj, file_name='say_hello.mustache')
expected = os.path.join(DATA_DIR, 'say_hello.mustache')
self.assertEquals(actual, expected)
......
......@@ -322,9 +322,9 @@ class TemplateSpecTests(unittest.TestCase):
view.template_extension = 'txt'
self._assert_template_location(view, (None, 'sample_view.txt'))
def test_get_template_path__with_directory(self):
def test_find__with_directory(self):
"""
Test get_template_path() with a view that has a directory specified.
Test _find() with a view that has a directory specified.
"""
locator = self._make_locator()
......@@ -333,14 +333,14 @@ class TemplateSpecTests(unittest.TestCase):
view.template_rel_path = 'foo/bar.txt'
self.assertTrue(locator.get_relative_template_location(view)[0] is not None)
actual = locator.get_template_path(view)
actual = locator._find(view)
expected = os.path.abspath(os.path.join(DATA_DIR, 'foo/bar.txt'))
self.assertEquals(actual, expected)
def test_get_template_path__without_directory(self):
def test_find__without_directory(self):
"""
Test get_template_path() with a view that doesn't have a directory specified.
Test _find() with a view that doesn't have a directory specified.
"""
locator = self._make_locator()
......@@ -348,7 +348,7 @@ class TemplateSpecTests(unittest.TestCase):
view = SampleView()
self.assertTrue(locator.get_relative_template_location(view)[0] is None)
actual = locator.get_template_path(view)
actual = locator._find(view)
expected = os.path.abspath(os.path.join(DATA_DIR, 'sample_view.mustache'))
self.assertEquals(actual, expected)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment