changeset 80:51b49bc484ff

partial refactor of factories; really, i dont know what im doing
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 14 Dec 2010 21:53:10 -0800
parents 4df927b0d847
children 77aaa99c8221
files bitsyblog/bitsyblog.py bitsyblog/factory.py
diffstat 2 files changed, 31 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/bitsyblog/bitsyblog.py	Sat Oct 23 16:54:36 2010 -0700
+++ b/bitsyblog/bitsyblog.py	Tue Dec 14 21:53:10 2010 -0800
@@ -4,8 +4,6 @@
 else can claim to be
 """
 
-### global variables
-
 ### imports
 
 import dateutil.parser
@@ -58,7 +56,6 @@
                  'post_handlers': '' # post handlers
                  }
 
-
     cooked_bodies = {}
 
     def __init__(self, kw, handler_args):
@@ -788,6 +785,13 @@
 class BitsierBlog(BitsyBlog):
     """single user version of bitsyblog"""
 
+    defaults = BitsyBlog.defaults.copy()
+    defaults['user'] = None
+
+    def __init__(self, kw, handler_args):
+        BitsyBlog.__init__(self, kw, handler_args)
+        assert self.user, 'BitsierBlog: no user given'
+
     def get(self, request):
 
         ### user space
--- a/bitsyblog/factory.py	Sat Oct 23 16:54:36 2010 -0700
+++ b/bitsyblog/factory.py	Tue Dec 14 21:53:10 2010 -0800
@@ -3,32 +3,27 @@
 from getpass import getpass 
 from paste.httpexceptions import HTTPExceptionHandler
 
-# accepted configuration keys, e.g. 'bitsyblog.file_dir'
-config = set(BitsyBlog.defaults.keys())
-# config = set(['file_dir', 
-#               'date_format', 
-#               'subject', 
-#               'n_links', 
-#               'help_file', 
-#               'header', 
-#               'template_directories', 
-#               'feed_items',
-#               ])
+def get_args(app_conf, app=BitsyBlog):
+    """return arguments for bitsyblog and its handlers"""
 
-def get_args(app_conf):
-    """return arguments for bitsyblog and its handlers"""
-    key_str = 'bitsyblog.'
+    # accepted configuration keys, e.g. 'bitsyblog.file_dir'
+    config = set(app.defaults.keys())
+    
+    key_str = app_conf.get('namespace', 'bitsyblog.')
     bitsyblog_args = {}
     handler_args = {}
     for key, value in app_conf.items():
         if key.startswith(key_str):
-            key = key.split(key_str, 1)[-1]
+            if key_str:
+                key = key.split(key_str, 1)[-1]
             if key in config:
                 bitsyblog_args[key] = value
-        else:
-            if '.' in key:
-                section, key = key.split('.', 1)
-                handler_args.setdefault(section, {})[key] = value
+                continue
+
+        # handler args
+        if '.' in key:
+            section, key = key.split('.', 1)
+            handler_args.setdefault(section, {})[key] = value
     return bitsyblog_args, handler_args
 
 def factory(global_conf, **app_conf):
@@ -43,21 +38,23 @@
                      'bitsyblog', 
                      secret)
 
-
-def bitsierfactory(global_conf, **app_conf):
-    """make single-user bitsyblog"""
-    bitsyblog_args, handler_args = get_args(app_conf)
-    user = app_conf['bitsyblog.user'] # ensure this exist
+def basebitsierfactory(global_conf, bitsyblog_args, handler_args):
+    user = bitsyblog_args['user'] # ensure this exists
     app = BitsierBlog(bitsyblog_args, handler_args)
-    app.user = user
-    secret = app_conf.get('secret', 'secret')
+    secret = bitsyblog_args.get('secret', 'secret')
     auth = BitsyAuth(HTTPExceptionHandler(app),
                      global_conf,
                      app.passwords,
                      newuser=None,
-                     site=app_conf.get('site', 'bitsyblog'),
+                     site=bitsyblog_args.get('site', 'bitsyblog'),
                      secret=secret)
     if not user in app.users:
         pw = getpass('Enter password for %s: ' % user)
         app.newuser(user, auth.hash(app.user, pw))
     return auth
+
+def bitsierfactory(global_conf, **app_conf):
+    """make single-user bitsyblog"""
+    bitsyblog_args, handler_args = get_args(app_conf, BitsierBlog)
+    app = basebitsierfactory(global_conf, bitsyblog_args, handler_args)
+    return app