<s> import re <EOL> from zope . sqlalchemy import ZopeTransactionExtension <EOL> from sqlalchemy import Column , Integer , DateTime , engine_from_config <EOL> from sqlalchemy . interfaces import PoolListener <EOL> from sqlalchemy . exc import DisconnectionError <EOL> from sqlalchemy . sql . expression import func , asc , desc <EOL> from sqlalchemy . orm import scoped_session , sessionmaker , joinedload <EOL> from sqlalchemy . ext . declarative import declarative_base , declared_attr <EOL> class ModelError ( Exception ) : <EOL> def __init__ ( self , errors ) : <EOL> super ( ModelError , self ) . __init__ ( '<STR_LIT:\n>' . join ( errors ) ) <EOL> self . errors = errors <EOL> class _Base ( object ) : <EOL> @ declared_attr <EOL> def __tablename__ ( cls ) : <EOL> s1 = re . sub ( '<STR_LIT>' , r'<STR_LIT>' , cls . __name__ ) <EOL> return re . sub ( '<STR_LIT>' , r'<STR_LIT>' , s1 ) . lower ( ) <EOL> __table_args__ = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:utf8>' <EOL> } <EOL> id = Column ( Integer , primary_key = True ) <EOL> created_at = Column ( DateTime , default = func . now ( ) ) <EOL> @ classmethod <EOL> def by_id ( cls , session , id ) : <EOL> return cls . first ( session , where = ( cls . id == id , ) ) <EOL> @ classmethod <EOL> def find ( cls , session , join = None , where = None , order_by = None , limit = None , <EOL> offset = None , count = None ) : <EOL> if count : <EOL> offset = limit = order_by = None <EOL> qry = cls . build_query ( session , join , where , order_by , limit , <EOL> offset , count ) <EOL> return qry . scalar ( ) if count is not None else qry . all ( ) <EOL> @ classmethod <EOL> def first ( cls , session , join = None , where = None , order_by = None ) : <EOL> return cls . build_query ( session , join , where , order_by ) . first ( ) <EOL> @ classmethod <EOL> def all ( cls , session , page_size = <NUM_LIT:1000> , order_by = None ) : <EOL> offset = <NUM_LIT:0> <EOL> order_by = order_by or cls . id <EOL> while True : <EOL> page = cls . find ( session , order_by = order_by , <EOL> limit = page_size , offset = offset ) <EOL> for m in page : <EOL> yield m <EOL> session . flush ( ) <EOL> if len ( page ) != page_size : <EOL> raise StopIteration ( ) <EOL> offset += page_size <EOL> @ classmethod <EOL> def build_query ( cls , session , join = None , where = None , order_by = None , <EOL> limit = None , offset = None , count = None ) : <EOL> if count is not None : <EOL> query = session . query ( func . count ( count ) ) . select_from ( cls ) <EOL> else : <EOL> query = session . query ( cls ) <EOL> if join : <EOL> if isinstance ( join , ( list , tuple ) ) : <EOL> for j in join : <EOL> query = query . join ( j ) <EOL> else : <EOL> query = query . join ( join ) <EOL> if where : <EOL> for filter in where : <EOL> query = query . filter ( filter ) <EOL> if order_by is not None : <EOL> if isinstance ( order_by , ( list , tuple ) ) : <EOL> query = query . order_by ( * order_by ) <EOL> else : <EOL> query = query . order_by ( order_by ) <EOL> if limit : <EOL> query = query . limit ( limit ) <EOL> if offset : <EOL> query = query . offset ( offset ) <EOL> return query <EOL> def validate ( self , session ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> class Database ( object ) : <EOL> databases = { } <EOL> @ classmethod <EOL> def register ( cls , name ) : <EOL> if name not in cls . databases : <EOL> cls . databases [ name ] = declarative_base ( cls = _Base ) <EOL> return cls . databases [ name ] <EOL> @ classmethod <EOL> def get ( cls , name ) : <EOL> return cls . databases [ name ] <EOL> class SessionFactory ( object ) : <EOL> sessions = { } <EOL> @ classmethod <EOL> def register ( cls , name , scoped ) : <EOL> if scoped : <EOL> cls . sessions [ name ] = scoped_session ( sessionmaker ( <EOL> extension = ZopeTransactionExtension ( ) ) ) <EOL> else : <EOL> cls . sessions [ name ] = sessionmaker ( ) <EOL> return cls . sessions [ name ] <EOL> @ classmethod <EOL> def get ( cls , name ) : <EOL> return cls . sessions [ name ] <EOL> def create_engine ( db_name , settings , prefix = '<STR_LIT>' , scoped = False ) : <EOL> engine = engine_from_config ( settings , prefix ) <EOL> DBSession = SessionFactory . register ( db_name , scoped ) <EOL> DBSession . configure ( bind = engine ) <EOL> Database . get ( db_name ) . metadata . bind = engine <EOL> return engine <EOL> def dispose_engine ( db_name ) : <EOL> Database . get ( db_name ) . metadata . bind . dispose ( ) </s>
