Mercurial > hg > pyloader
changeset 15:0bea5297c156
introduce a more betterer test
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 27 May 2011 18:56:00 -0700 |
parents | 30979d82c95d |
children | edecb6fbd5a7 |
files | pyloader/factory.py tests/objects.py tests/test_factory.txt |
diffstat | 3 files changed, 31 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/pyloader/factory.py Fri May 27 18:43:14 2011 -0700 +++ b/pyloader/factory.py Fri May 27 18:56:00 2011 -0700 @@ -3,6 +3,9 @@ """ import loader +import os + +__all__ = ['CircularReferenceError', 'PyFactory', 'IniFactory'] class CircularReferenceError(Exception): """factory has detected a circular reference""" @@ -78,3 +81,10 @@ return self.load(value) return value +class IniFactory(PyFactory): + + def __init__(self, inifile, main=''): + assert os.path.exists(inifile), "File not found: %s" % inifile + + def read(self): + """reads configuration from an .ini file"""
--- a/tests/objects.py Fri May 27 18:43:14 2011 -0700 +++ b/tests/objects.py Fri May 27 18:56:00 2011 -0700 @@ -5,6 +5,16 @@ def stringsort(*strings): return ''.join(sorted(''.join(strings))) +class StringMunge(object): + def __init__(self, prefix, delimeter=':', callback=None): + self.delimeter = delimeter + self.prefix = prefix + self.callback = callback + def __call__(self, string): + if self.callback: + string = self.callback(string) + return self.prefix + self.delimeter + string + class Foo(object): def __init__(self, a, b): self.factor = a*100 + b
--- a/tests/test_factory.txt Fri May 27 18:43:14 2011 -0700 +++ b/tests/test_factory.txt Fri May 27 18:56:00 2011 -0700 @@ -5,6 +5,7 @@ >>> import os >>> from pyloader.factory import PyFactory + >>> objects = os.path.join(here, 'objects.py') Construct a factory:: @@ -12,7 +13,7 @@ Make a configuration:: - >>> config = {'': {'path': os.path.join(here, 'objects.py:stringsort'), 'args': ['foo', 'bar']}} + >>> config = {'': {'path': objects + ':stringsort', 'args': ['foo', 'bar']}} Load it:: @@ -22,3 +23,12 @@ >>> factory.load() 'abfoor' + +Now lets try something better!:: + + >>> config = {'': {'path': objects + ':StringMunge', 'kwargs': {'prefix': 'PRE', 'callback': '%(callback)s'}}, + ... 'callback': {'path': objects + ':stringsort'} } + >>> factory.configure(config) + >>> obj = factory.load() + >>> obj('foobar') + 'PRE:abfoor'