<s> """<STR_LIT>""" <EOL> from . declarative import declarative_base as _declarative_base <EOL> from . declarative . base import _DeferredMapperConfig <EOL> from . . sql import and_ <EOL> from . . schema import ForeignKeyConstraint <EOL> from . . orm import relationship , backref , interfaces <EOL> from . . import util <EOL> def classname_for_table ( base , tablename , table ) : <EOL> """<STR_LIT>""" <EOL> return str ( tablename ) <EOL> def name_for_scalar_relationship ( base , local_cls , referred_cls , constraint ) : <EOL> """<STR_LIT>""" <EOL> return referred_cls . __name__ . lower ( ) <EOL> def name_for_collection_relationship ( <EOL> base , local_cls , referred_cls , constraint ) : <EOL> """<STR_LIT>""" <EOL> return referred_cls . __name__ . lower ( ) + "<STR_LIT>" <EOL> def generate_relationship ( <EOL> base , direction , return_fn , attrname , local_cls , referred_cls , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if return_fn is backref : <EOL> return return_fn ( attrname , ** kw ) <EOL> elif return_fn is relationship : <EOL> return return_fn ( referred_cls , ** kw ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % return_fn ) <EOL> class AutomapBase ( object ) : <EOL> """<STR_LIT>""" <EOL> __abstract__ = True <EOL> classes = None <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def prepare ( <EOL> cls , <EOL> engine = None , <EOL> reflect = False , <EOL> classname_for_table = classname_for_table , <EOL> collection_class = list , <EOL> name_for_scalar_relationship = name_for_scalar_relationship , <EOL> name_for_collection_relationship = name_for_collection_relationship , <EOL> generate_relationship = generate_relationship ) : <EOL> """<STR_LIT>""" <EOL> if reflect : <EOL> cls . metadata . reflect ( <EOL> engine , <EOL> extend_existing = True , <EOL> autoload_replace = False <EOL> ) <EOL> table_to_map_config = dict ( <EOL> ( m . local_table , m ) <EOL> for m in _DeferredMapperConfig . <EOL> classes_for_base ( cls , sort = False ) <EOL> ) <EOL> many_to_many = [ ] <EOL> for table in cls . metadata . tables . values ( ) : <EOL> lcl_m2m , rem_m2m , m2m_const = _is_many_to_many ( cls , table ) <EOL> if lcl_m2m is not None : <EOL> many_to_many . append ( ( lcl_m2m , rem_m2m , m2m_const , table ) ) <EOL> elif not table . primary_key : <EOL> continue <EOL> elif table not in table_to_map_config : <EOL> mapped_cls = type ( <EOL> classname_for_table ( cls , table . name , table ) , <EOL> ( cls , ) , <EOL> { "<STR_LIT>" : table } <EOL> ) <EOL> map_config = _DeferredMapperConfig . config_for_cls ( mapped_cls ) <EOL> cls . classes [ map_config . cls . __name__ ] = mapped_cls <EOL> table_to_map_config [ table ] = map_config <EOL> for map_config in table_to_map_config . values ( ) : <EOL> _relationships_for_fks ( cls , <EOL> map_config , <EOL> table_to_map_config , <EOL> collection_class , <EOL> name_for_scalar_relationship , <EOL> name_for_collection_relationship , <EOL> generate_relationship ) <EOL> for lcl_m2m , rem_m2m , m2m_const , table in many_to_many : <EOL> _m2m_relationship ( cls , lcl_m2m , rem_m2m , m2m_const , table , <EOL> table_to_map_config , <EOL> collection_class , <EOL> name_for_scalar_relationship , <EOL> name_for_collection_relationship , <EOL> generate_relationship ) <EOL> for map_config in _DeferredMapperConfig . classes_for_base ( cls ) : <EOL> map_config . map ( ) <EOL> _sa_decl_prepare = True <EOL> """<STR_LIT>""" <EOL> def automap_base ( declarative_base = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if declarative_base is None : <EOL> Base = _declarative_base ( ** kw ) <EOL> else : <EOL> Base = declarative_base <EOL> return type ( <EOL> Base . __name__ , <EOL> ( AutomapBase , Base , ) , <EOL> { "<STR_LIT>" : True , "<STR_LIT>" : util . Properties ( { } ) } <EOL> ) <EOL> def _is_many_to_many ( automap_base , table ) : <EOL> fk_constraints = [ const for const in table . constraints <EOL> if isinstance ( const , ForeignKeyConstraint ) ] <EOL> if len ( fk_constraints ) != <NUM_LIT:2> : <EOL> return None , None , None <EOL> cols = sum ( <EOL> [ [ fk . parent for fk in fk_constraint . elements ] <EOL> for fk_constraint in fk_constraints ] , [ ] ) <EOL> if set ( cols ) != set ( table . c ) : <EOL> return None , None , None <EOL> return ( <EOL> fk_constraints [ <NUM_LIT:0> ] . elements [ <NUM_LIT:0> ] . column . table , <EOL> fk_constraints [ <NUM_LIT:1> ] . elements [ <NUM_LIT:0> ] . column . table , <EOL> fk_constraints <EOL> ) <EOL> def _relationships_for_fks ( automap_base , map_config , table_to_map_config , <EOL> collection_class , <EOL> name_for_scalar_relationship , <EOL> name_for_collection_relationship , <EOL> generate_relationship ) : <EOL> local_table = map_config . local_table <EOL> local_cls = map_config . cls <EOL> if local_table is None : <EOL> return <EOL> for constraint in local_table . constraints : <EOL> if isinstance ( constraint , ForeignKeyConstraint ) : <EOL> fks = constraint . elements <EOL> referred_table = fks [ <NUM_LIT:0> ] . column . table <EOL> referred_cfg = table_to_map_config . get ( referred_table , None ) <EOL> if referred_cfg is None : <EOL> continue <EOL> referred_cls = referred_cfg . cls <EOL> if local_cls is not referred_cls and issubclass ( <EOL> local_cls , referred_cls ) : <EOL> continue <EOL> relationship_name = name_for_scalar_relationship ( <EOL> automap_base , <EOL> local_cls , <EOL> referred_cls , constraint ) <EOL> backref_name = name_for_collection_relationship ( <EOL> automap_base , <EOL> referred_cls , <EOL> local_cls , <EOL> constraint <EOL> ) <EOL> o2m_kws = { } <EOL> nullable = False not in set ( [ fk . parent . nullable for fk in fks ] ) <EOL> if not nullable : <EOL> o2m_kws [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> if constraint . ondelete and constraint . ondelete . lower ( ) == "<STR_LIT>" : <EOL> o2m_kws [ '<STR_LIT>' ] = True <EOL> else : <EOL> if constraint . ondelete and constraint . ondelete . lower ( ) == "<STR_LIT>" : <EOL> o2m_kws [ '<STR_LIT>' ] = True <EOL> create_backref = backref_name not in referred_cfg . properties <EOL> if relationship_name not in map_config . properties : <EOL> if create_backref : <EOL> backref_obj = generate_relationship ( <EOL> automap_base , <EOL> interfaces . ONETOMANY , backref , <EOL> backref_name , referred_cls , local_cls , <EOL> collection_class = collection_class , <EOL> ** o2m_kws ) <EOL> else : <EOL> backref_obj = None <EOL> rel = generate_relationship ( automap_base , <EOL> interfaces . MANYTOONE , <EOL> relationship , <EOL> relationship_name , <EOL> local_cls , referred_cls , <EOL> foreign_keys = [ <EOL> fk . parent <EOL> for fk in constraint . elements ] , <EOL> backref = backref_obj , <EOL> remote_side = [ <EOL> fk . column <EOL> for fk in constraint . elements ] <EOL> ) <EOL> if rel is not None : <EOL> map_config . properties [ relationship_name ] = rel <EOL> if not create_backref : <EOL> referred_cfg . properties [ <EOL> backref_name ] . back_populates = relationship_name <EOL> elif create_backref : <EOL> rel = generate_relationship ( automap_base , <EOL> interfaces . ONETOMANY , <EOL> relationship , <EOL> backref_name , <EOL> referred_cls , local_cls , <EOL> foreign_keys = [ <EOL> fk . parent <EOL> for fk in constraint . elements ] , <EOL> back_populates = relationship_name , <EOL> collection_class = collection_class , <EOL> ** o2m_kws ) <EOL> if rel is not None : <EOL> referred_cfg . properties [ backref_name ] = rel <EOL> map_config . properties [ <EOL> relationship_name ] . back_populates = backref_name <EOL> def _m2m_relationship ( automap_base , lcl_m2m , rem_m2m , m2m_const , table , <EOL> table_to_map_config , <EOL> collection_class , <EOL> name_for_scalar_relationship , <EOL> name_for_collection_relationship , <EOL> generate_relationship ) : <EOL> map_config = table_to_map_config . get ( lcl_m2m , None ) <EOL> referred_cfg = table_to_map_config . get ( rem_m2m , None ) <EOL> if map_config is None or referred_cfg is None : <EOL> return <EOL> local_cls = map_config . cls <EOL> referred_cls = referred_cfg . cls <EOL> relationship_name = name_for_collection_relationship ( <EOL> automap_base , <EOL> local_cls , <EOL> referred_cls , m2m_const [ <NUM_LIT:0> ] ) <EOL> backref_name = name_for_collection_relationship ( <EOL> automap_base , <EOL> referred_cls , <EOL> local_cls , <EOL> m2m_const [ <NUM_LIT:1> ] <EOL> ) <EOL> create_backref = backref_name not in referred_cfg . properties <EOL> if relationship_name not in map_config . properties : <EOL> if create_backref : <EOL> backref_obj = generate_relationship ( <EOL> automap_base , <EOL> interfaces . MANYTOMANY , <EOL> backref , <EOL> backref_name , <EOL> referred_cls , local_cls , <EOL> collection_class = collection_class <EOL> ) <EOL> else : <EOL> backref_obj = None <EOL> rel = generate_relationship ( automap_base , <EOL> interfaces . MANYTOMANY , <EOL> relationship , <EOL> relationship_name , <EOL> local_cls , referred_cls , <EOL> secondary = table , <EOL> primaryjoin = and_ ( <EOL> fk . column == fk . parent <EOL> for fk in m2m_const [ <NUM_LIT:0> ] . elements ) , <EOL> secondaryjoin = and_ ( <EOL> fk . column == fk . parent <EOL> for fk in m2m_const [ <NUM_LIT:1> ] . elements ) , <EOL> backref = backref_obj , <EOL> collection_class = collection_class <EOL> ) <EOL> if rel is not None : <EOL> map_config . properties [ relationship_name ] = rel <EOL> if not create_backref : <EOL> referred_cfg . properties [ <EOL> backref_name ] . back_populates = relationship_name <EOL> elif create_backref : <EOL> rel = generate_relationship ( automap_base , <EOL> interfaces . MANYTOMANY , <EOL> relationship , <EOL> backref_name , <EOL> referred_cls , local_cls , <EOL> secondary = table , <EOL> primaryjoin = and_ ( <EOL> fk . column == fk . parent <EOL> for fk in m2m_const [ <NUM_LIT:1> ] . elements ) , <EOL> secondaryjoin = and_ ( <EOL> fk . column == fk . parent <EOL> for fk in m2m_const [ <NUM_LIT:0> ] . elements ) , <EOL> back_populates = relationship_name , <EOL> collection_class = collection_class ) <EOL> if rel is not None : <EOL> referred_cfg . properties [ backref_name ] = rel <EOL> map_config . properties [ <EOL> relationship_name ] . back_populates = backref_name </s>
