<s> """<STR_LIT>""" <EOL> from ... schema import Table , MetaData <EOL> from ... orm import synonym as _orm_synonym , mapper , comparable_property , interfaces , properties <EOL> from ... orm . util import polymorphic_union <EOL> from ... orm . base import _mapper_or_none <EOL> from ... util import OrderedDict <EOL> from ... import exc <EOL> import weakref <EOL> from . base import _as_declarative , _declarative_constructor , _DeferredMapperConfig , _add_attribute <EOL> from . clsregistry import _class_resolver <EOL> from . import clsregistry <EOL> def instrument_declarative ( cls , registry , metadata ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in cls . __dict__ : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % cls ) <EOL> cls . _decl_class_registry = registry <EOL> cls . metadata = metadata <EOL> _as_declarative ( cls , cls . __name__ , cls . __dict__ ) <EOL> def has_inherited_table ( cls ) : <EOL> """<STR_LIT>""" <EOL> for class_ in cls . __mro__ [ <NUM_LIT:1> : ] : <EOL> if getattr ( class_ , '<STR_LIT>' , None ) is not None : <EOL> return True <EOL> return False <EOL> class DeclarativeMeta ( type ) : <EOL> def __init__ ( cls , classname , bases , dict_ ) : <EOL> if '<STR_LIT>' not in cls . __dict__ : <EOL> _as_declarative ( cls , classname , cls . __dict__ ) <EOL> type . __init__ ( cls , classname , bases , dict_ ) <EOL> def __setattr__ ( cls , key , value ) : <EOL> _add_attribute ( cls , key , value ) <EOL> def synonym_for ( name , map_column = False ) : <EOL> """<STR_LIT>""" <EOL> def decorate ( fn ) : <EOL> return _orm_synonym ( name , map_column = map_column , descriptor = fn ) <EOL> return decorate <EOL> def comparable_using ( comparator_factory ) : <EOL> """<STR_LIT>""" <EOL> def decorate ( fn ) : <EOL> return comparable_property ( comparator_factory , fn ) <EOL> return decorate <EOL> class declared_attr ( interfaces . _MappedAttribute , property ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fget , * arg , ** kw ) : <EOL> super ( declared_attr , self ) . __init__ ( fget , * arg , ** kw ) <EOL> self . __doc__ = fget . __doc__ <EOL> def __get__ ( desc , self , cls ) : <EOL> return desc . fget ( cls ) <EOL> def declarative_base ( bind = None , metadata = None , mapper = None , cls = object , <EOL> name = '<STR_LIT>' , constructor = _declarative_constructor , <EOL> class_registry = None , <EOL> metaclass = DeclarativeMeta ) : <EOL> """<STR_LIT>""" <EOL> lcl_metadata = metadata or MetaData ( ) <EOL> if bind : <EOL> lcl_metadata . bind = bind <EOL> if class_registry is None : <EOL> class_registry = weakref . WeakValueDictionary ( ) <EOL> bases = not isinstance ( cls , tuple ) and ( cls , ) or cls <EOL> class_dict = dict ( _decl_class_registry = class_registry , <EOL> metadata = lcl_metadata ) <EOL> if constructor : <EOL> class_dict [ '<STR_LIT>' ] = constructor <EOL> if mapper : <EOL> class_dict [ '<STR_LIT>' ] = mapper <EOL> return metaclass ( name , bases , class_dict ) <EOL> def as_declarative ( ** kw ) : <EOL> """<STR_LIT>""" <EOL> def decorate ( cls ) : <EOL> kw [ '<STR_LIT>' ] = cls <EOL> kw [ '<STR_LIT:name>' ] = cls . __name__ <EOL> return declarative_base ( ** kw ) <EOL> return decorate <EOL> class ConcreteBase ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def _create_polymorphic_union ( cls , mappers ) : <EOL> return polymorphic_union ( OrderedDict ( <EOL> ( mp . polymorphic_identity , mp . local_table ) <EOL> for mp in mappers <EOL> ) , '<STR_LIT:type>' , '<STR_LIT>' ) <EOL> @ classmethod <EOL> def __declare_first__ ( cls ) : <EOL> m = cls . __mapper__ <EOL> if m . with_polymorphic : <EOL> return <EOL> mappers = list ( m . self_and_descendants ) <EOL> pjoin = cls . _create_polymorphic_union ( mappers ) <EOL> m . _set_with_polymorphic ( ( "<STR_LIT:*>" , pjoin ) ) <EOL> m . _set_polymorphic_on ( pjoin . c . type ) <EOL> class AbstractConcreteBase ( ConcreteBase ) : <EOL> """<STR_LIT>""" <EOL> __abstract__ = True <EOL> @ classmethod <EOL> def __declare_first__ ( cls ) : <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> return <EOL> clsregistry . add_class ( cls . __name__ , cls ) <EOL> mappers = [ ] <EOL> stack = list ( cls . __subclasses__ ( ) ) <EOL> while stack : <EOL> klass = stack . pop ( ) <EOL> stack . extend ( klass . __subclasses__ ( ) ) <EOL> mn = _mapper_or_none ( klass ) <EOL> if mn is not None : <EOL> mappers . append ( mn ) <EOL> pjoin = cls . _create_polymorphic_union ( mappers ) <EOL> cls . __mapper__ = m = mapper ( cls , pjoin , polymorphic_on = pjoin . c . type ) <EOL> for scls in cls . __subclasses__ ( ) : <EOL> sm = _mapper_or_none ( scls ) <EOL> if sm and sm . concrete and cls in scls . __bases__ : <EOL> sm . _set_concrete_base ( m ) <EOL> class DeferredReflection ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def prepare ( cls , engine ) : <EOL> """<STR_LIT>""" <EOL> to_map = _DeferredMapperConfig . classes_for_base ( cls ) <EOL> for thingy in to_map : <EOL> cls . _sa_decl_prepare ( thingy . local_table , engine ) <EOL> thingy . map ( ) <EOL> mapper = thingy . cls . __mapper__ <EOL> metadata = mapper . class_ . metadata <EOL> for rel in mapper . _props . values ( ) : <EOL> if isinstance ( rel , properties . RelationshipProperty ) and rel . secondary is not None : <EOL> if isinstance ( rel . secondary , Table ) : <EOL> cls . _reflect_table ( rel . secondary , engine ) <EOL> elif isinstance ( rel . secondary , _class_resolver ) : <EOL> rel . secondary . _resolvers += ( <EOL> cls . _sa_deferred_table_resolver ( engine , metadata ) , <EOL> ) <EOL> @ classmethod <EOL> def _sa_deferred_table_resolver ( cls , engine , metadata ) : <EOL> def _resolve ( key ) : <EOL> t1 = Table ( key , metadata ) <EOL> cls . _reflect_table ( t1 , engine ) <EOL> return t1 <EOL> return _resolve <EOL> @ classmethod <EOL> def _sa_decl_prepare ( cls , local_table , engine ) : <EOL> if local_table is not None : <EOL> cls . _reflect_table ( local_table , engine ) <EOL> @ classmethod <EOL> def _reflect_table ( cls , table , engine ) : <EOL> Table ( table . name , <EOL> table . metadata , <EOL> extend_existing = True , <EOL> autoload_replace = False , <EOL> autoload = True , <EOL> autoload_with = engine , <EOL> schema = table . schema ) </s>
