def test_get_object_directory(self): locator = Locator() obj = SayHello() actual = locator.get_object_directory(obj) self._assert_paths(actual, DATA_DIR)
def test_get_object_directory__not_hasattr_module(self): locator = Locator() obj = datetime(2000, 1, 1) self.assertFalse(hasattr(obj, '__module__')) self.assertEqual(locator.get_object_directory(obj), None) self.assertFalse(hasattr(None, '__module__')) self.assertEqual(locator.get_object_directory(None), None)
def test_find_object__none_file_name(self): locator = Locator() obj = SayHello() actual = locator.find_object(search_dirs=[], obj=obj) expected = os.path.join(DATA_DIR, 'say_hello.mustache') self.assertEqual(actual, expected)
def test_find_object(self): locator = Locator() obj = SayHello() actual = locator.find_object(search_dirs=[], obj=obj, file_name='sample_view.mustache') expected = os.path.join(DATA_DIR, 'sample_view.mustache') self._assert_paths(actual, expected)
def test_find_object__none_object_directory(self): locator = Locator() obj = None self.assertEqual(None, locator.get_object_directory(obj)) 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.assertEqual(actual, expected)
def test_make_template_name(self): """ Test make_template_name(). """ locator = Locator() class FooBar(object): pass foo = FooBar() self.assertEqual(locator.make_template_name(foo), 'foo_bar')
def test_find_name__precedence(self): """ Test the order in which find_name() searches directories. """ locator = Locator() dir1 = DATA_DIR dir2 = os.path.join(DATA_DIR, 'locator') 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_name(search_dirs=[dir2, dir1], template_name='duplicate') dirpath = os.path.dirname(path) dirname = os.path.split(dirpath)[-1] self.assertEqual(dirname, 'locator')
def test_make_file_name(self): locator = Locator() locator.template_extension = 'bar' self.assertEqual(locator.make_file_name('foo'), 'foo.bar') locator.template_extension = False self.assertEqual(locator.make_file_name('foo'), 'foo') locator.template_extension = '' self.assertEqual(locator.make_file_name('foo'), 'foo.')
def __init__(self, file_encoding=None, extension=None, to_unicode=None, search_dirs=None): """ Construct a template loader instance. Arguments: extension: the template file extension. Pass False for no extension (i.e. 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. search_dirs: the list of directories in which to search when loading a template by name or file name. Defaults to the package default. to_unicode: the function to use when converting strings of type str to unicode. The function should have the signature: to_unicode(s, encoding=None) It should accept a string of type str and an optional encoding name and return a string of type unicode. Defaults to calling Python's built-in function unicode() using the package string encoding and decode errors defaults. """ if extension is None: extension = defaults.TEMPLATE_EXTENSION if file_encoding is None: file_encoding = defaults.FILE_ENCODING if search_dirs is None: search_dirs = defaults.SEARCH_DIRS if to_unicode is None: to_unicode = default_unicode self.extension = extension self.file_encoding = file_encoding # TODO: unit test setting this attribute. self.search_dirs = search_dirs self.to_unicode = to_unicode self.locator = Locator(extension=self.extension)
def test_find_name__using_list_of_paths(self): locator = Locator() path = locator.find_name(search_dirs=[EXAMPLES_DIR, 'doesnt_exist'], template_name='simple') self.assertTrue(path)
def test_find_name(self): locator = Locator() path = locator.find_name(search_dirs=[EXAMPLES_DIR], template_name='simple') self.assertEqual(os.path.basename(path), 'simple.mustache')
def test_make_file_name__template_extension_argument(self): locator = Locator() self.assertEqual(locator.make_file_name('foo', template_extension='bar'), 'foo.bar')
class Loader(object): """ Loads the template associated to a name or user-defined object. """ def __init__(self, file_encoding=None, extension=None, to_unicode=None, search_dirs=None): """ Construct a template loader instance. Arguments: extension: the template file extension. Pass False for no extension (i.e. 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. search_dirs: the list of directories in which to search when loading a template by name or file name. Defaults to the package default. to_unicode: the function to use when converting strings of type str to unicode. The function should have the signature: to_unicode(s, encoding=None) It should accept a string of type str and an optional encoding name and return a string of type unicode. Defaults to calling Python's built-in function unicode() using the package string encoding and decode errors defaults. """ if extension is None: extension = defaults.TEMPLATE_EXTENSION if file_encoding is None: file_encoding = defaults.FILE_ENCODING if search_dirs is None: search_dirs = defaults.SEARCH_DIRS if to_unicode is None: to_unicode = default_unicode self.extension = extension self.file_encoding = file_encoding # TODO: unit test setting this attribute. self.search_dirs = search_dirs self.to_unicode = to_unicode self.locator = Locator(extension=self.extension) def unicode(self, s, encoding=None): """ Convert a string to unicode using the given encoding, and return it. This function uses the underlying to_unicode attribute. Arguments: s: a basestring instance to convert to unicode. Unlike Python's built-in unicode() function, it is okay to pass unicode strings to this function. (Passing a unicode string to Python's unicode() with the encoding argument throws the error, "TypeError: decoding Unicode is not supported.") encoding: the encoding to pass to the to_unicode attribute. Defaults to None. """ if isinstance(s, unicode): return unicode(s) return self.to_unicode(s, encoding) def read(self, path, encoding=None): """ Read the template at the given path, and return it as a unicode string. """ b = common.read(path) if encoding is None: encoding = self.file_encoding return self.unicode(b, encoding) # TODO: unit-test this method. def load_name(self, name): """ Find and return the template with the given name. Arguments: name: the name of the template. search_dirs: the list of directories in which to search. """ path = self.locator.find_name(name, self.search_dirs) return self.read(path) # TODO: unit-test this method. def load_object(self, obj): """ Find and return the template associated to the given object. Arguments: obj: an instance of a user-defined class. search_dirs: the list of directories in which to search. """ path = self.locator.find_object(obj, self.search_dirs) return self.read(path)