Dan Colish  committed b2fc9a6

initial refactor of db and app separation, add configuration, modifiy search

  • Participants
  • Parent commits d178439
  • Branches lodgeit

Comments (0)

Files changed (11)

File conf/lodgeit.ini

+dburi = sqlite:///lodgeit.db
+secret_key = no secret key

File lodgeit/

     :copyright: 2007 by Armin Ronacher.
     :license: BSD
+from lodgeit.utils import config
 from lodgeit.application import make_app

File lodgeit/

 from werkzeug import SharedDataMiddleware, ClosingIterator
 from werkzeug.exceptions import HTTPException, NotFound
-from sqlalchemy import create_engine
-from lodgeit import i18n, local
-from lodgeit.local import ctx, _local_manager
+from lodgeit import i18n, config
+from lodgeit.local import ctx, local_manager
 from lodgeit.urls import urlmap
 from lodgeit.utils import COOKIE_NAME, Request, jinja_environment
 from lodgeit.database import db
 class LodgeIt(object):
     """The WSGI Application"""
-    def __init__(self, dburi, secret_key):
-        self.secret_key = secret_key
+    def __init__(self):
+        self.secret_key = config.get('lodgeit', 'secret_key')
         #: bind metadata, create engine and create all tables
-        self.engine = engine = create_engine(dburi, convert_unicode=True)
+        self.engine = engine = db.engine
+        #TODO:dc: make this part of the database code
         db.metadata.bind = engine
         db.metadata.create_all(engine, [Paste.__table__])
-def make_app(dburi, secret_key, debug=False, shell=False):
+def make_app(debug=False, shell=False):
     """Apply the used middlewares and create the application."""
     static_path = os.path.join(os.path.dirname(__file__), 'static')
-    app = LodgeIt(dburi, secret_key)
+    app = LodgeIt()
     if debug:
         app.engine.echo = True
     if not shell:
         # we don't need access to the shared data middleware in shell mode
         app = SharedDataMiddleware(app, {
             '/static': static_path})
-    return _local_manager.make_middleware(app)
+    return local_manager.make_middleware(app)

File lodgeit/controllers/

 from lodgeit.i18n import list_languages as i18n_list_languages
 from lodgeit.utils import render_to_response, url_for
 from lodgeit.models import Paste
-from lodgeit.database import session
+from lodgeit.database import db
 from lodgeit.lib.highlighting import list_languages, STYLES, get_style
 from lodgeit.lib.pagination import generate_pagination
 from import index, search
             if code and language and not error:
                 paste = Paste(code, language, parent_id, req.user_hash,
                               'private' in req.form)
-                session.add(paste)
-                session.commit()
+                db.session.add(paste)
+                db.session.commit()
                 local.request.session['language'] = language
                 return redirect(url_for('pastes/show_paste',

File lodgeit/

 import sys
 from types import ModuleType
 import sqlalchemy
-from sqlalchemy import MetaData, orm
+from sqlalchemy import MetaData, orm, create_engine
+from sqlalchemy.orm import mapper, scoped_session, sessionmaker
 from sqlalchemy.ext.declarative import declarative_base
-from lodgeit.local import application, _local_manager
+from lodgeit import config
+from lodgeit.local import local_manager
 metadata = MetaData()
-def session_factory():
-    options = {'autoflush': True, 'autocommit': False}
-    return orm.create_session(application.engine, **options)
-session = orm.scoped_session(session_factory,
-                             scopefunc=_local_manager.get_ident)
+engine = create_engine(config.get('lodgeit', 'dburi'), convert_unicode=True)
+session = sessionmaker(bind=engine,
+                       autoflush=True,
+                       autocommit=False)
+ScopedSession = scoped_session(session, scopefunc=local_manager.get_ident)
 class ModelBase(object):
 # configure the declarative base
 Model = declarative_base(name='Model', cls=ModelBase,
-                         mapper=orm.mapper, metadata=metadata)
-ModelBase.query = session.query_property()
+                         mapper=mapper, metadata=metadata)
+ModelBase.query = ScopedSession.query_property()
 def _make_module():
     db = ModuleType('db')
+    #XXX:dc: stop rexporting all of this stuff
     for mod in sqlalchemy, orm:
         for key, value in mod.__dict__.iteritems():
             if key in mod.__all__:
                 setattr(db, key, value)
-    db.session = session
+    db.engine = engine
+    db.session = ScopedSession
     db.metadata = metadata
     db.Model = Model
     db.NoResultFound = orm.exc.NoResultFound

File lodgeit/

 #: context locals
 ctx = Local()
-_local_manager = LocalManager(ctx)
+local_manager = LocalManager(ctx)
 #: local objects
 request = ctx('request')

File lodgeit/

 from whoosh.index import create_in, open_dir
 from whoosh.qparser import QueryParser
+from lodgeit.models import Paste
 ix = None
 paste_schema = Schema(paste_id=ID(stored=True),
+def index_all():
+    global ix
+    build_indexer()
+    writer = ix.writer()
+    paste_doc = lambda paste: {
+        u'paste_id': unicode(paste.paste_id),
+        u'code': paste.code
+        }
+    for paste in Paste.find_all():
+        writer.add_document(**paste_doc(paste))
+    writer.commit()
 def search(query):
     global ix
     schema = ix.schema

File lodgeit/

 # -*- coding: utf-8 -*-
+#TODO:dc: refactor these utils into more reasonable modules
     :copyright: 2007-2008 by Christopher Grebs.
     :license: BSD
+from ConfigParser import ParsingError, ConfigParser
+from functools import partial
+from os.path import abspath, dirname, join as pjoin
+from random import random
 import re
 import time
-from os.path import dirname, join as pjoin
-from random import random
-from functools import partial
+from babel import Locale
+from jinja2 import Environment, FileSystemLoader
 from werkzeug import Request as RequestBase, Response
 from werkzeug.contrib.securecookie import SecureCookie
-from jinja2 import Environment, FileSystemLoader
-from babel import Locale
 from lodgeit import local
 from lodgeit.i18n import get_translations
     def translations(self):
         return get_translations(self.locale)
 def render_template(template_name, **context):
     request = local.request
     return Response(render_template(template_name, **context),
+config = ConfigParser()
+if not'conf/lodgeit.ini')):
+    raise ParsingError("Bad parse")
 from lodgeit.application import make_app
 from lodgeit.database import db
-dburi = 'sqlite:////tmp/lodgeit.db'
-SECRET_KEY = 'no secret key'
 def run_app(app):
     run_simple('localhost', 5000, app,
 action_runserver = script.make_runserver(
-    lambda: make_app(dburi, SECRET_KEY, debug=True),
+    lambda: make_app(debug=True),
 action_shell = script.make_shell(
     lambda: {
-        'app': make_app(dburi, SECRET_KEY, False, True),
+        'app': make_app(False, True),
         'local': local,
         'db': db,
         'run_app': run_app
-        'whoosh']
+        'whoosh'],
+      entry_points={
+        'console_scripts': [
+            '',
+            ],
+        }

File tests/utilities/

 from lodgeit.application import db, make_app
 from lodgeit.models import Paste
-foo = make_app('sqlite://', 'NONE', False, True)
+foo = make_app(False, True)
 def setup():