diff decoupage/web.py @ 59:07cf168aa98c

make formats classes that can take arguments
author Jeff Hammel <jhammel@mozilla.com>
date Sun, 21 Nov 2010 13:23:34 -0800
parents 0cc1af24602b
children f5ca54558292
line wrap: on
line diff
--- a/decoupage/web.py	Thu Nov 18 08:36:07 2010 -0800
+++ b/decoupage/web.py	Sun Nov 21 13:23:34 2010 -0800
@@ -54,16 +54,25 @@
         self.fileserver = FileApp # XXX still used?!?
 
         # pluggable formats
+        s = 'decoupage.format.'
+        _format_args = [ (i.split(s, 1)[-1], j) for i, j in app_conf.items()
+                         if i.startswith(s) ]
+        format_args = {}
+        for i, j in _format_args:
+            assert i.count('.') == 1, 'Illegal string or something'
+            format_name, var_name = i.split('.')
+            format_args.setdefault(format_name, {})[var_name] = j
         self.formats = {}
         for _format in iter_entry_points('decoupage.formats'):
             try:
-                function = _format.load()
+                _cls = _format.load()
+                _instance = _cls(self, **format_args.get(_format.name, {}))
             except Exception, e:
                 # record the error, but persist
                 print >> sys.stderr, "Couldn't load format: %s" % _format
                 print >> sys.stderr, e
                 continue
-            self.formats[_format.name] = function
+            self.formats[_format.name] = _instance
         
         # pluggable index data formatters
         self.formatters = {}
@@ -170,8 +179,8 @@
         if 'format' in request.GET:
             format_name = request.GET['format']
             if format_name in self.formats:
-                function = self.formats[format_name]
-                content_type, body = function(data)
+                _format = self.formats[format_name]
+                content_type, body = _format(request, data)
                 return Response(content_type=content_type, body=body)
 
         # render the template