changeset 10:ba2355d57998

finish baseline abstract factory implementation
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 27 May 2011 06:40:15 -0700
parents ff634cc2e62b
children ff272dcd5cd8
files pyloader/factory.py
diffstat 1 files changed, 36 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/pyloader/factory.py	Thu May 26 18:14:56 2011 -0700
+++ b/pyloader/factory.py	Fri May 27 06:40:15 2011 -0700
@@ -13,13 +13,22 @@
 
     def __init__(self, config=None, main=''):
         self.main = main  # main section
+        self.configure(config or {})
+
+    def configure(self, config):
+        """load a new configuration"""
+        # TODO: this should really be a configuration update.  If you keep
+        # track of all "apps" and their parents (i.e. as a ADG)
+        # you should be able to update only relevent apps
+        self.config = config
         self.seen = set() # already seen apps to note cyclic dependencies
         self.parsed = {}  # instantiated apps
-        self.load(config or {})
 
-    def load(self, config, name=None):
+    def load(self, name=None):
+        """load an object"""
+        
         name = name or self.main # load main section by default
-        assert name in config, "'%s' not found in config"
+        assert name in self.config, "'%s' not found in configuration"
         if name in self.parsed:
             return self.parsed[name]
         if name in self.seen:
@@ -27,25 +36,45 @@
         self.seen.add(name)
 
         # get section
-        section = config['name']
+        section = config[name]
         assert 'path' in section
 
         # load object
         obj = loader.load(section['path'])
 
-        # interpolate arguments
+        # get the object's arguments (if any)
         args = section.get('args', None)
         kwargs = section.get('kwargs', None)
+
+        # if args and kwargs aren't there, you're done!
+        if args is None and kwargs is None:
+            self.parsed[name] = obj
+            return obj
+
+        # interpolate arguments
         if args:
             args = [self.iterpolate(arg) for arg in args]
         if kwargs:
             kwargs = dict([(key, self.interpolate(value))
                            for key, value in kwargs.items()])
-        
-        # if args and kwargs aren't there, you're done!
+
+        # invoke
+        self.parsed[name] = obj(*args, **kwargs)
+        return self.parsed[name]
 
     def interpolate(self, value):
 
         # only interpolate strings
         if not isinstance(value, basestring):
             return value
+
+        if value.startswith(self.delimeters[0]) and value.endswith(self.delimeters[1]):
+            value = value[len(self.delimeters[0]):-len(self.delimeters[1])]
+            if value in self.config:
+                return self.load(value)
+            elif value == 'self':
+                # reference this factory
+                # tricky and dangerous
+                return self
+        return value
+