<s> """<STR_LIT>""" <EOL> from toolkit_library . string_util import StringUtil <EOL> from sqlalchemy import create_engine , Column , Integer , ForeignKey , String , DateTime , event <EOL> from sqlalchemy . orm import scoped_session , sessionmaker , relationship , backref <EOL> from sqlalchemy . schema import Table <EOL> from sqlalchemy . ext . declarative import declarative_base , DeclarativeMeta , _as_declarative <EOL> from extensions import DatabaseExtension , SessionExtension , ModelExtension <EOL> models = list ( ) <EOL> class MyDeclarativeMeta ( DeclarativeMeta ) : <EOL> def __init__ ( cls , classname , bases , attrs ) : <EOL> models . append ( cls ) <EOL> return type . __init__ ( cls , classname , bases , attrs ) <EOL> @ DatabaseExtension . extend <EOL> class Database ( object ) : <EOL> Base = declarative_base ( ) <EOL> @ staticmethod <EOL> def register ( ) : <EOL> for i in range ( len ( models ) ) : <EOL> model = models [ i ] <EOL> if '<STR_LIT>' in model . __dict__ : <EOL> continue <EOL> _as_declarative ( model , model . __name__ , model . __dict__ ) <EOL> event . listen ( model , '<STR_LIT>' , ModelExtension . before_insert_listener ) <EOL> event . listen ( model , '<STR_LIT>' , ModelExtension . before_update_listener ) <EOL> for j in range ( i ) : <EOL> if not models [ j ] in model . __bases__ : <EOL> continue <EOL> parent = models [ j ] <EOL> for grandparent in parent . _many_to_models : <EOL> setattr ( grandparent , model . _readable_names , <EOL> ( lambda parent , model : property ( lambda self : getattr ( self , parent . _readable_names ) <EOL> . filter_by ( real_type = model . _readable_name ) ) ) ( parent , model ) ) <EOL> for grandparent in parent . _one_to_models : <EOL> setattr ( grandparent , model . _readable_name , <EOL> ( lambda parent , model : property ( lambda self : getattr ( self , parent . _readable_name ) <EOL> if getattr ( self , parent . _readable_name ) . real_type == model . _readable_name else None ) ) ( parent , model ) ) <EOL> models [ : ] = [ ] <EOL> def __init__ ( self , connection_string , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in connection_string and '<STR_LIT>' not in connection_string : <EOL> raise ValueError ( """<STR_LIT>""" ) <EOL> self . engine = create_engine ( connection_string , convert_unicode = True , encoding = '<STR_LIT:utf-8>' , ** kwargs ) <EOL> self . session = SessionExtension . extend ( scoped_session ( sessionmaker ( autocommit = False , autoflush = False , bind = self . engine ) ) ) <EOL> @ staticmethod <EOL> def foreign_key ( ref_model , ref_name = None , backref_name = None , one_to_one = False ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( ref_model , str ) : <EOL> ref_model_name = ref_model <EOL> else : <EOL> ref_model_name = ref_model . __name__ <EOL> ref_table_name = StringUtil . camelcase_to_underscore ( ref_model_name ) <EOL> ref_name = ref_name or ref_table_name <EOL> foreign_key = '<STR_LIT>' . format ( ref_name ) <EOL> def ref_table ( cls ) : <EOL> if one_to_one : <EOL> if backref_name : <EOL> cls . _readable_name = backref_name <EOL> if not isinstance ( ref_model , str ) : <EOL> if ref_name : <EOL> ref_model . _readable_name = ref_name <EOL> cls . _one_to_models . append ( ref_model ) <EOL> ref_model . _one_to_models . append ( cls ) <EOL> else : <EOL> if backref_name : <EOL> cls . _readable_names = backref_name <EOL> if not isinstance ( ref_model , str ) : <EOL> if ref_name : <EOL> ref_model . _readable_name = ref_name <EOL> cls . _many_to_models . append ( ref_model ) <EOL> ref_model . _one_to_models . append ( cls ) <EOL> model_name = cls . __name__ <EOL> table_name = cls . _readable_name <EOL> setattr ( cls , foreign_key , Column ( Integer , ForeignKey ( '<STR_LIT>' . format ( ref_table_name ) , ondelete = "<STR_LIT>" ) ) ) <EOL> my_backref_name = backref_name or ( table_name if one_to_one else '<STR_LIT>' . format ( table_name ) ) <EOL> backref_options = dict ( uselist = False ) if one_to_one else dict ( lazy = '<STR_LIT>' ) <EOL> backref_options [ '<STR_LIT>' ] = '<STR_LIT:all>' <EOL> setattr ( cls , ref_name , relationship ( ref_model_name , <EOL> primaryjoin = '<STR_LIT>' . format ( model_name , foreign_key , ref_model_name ) , <EOL> backref = backref ( my_backref_name , ** backref_options ) , remote_side = '<STR_LIT>' . format ( ref_model_name ) ) ) <EOL> return cls <EOL> return ref_table <EOL> @ staticmethod <EOL> def many_to_one ( ref_model , ref_name = None , backref_name = None ) : <EOL> return Database . foreign_key ( ref_model , ref_name , backref_name , False ) <EOL> @ staticmethod <EOL> def one_to_one ( ref_model , ref_name = None , backref_name = None ) : <EOL> return Database . foreign_key ( ref_model , ref_name , backref_name , True ) <EOL> @ staticmethod <EOL> def many_to_many ( ref_model , ref_name = None , backref_name = None , middle_table_name = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( ref_model , str ) : <EOL> ref_model_name = ref_model <EOL> else : <EOL> ref_model_name = ref_model . __name__ <EOL> ref_table_name = StringUtil . camelcase_to_underscore ( ref_model_name ) <EOL> ref_name = ref_name or '<STR_LIT>' . format ( ref_table_name ) <EOL> def ref_table ( cls ) : <EOL> if backref_name : <EOL> cls . _readable_names = backref_name <EOL> if not isinstance ( ref_model , str ) : <EOL> ref_model . _readable_names = ref_name <EOL> cls . _many_to_models . append ( ref_model ) <EOL> ref_model . _many_to_models . append ( cls ) <EOL> table_name = cls . _readable_name <EOL> my_middle_table_name = middle_table_name or '<STR_LIT>' . format ( table_name , ref_table_name ) <EOL> if table_name == ref_table_name : <EOL> left_column_name = '<STR_LIT>' <EOL> right_column_name = '<STR_LIT>' <EOL> else : <EOL> left_column_name = '<STR_LIT>' . format ( table_name ) <EOL> right_column_name = '<STR_LIT>' . format ( ref_table_name ) <EOL> middle_table = Table ( my_middle_table_name , Database . Base . metadata , <EOL> Column ( left_column_name , Integer , ForeignKey ( '<STR_LIT>' . format ( table_name ) , ondelete = "<STR_LIT>" ) , primary_key = True ) , <EOL> Column ( right_column_name , Integer , ForeignKey ( '<STR_LIT>' . format ( ref_table_name ) , ondelete = "<STR_LIT>" ) , primary_key = True ) ) <EOL> my_backref_name = backref_name or '<STR_LIT>' . format ( table_name ) <EOL> parameters = dict ( secondary = middle_table , lazy = '<STR_LIT>' , backref = backref ( my_backref_name , lazy = '<STR_LIT>' ) ) <EOL> if table_name == ref_table_name : <EOL> parameters [ '<STR_LIT>' ] = cls . id == middle_table . c . left_id <EOL> parameters [ '<STR_LIT>' ] = cls . id == middle_table . c . right_id <EOL> setattr ( cls , ref_name , relationship ( ref_model_name , ** parameters ) ) <EOL> return cls <EOL> return ref_table <EOL> class DefaultMeta ( MyDeclarativeMeta ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> bases = list ( bases ) <EOL> if object in bases : <EOL> bases . remove ( object ) <EOL> bases . append ( Database . Base ) <EOL> seen = set ( ) <EOL> bases = tuple ( base for base in bases if not base in seen and not seen . add ( base ) ) <EOL> attrs [ '<STR_LIT>' ] = StringUtil . camelcase_to_underscore ( name ) <EOL> attrs [ '<STR_LIT:id>' ] = Column ( Integer , primary_key = True ) <EOL> attrs [ '<STR_LIT>' ] = Column ( DateTime ) <EOL> attrs [ '<STR_LIT>' ] = Column ( DateTime ) <EOL> attrs [ '<STR_LIT>' ] = attrs [ '<STR_LIT>' ] <EOL> attrs [ '<STR_LIT>' ] = attrs [ '<STR_LIT>' ] + '<STR_LIT:s>' <EOL> attrs [ '<STR_LIT>' ] = attrs [ '<STR_LIT>' ] = [ ] <EOL> if not '<STR_LIT>' in attrs : <EOL> attrs [ '<STR_LIT>' ] = { } <EOL> for base in [ base for base in bases if base in models ] : <EOL> if not hasattr ( base , '<STR_LIT>' ) : <EOL> base . real_type = Column ( '<STR_LIT>' , String ( <NUM_LIT> ) , nullable = False , index = True ) <EOL> base . __mapper_args__ [ '<STR_LIT>' ] = base . real_type <EOL> base . __mapper_args__ [ '<STR_LIT>' ] = base . _readable_name <EOL> attrs [ '<STR_LIT:id>' ] = Column ( Integer , ForeignKey ( '<STR_LIT>' . format ( base . _readable_name ) , ondelete = "<STR_LIT>" ) , primary_key = True ) <EOL> attrs [ '<STR_LIT>' ] [ '<STR_LIT>' ] = attrs [ '<STR_LIT>' ] <EOL> attrs [ '<STR_LIT>' ] [ '<STR_LIT>' ] = attrs [ '<STR_LIT:id>' ] == base . id <EOL> return MyDeclarativeMeta . __new__ ( cls , name , bases , attrs ) <EOL> @ staticmethod <EOL> def MetaBuilder ( * models ) : <EOL> """<STR_LIT>""" <EOL> class InnerMeta ( Database . DefaultMeta ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> bases = list ( bases ) <EOL> bases . extend ( models ) <EOL> seen = set ( ) <EOL> bases = tuple ( base for base in bases if not base in seen and not seen . add ( base ) ) <EOL> return Database . DefaultMeta . __new__ ( cls , name , bases , attrs ) <EOL> return InnerMeta </s>
