Source

pyconjp2011 / models / pyconjp2011 / models / commands.py

Full commit
from argparse import ArgumentParser
from sqlalchemy import create_engine
from sqlalchemy.orm.exc import NoResultFound
from . import *

def cmd_create(args):
    url = args.url
    engine = create_engine(url)
    engine.echo = args.echo
    init_db(engine)
    create_all()
    for name in (u"g1", u"g2"):
        try:
            DBSession.query(Group).filter_by(group_name=name).one()
        except NoResultFound:
            g = Group(group_name=name)
            DBSession.add(g)
    import transaction
    transaction.commit()

parser = ArgumentParser()
subparsers = parser.add_subparsers()
create_parser = subparsers.add_parser('create')
create_parser.add_argument('-u', '--url', default="sqlite:///")
create_parser.add_argument('-e', '--echo', action="store_true")
create_parser.set_defaults(func=cmd_create)

def main():
    args = parser.parse_args()
    if hasattr(args, "func"):
        args.func(args)