1
import sqlalchemy
2
import sqlalchemy.orm
3
import config
4
import datetime
5
6
7
engine = sqlalchemy.create_engine(config.URI)
8
metadata = sqlalchemy.MetaData(bind=engine)
9
session = sqlalchemy.orm.scoped_session(sqlalchemy.orm.sessionmaker(bind=engine))
10
query = session.query_property()
11
12
13
class Model(object):
14
    query = query
15
16
def tableFactory(tablename):
17
    """Return a class type"""
18
    class LoadedTable(Model):
19
        pass
20
21
    LoadedTable.__name__ = tablename
22
    return LoadedTable
23
24
25
class Logger:
26
    def __init__(self, tablename):
27
        """This method loads a table from the database and maps it to LoadedTable class.
28
        The loaded table is available through the "lt" data member."""
29
        table = sqlalchemy.Table(tablename, metadata, autoload=True)
30
        self.lt = tableFactory(tablename)
31
        sqlalchemy.orm.mapper(self.lt, table)