<s> import types <EOL> from functools import update_wrapper <EOL> from . relations . wrapper import Wrapper <EOL> from . builder import Builder <EOL> from . . query import QueryBuilder <EOL> from . relations import ( <EOL> HasOne , HasMany , HasManyThrough , <EOL> BelongsTo , BelongsToMany , <EOL> MorphOne , MorphMany , MorphTo , MorphToMany <EOL> ) <EOL> class accessor ( object ) : <EOL> def __init__ ( self , accessor_ , attribute = None ) : <EOL> self . accessor = accessor_ <EOL> self . mutator_ = None <EOL> if attribute is not None : <EOL> self . attribute = attribute <EOL> else : <EOL> if isinstance ( accessor_ , property ) : <EOL> self . attribute = accessor_ . fget . __name__ <EOL> else : <EOL> self . attribute = self . accessor . __name__ <EOL> self . expr = accessor_ <EOL> if accessor_ is not None : <EOL> update_wrapper ( self , accessor_ ) <EOL> def __get__ ( self , instance , owner ) : <EOL> if instance is None : <EOL> return self . expr <EOL> else : <EOL> return self . accessor ( instance ) <EOL> def __set__ ( self , instance , value ) : <EOL> if self . mutator_ is None : <EOL> return instance . set_attribute ( self . attribute , value ) <EOL> self . mutator_ ( instance , value ) <EOL> def mutator ( self , f ) : <EOL> self . mutator_ = f <EOL> return mutator ( f , self . attribute ) <EOL> class mutator ( object ) : <EOL> def __init__ ( self , mutator_ , attribute = None ) : <EOL> self . mutator = mutator_ <EOL> self . accessor_ = None <EOL> self . attribute = attribute or self . mutator . __name__ <EOL> def __get__ ( self , instance , owner ) : <EOL> if instance is None : <EOL> return self . mutator <EOL> else : <EOL> if self . accessor_ is None : <EOL> return instance . get_attribute ( self . attribute ) <EOL> return self . accessor_ ( instance ) <EOL> def __set__ ( self , instance , value ) : <EOL> self . mutator ( instance , value ) <EOL> def accessor ( self , f ) : <EOL> self . accessor_ = f <EOL> return accessor ( f , self . attribute ) <EOL> class column ( object ) : <EOL> def __init__ ( self , property_ , attribute = None ) : <EOL> self . property = property_ <EOL> self . mutator_ = None <EOL> self . accessor_ = None <EOL> if attribute is not None : <EOL> self . attribute = attribute <EOL> else : <EOL> if isinstance ( property_ , property ) : <EOL> self . attribute = property_ . fget . __name__ <EOL> else : <EOL> self . attribute = self . property . __name__ <EOL> def __get__ ( self , instance , owner ) : <EOL> if instance is None : <EOL> return self . mutator_ <EOL> else : <EOL> if self . accessor_ is None : <EOL> return instance . get_attribute ( self . attribute ) <EOL> return self . accessor_ ( instance ) <EOL> def __set__ ( self , instance , value ) : <EOL> if self . mutator_ is None : <EOL> return instance . set_attribute ( self . attribute , value ) <EOL> self . mutator_ ( instance , value ) <EOL> def mutator ( self , f ) : <EOL> self . mutator_ = f <EOL> return mutator ( f , self . attribute ) <EOL> def accessor ( self , f ) : <EOL> self . accessor_ = f <EOL> return accessor ( f , self . attribute ) <EOL> class scope ( classmethod ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , method ) : <EOL> super ( scope , self ) . __init__ ( method ) <EOL> self . _method = method <EOL> self . _owner = None <EOL> update_wrapper ( self , method ) <EOL> def __get__ ( self , instance , owner , * args , ** kwargs ) : <EOL> if instance : <EOL> self . _owner = None <EOL> else : <EOL> self . _owner = owner <EOL> return self <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> if not self . _owner : <EOL> return self . _method ( self . _owner , * args , ** kwargs ) <EOL> else : <EOL> return getattr ( self . _owner . query ( ) , self . _method . __name__ ) ( * args , ** kwargs ) <EOL> class relation ( object ) : <EOL> """<STR_LIT>""" <EOL> relation_class = None <EOL> def __init__ ( self , func = None , relation = None ) : <EOL> self . _relation = relation <EOL> self . _related = None <EOL> self . _conditions = None <EOL> self . set_func ( func ) <EOL> def set_func ( self , func ) : <EOL> self . func = func <EOL> if self . _relation is None : <EOL> if isinstance ( func , property ) : <EOL> self . _relation = func . fget . __name__ if func else None <EOL> else : <EOL> self . _relation = func . __name__ if func else None <EOL> self . expr = func <EOL> if func is not None : <EOL> update_wrapper ( self , func ) <EOL> def __get__ ( self , instance , owner ) : <EOL> if instance is None : <EOL> return self . expr <EOL> if self . _relation in instance . _relations : <EOL> return instance . _relations [ self . _relation ] <EOL> self . _related = self . func ( instance ) <EOL> if isinstance ( self . _related , ( Builder , QueryBuilder ) ) : <EOL> self . _conditions = self . _related <EOL> self . _related = self . _related . get_model ( ) . __class__ <EOL> relation = self . _get ( instance ) <EOL> if self . _conditions : <EOL> self . _set_conditions ( relation ) <EOL> relation = Wrapper ( relation ) <EOL> instance . _relations [ self . _relation ] = relation <EOL> return relation <EOL> def _get ( self , instance ) : <EOL> raise NotImplementedError ( ) <EOL> def _set_conditions ( self , relation ) : <EOL> relation . merge_query ( self . _conditions ) <EOL> relation . set_extra_query ( self . _conditions ) <EOL> def __call__ ( self , func ) : <EOL> self . set_func ( func ) <EOL> return self <EOL> class has_one ( relation ) : <EOL> """<STR_LIT>""" <EOL> relation_class = HasOne <EOL> def __init__ ( self , foreign_key = None , local_key = None , relation = None ) : <EOL> if isinstance ( foreign_key , ( types . FunctionType , types . MethodType ) ) : <EOL> func = foreign_key <EOL> foreign_key = None <EOL> else : <EOL> func = None <EOL> self . _foreign_key = foreign_key <EOL> self . _local_key = local_key <EOL> super ( has_one , self ) . __init__ ( func , relation ) <EOL> def _get ( self , instance ) : <EOL> return instance . has_one ( <EOL> self . _related , <EOL> self . _foreign_key , <EOL> self . _local_key , <EOL> self . _relation , <EOL> _wrapped = False <EOL> ) <EOL> class morph_one ( relation ) : <EOL> """<STR_LIT>""" <EOL> relation_class = MorphOne <EOL> def __init__ ( self , name , type_column = None , id_column = None , local_key = None , relation = None ) : <EOL> if isinstance ( name , ( types . FunctionType , types . MethodType ) ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> self . _name = name <EOL> self . _type_column = type_column <EOL> self . _id_column = id_column <EOL> self . _local_key = local_key <EOL> super ( morph_one , self ) . __init__ ( relation = relation ) <EOL> def _get ( self , instance ) : <EOL> return instance . morph_one ( <EOL> self . _related , self . _name , <EOL> self . _type_column , self . _id_column , <EOL> self . _local_key , self . _relation , <EOL> _wrapped = False <EOL> ) <EOL> class belongs_to ( relation ) : <EOL> """<STR_LIT>""" <EOL> relation_class = BelongsTo <EOL> def __init__ ( self , foreign_key = None , other_key = None , relation = None ) : <EOL> if isinstance ( foreign_key , ( types . FunctionType , types . MethodType ) ) : <EOL> func = foreign_key <EOL> foreign_key = None <EOL> else : <EOL> func = None <EOL> self . _foreign_key = foreign_key <EOL> self . _other_key = other_key <EOL> super ( belongs_to , self ) . __init__ ( func , relation ) <EOL> def _get ( self , instance ) : <EOL> return instance . belongs_to ( <EOL> self . _related , <EOL> self . _foreign_key , <EOL> self . _other_key , <EOL> self . _relation , <EOL> _wrapped = False <EOL> ) <EOL> def _set ( self , relation ) : <EOL> relation . _foreign_key = self . _foreign_key <EOL> relation . _other_key = self . _other_key <EOL> relation . _relation = self . _relation <EOL> class morph_to ( relation ) : <EOL> """<STR_LIT>""" <EOL> relation_class = MorphTo <EOL> def __init__ ( self , name = None , type_column = None , id_column = None ) : <EOL> if isinstance ( name , ( types . FunctionType , types . MethodType ) ) : <EOL> func = name <EOL> name = None <EOL> else : <EOL> func = None <EOL> self . _name = name <EOL> self . _type_column = type_column <EOL> self . _id_column = id_column <EOL> super ( morph_to , self ) . __init__ ( func , name ) <EOL> def _get ( self , instance ) : <EOL> return instance . morph_to ( <EOL> self . _relation , <EOL> self . _type_column , self . _id_column , <EOL> _wrapped = False <EOL> ) <EOL> class has_many ( relation ) : <EOL> """<STR_LIT>""" <EOL> relation_class = HasMany <EOL> def __init__ ( self , foreign_key = None , local_key = None , relation = None ) : <EOL> if isinstance ( foreign_key , ( types . FunctionType , types . MethodType ) ) : <EOL> func = foreign_key <EOL> foreign_key = None <EOL> else : <EOL> func = None <EOL> self . _foreign_key = foreign_key <EOL> self . _local_key = local_key <EOL> super ( has_many , self ) . __init__ ( func , relation ) <EOL> def _get ( self , instance ) : <EOL> return instance . has_many ( <EOL> self . _related , <EOL> self . _foreign_key , <EOL> self . _local_key , <EOL> self . _relation , <EOL> _wrapped = False <EOL> ) <EOL> class has_many_through ( relation ) : <EOL> """<STR_LIT>""" <EOL> relation_class = HasManyThrough <EOL> def __init__ ( self , through , first_key = None , second_key = None , relation = None ) : <EOL> if isinstance ( through , ( types . FunctionType , types . MethodType ) ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> self . _through = through <EOL> self . _first_key = first_key <EOL> self . _second_key = second_key <EOL> super ( has_many_through , self ) . __init__ ( relation = relation ) <EOL> def _get ( self , instance ) : <EOL> return instance . has_many_through ( <EOL> self . _related , <EOL> self . _through , <EOL> self . _first_key , <EOL> self . _second_key , <EOL> self . _relation , <EOL> _wrapped = False <EOL> ) <EOL> class morph_many ( relation ) : <EOL> """<STR_LIT>""" <EOL> relation_class = MorphMany <EOL> def __init__ ( self , name , type_column = None , id_column = None , local_key = None , relation = None ) : <EOL> if isinstance ( name , ( types . FunctionType , types . MethodType ) ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> self . _name = name <EOL> self . _type_column = type_column <EOL> self . _id_column = id_column <EOL> self . _local_key = local_key <EOL> super ( morph_many , self ) . __init__ ( relation = relation ) <EOL> def _get ( self , instance ) : <EOL> return instance . morph_many ( <EOL> self . _related , self . _name , <EOL> self . _type_column , self . _id_column , <EOL> self . _local_key , self . _relation , <EOL> _wrapped = False <EOL> ) <EOL> class belongs_to_many ( relation ) : <EOL> """<STR_LIT>""" <EOL> relation_class = BelongsToMany <EOL> def __init__ ( self , table = None , foreign_key = None , other_key = None , <EOL> relation = None , with_timestamps = False , with_pivot = None ) : <EOL> if isinstance ( table , ( types . FunctionType , types . MethodType ) ) : <EOL> func = table <EOL> table = None <EOL> else : <EOL> func = None <EOL> self . _table = table <EOL> self . _foreign_key = foreign_key <EOL> self . _other_key = other_key <EOL> self . _timestamps = with_timestamps <EOL> self . _pivot = with_pivot <EOL> super ( belongs_to_many , self ) . __init__ ( func , relation ) <EOL> def _get ( self , instance ) : <EOL> r = instance . belongs_to_many ( <EOL> self . _related , <EOL> self . _table , <EOL> self . _foreign_key , <EOL> self . _other_key , <EOL> self . _relation , <EOL> _wrapped = False <EOL> ) <EOL> if self . _timestamps : <EOL> r = r . with_timestamps ( ) <EOL> if self . _pivot : <EOL> r = r . with_pivot ( * self . _pivot ) <EOL> return r <EOL> class morph_to_many ( relation ) : <EOL> """<STR_LIT>""" <EOL> relation_class = MorphToMany <EOL> def __init__ ( self , name , table = None , foreign_key = None , other_key = None , relation = None ) : <EOL> if isinstance ( name , ( types . FunctionType , types . MethodType ) ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> self . _name = name <EOL> self . _table = table <EOL> self . _foreign_key = foreign_key <EOL> self . _other_key = other_key <EOL> super ( morph_to_many , self ) . __init__ ( relation = relation ) <EOL> def _get ( self , instance ) : <EOL> return instance . morph_to_many ( <EOL> self . _related , <EOL> self . _name , <EOL> self . _table , <EOL> self . _foreign_key , <EOL> self . _other_key , <EOL> relation = self . _relation , <EOL> _wrapped = False <EOL> ) <EOL> class morphed_by_many ( relation ) : <EOL> """<STR_LIT>""" <EOL> relation_class = MorphToMany <EOL> def __init__ ( self , name , table = None , foreign_key = None , other_key = None , relation = None ) : <EOL> if isinstance ( foreign_key , ( types . FunctionType , types . MethodType ) ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> self . _name = name <EOL> self . _table = table <EOL> self . _foreign_key = foreign_key <EOL> self . _other_key = other_key <EOL> super ( morphed_by_many , self ) . __init__ ( relation = relation ) <EOL> def _get ( self , instance ) : <EOL> return instance . morphed_by_many ( <EOL> self . _related , <EOL> self . _name , <EOL> self . _table , <EOL> self . _foreign_key , <EOL> self . _other_key , <EOL> self . _relation , <EOL> _wrapped = False <EOL> ) </s>
