view bitsyblog/factory.py @ 73:e6055bf127eb

fix details necessary to make tweeting work...muahaha
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 08 Jul 2010 11:13:08 -0700
parents 0c98d1c2c6df
children 51b49bc484ff
line wrap: on
line source

from bitsyauth import BitsyAuth
from bitsyblog import BitsyBlog, BitsierBlog
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):
    """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:
                bitsyblog_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"""
    bitsyblog_args, handler_args = get_args(app_conf)
    app = BitsyBlog(bitsyblog_args, handler_args)
    secret = app_conf.get('secret', 'secret')
    return BitsyAuth(HTTPExceptionHandler(app), 
                     global_conf,
                     app.passwords,
                     app.newuser,
                     '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
    app = BitsierBlog(bitsyblog_args, handler_args)
    app.user = user
    secret = app_conf.get('secret', 'secret')
    auth = BitsyAuth(HTTPExceptionHandler(app),
                     global_conf,
                     app.passwords,
                     newuser=None,
                     site=app_conf.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