changeset 70:f6a6a4b072e7

complete overhaul to allow event handlers
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 07 Jul 2010 16:18:19 -0700
parents bcc3a59713c3
children 0c98d1c2c6df
files bitsyblog/bitsyblog.py bitsyblog/blog.py bitsyblog/factory.py
diffstat 3 files changed, 42 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/bitsyblog/bitsyblog.py	Wed Jul 07 13:58:21 2010 -0700
+++ b/bitsyblog/bitsyblog.py	Wed Jul 07 16:18:19 2010 -0700
@@ -60,7 +60,7 @@
 
     cooked_bodies = {}
 
-    def __init__(self, **kw):
+    def __init__(self, kw, handler_args):
         for key in self.defaults:
             setattr(self, key, kw.get(key, self.defaults[key]))
         self.n_links = int(self.n_links) # could be a string from the .ini
@@ -73,7 +73,7 @@
         from user import FilespaceUsers
 
         self.users = FilespaceUsers(self.file_dir)
-        self.blog = FileBlog(self.file_dir)
+        self.blog = FileBlog(self, self.file_dir)
         self.cooker = self.restructuredText
         self.feed_items = int(self.feed_items)
 
@@ -105,6 +105,17 @@
         # for BitsyAuth
         self.newuser = self.users.new
 
+        # post handlers
+        self.handlers = [] # handlers for blog post event
+        for entry_point in iter_entry_points('bitsyblog.listeners'):
+          try:
+              handler = entry_point.load()(self, **handler_args.get(entry_point.name, {}))
+              self.handlers.append(handler)
+          except:
+              print 'Cant load entry point %s' entry_point.name 
+              continue
+
+
     ### methods dealing with HTTP
 
     def __call__(self, environ, start_response):
@@ -326,7 +337,12 @@
         # write the file
         now = utils.datestamp(datetime.datetime.now())
         location = self.user_url(request, user, now)
-        self.blog.post(user, now, body, privacy)
+        blog_entry = self.blog.post(user, now, body, privacy)
+
+        # fire event handlers
+        # XXX could be done asynchronously
+        for handler in self.handlers:
+            handler(blog_entry)
 
         # point the user at the post
         return exc.HTTPSeeOther("Post blogged by bitsy", location=location)
--- a/bitsyblog/blog.py	Wed Jul 07 13:58:21 2010 -0700
+++ b/bitsyblog/blog.py	Wed Jul 07 16:18:19 2010 -0700
@@ -47,14 +47,6 @@
 
 class Blog(object):
     """abstract class for a users' blog"""
-    def __init__(self):
-        self.handlers = [] # handlers for blog post event
-        for entry_point in iter_entry_points('bitsyblog.listeners'):
-          try:
-              handler = entry_point.load()
-              self.handlers.append(handler)
-          except:
-              continue
 
     def __call__(self, user, permissions=('public',), number=None):
         return self.blog(user, permissions, number=number)
@@ -87,9 +79,7 @@
 
     def post(self, user, date, text, privacy):
         """post a new blog entry"""
-        entry = BlogEntry(date, body, privacy, user):
-        for handler in self.handlers:
-            handler(entry)
+        return BlogEntry(date, text, privacy, user):
 
     def delete(self, user, datestamp):
         """remove a blog entry"""
@@ -98,7 +88,6 @@
     """a blog that lives on the filesystem"""
 
     def __init__(self, directory):
-        Blog.__init__(self)
         self.directory = directory
 
     def location(self, user, permission, *path):
@@ -159,6 +148,7 @@
     def post(self, user, datestamp, body, privacy):
         blog = file(self.location(user, privacy, datestamp), 'w')
         print >> blog, body
+        return Blog.post(user, datestamp, body, privacy)
 
     def delete(self, user, datestamp):
         for permission in 'public', 'secret', 'private':
--- a/bitsyblog/factory.py	Wed Jul 07 13:58:21 2010 -0700
+++ b/bitsyblog/factory.py	Wed Jul 07 16:18:19 2010 -0700
@@ -11,13 +11,28 @@
               'help_file', 
               'header', 
               'template_directories', 
-              'feed_items'])
+              'feed_items',
+              ])
+
+def get_args(app_conf):
+    """return arguments for bitsyblog and its handlers"""
+    key_str = '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 in config:
+                bitysblog_args[key] = value
+        else:
+            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):
     """make bitsyauth app and wrap it in middleware"""
-    key_str = 'bitsyblog.%s'
-    args = dict([ (key, app_conf[ key_str % key]) for key in config
-                  if app_conf.has_key(key_str % key) ])
+    bitsyblog_args, handler_args = get_args(app_conf)
     app = BitsyBlog(**args)
     secret = app_conf.get('secret', 'secret')
     return BitsyAuth(HTTPExceptionHandler(app), 
@@ -30,10 +45,8 @@
 
 def bitsierfactory(global_conf, **app_conf):
     """make single-user bitsyblog"""
-    key_str = 'bitsyblog.%s'
-    args = dict([ (key, app_conf[ key_str % key]) for key in config
-                  if app_conf.has_key(key_str % key) ])
-    user = app_conf['bitsyblog.user']
+    bitsyblog_args, handler_args = get_args(app_conf)
+    user = app_conf['bitsyblog.user'] # ensure this exist
     app = BitsierBlog(**args)
     app.user = user
     secret = app_conf.get('secret', 'secret')