Changeset 421

Show
Ignore:
Timestamp:
12/04/08 15:48:48 (5 years ago)
Author:
ged
Message:

- moved all relationship options to explicit kwargs, instead of doing

"kwargs.pops()" all over the place.

- changed empty dicts and lists to {} and []. I really tried to get used to the

other notation but it seems like it didn't work out.

Location:
elixir/trunk/elixir
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • elixir/trunk/elixir/entity.py

    r420 r421  
    6666 
    6767        # columns and constraints waiting for a table to exist 
    68         self._columns = list() 
    69         self.constraints = list() 
     68        self._columns = [] 
     69        self.constraints = [] 
    7070 
    7171        # properties (it is only useful for checking dupe properties at the 
    7272        # moment, and when adding properties before the mapper is created, 
    7373        # which shouldn't happen). 
    74         self.properties = dict() 
     74        self.properties = {} 
    7575 
    7676        # 
    77         self.relationships = list() 
     77        self.relationships = [] 
    7878 
    7979        # set default value for options 
    80         self.table_args = list() 
     80        self.table_args = [] 
    8181 
    8282        # base class options_defaults 
     
    343343            order_by = [order_by] 
    344344 
    345         order = list() 
     345        order = [] 
    346346        for colname in order_by: 
    347347            col = self.get_column(colname.strip('-')) 
     
    373373        if self.inheritance in ('single', 'concrete', 'multi'): 
    374374            if self.parent and \ 
    375                not (self.inheritance == 'concrete' and not self.polymorphic): 
     375               (self.inheritance != 'concrete' or self.polymorphic): 
     376                # non-polymorphic concrete doesn't need this 
    376377                kwargs['inherits'] = self.parent.mapper 
    377378 
     
    388389                        keys.extend([(child._descriptor.identity, child.table) 
    389390                                     for child in self._get_children()]) 
    390                         #XXX: we might need to change the alias name so that 
    391                         # children (which are parent themselves) don't end up 
    392                         # with the same alias than their parent? 
     391                        # Having the same alias name for an entity and one of 
     392                        # its child (which is a parent itself) shouldn't cause 
     393                        # any problem because the join shouldn't be used at 
     394                        # the same time. But in reality, some versions of SA 
     395                        # do misbehave on this. Since it doesn't hurt to have 
     396                        # different names anyway, here they go. 
    393397                        pjoin = polymorphic_union( 
    394                                     dict(keys), self.polymorphic, 'pjoin') 
     398                                    dict(keys), self.polymorphic, 
     399                                    'pjoin_%s' % self.identity) 
    395400 
    396401                        kwargs['with_polymorphic'] = ('*', pjoin) 
  • elixir/trunk/elixir/options.py

    r417 r421  
    211211    allowcoloverride=False, 
    212212    order_by=None, 
    213     mapper_options=dict(), 
    214     table_options=dict() 
     213    mapper_options={}, 
     214    table_options={} 
    215215) 
    216216 
  • elixir/trunk/elixir/relationships.py

    r414 r421  
    414414    ''' 
    415415 
    416     def __init__(self, of_kind, *args, **kwargs): 
     416    def __init__(self, of_kind, inverse=None, *args, **kwargs): 
    417417        super(Relationship, self).__init__() 
    418418 
    419         self.inverse_name = kwargs.pop('inverse', None) 
    420  
    421419        self.of_kind = of_kind 
     420        self.inverse_name = inverse 
    422421 
    423422        self._target = None 
     
    542541    ''' 
    543542 
    544     def __init__(self, *args, **kwargs): 
    545         self._handle_column_args(kwargs) 
    546         self._handle_constraint_args(kwargs) 
    547  
    548         self.foreign_key = list() 
    549         self.primaryjoin_clauses = list() 
    550  
    551         self.target_column = kwargs.pop('target_column', None) 
    552         if self.target_column is not None and \ 
    553            not isinstance(self.target_column, list): 
    554             self.target_column = [self.target_column] 
    555  
    556         super(ManyToOne, self).__init__(*args, **kwargs) 
    557  
    558     def _handle_column_args(self, kwargs): 
     543    def __init__(self, of_kind, 
     544                 column_kwargs=None, 
     545                 colname=None, required=None, primary_key=None, 
     546                 field=None, 
     547                 constraint_kwargs=None, 
     548                 use_alter=None, ondelete=None, onupdate=None, 
     549                 target_column=None, 
     550                 *args, **kwargs): 
     551 
     552        # 1) handle column-related args 
     553 
    559554        # check that the column arguments don't conflict 
    560         col_args_specified = 'column_kwargs' in kwargs or 'colname' in kwargs 
    561         field_specified = 'field' in kwargs 
    562         assert not (field_specified and col_args_specified), \ 
     555        assert not (field and (column_kwargs or colname)), \ 
    563556               "ManyToOne can accept the 'field' argument or column " \ 
    564557               "arguments ('colname' or 'column_kwargs') but not both!" 
    565558 
    566         self.colname = kwargs.pop('colname', []) 
    567         if self.colname and not isinstance(self.colname, list): 
    568             self.colname = [self.colname] 
    569  
    570         # populate column_kwargs 
    571         self.column_kwargs = kwargs.pop('column_kwargs', {}) 
    572         if 'required' in kwargs: 
    573             self.column_kwargs['nullable'] = not kwargs.pop('required') 
    574         if 'primary_key' in kwargs: 
    575             self.column_kwargs['primary_key'] = kwargs.pop('primary_key') 
    576         # by default, columns created will have an index. 
    577         self.column_kwargs.setdefault('index', True) 
    578  
    579         self.field = kwargs.pop('field', []) 
    580         if self.field and not isinstance(self.field, list): 
    581             self.field = [self.field] 
    582  
    583     def _handle_constraint_args(self, kwargs): 
    584         self.constraint_kwargs = kwargs.pop('constraint_kwargs', {}) 
    585         if 'use_alter' in kwargs: 
    586             self.constraint_kwargs['use_alter'] = kwargs.pop('use_alter') 
    587  
    588         if 'ondelete' in kwargs: 
    589             self.constraint_kwargs['ondelete'] = kwargs.pop('ondelete') 
    590         if 'onupdate' in kwargs: 
    591             self.constraint_kwargs['onupdate'] = kwargs.pop('onupdate') 
     559        if colname and not isinstance(colname, list): 
     560            colname = [colname] 
     561        self.colname = colname or [] 
     562 
     563        column_kwargs = column_kwargs or {} 
     564        # kwargs go by default to the relation(), so we need to manually 
     565        # extract those targeting the Column 
     566        if required is not None: 
     567            column_kwargs['nullable'] = not required 
     568        if primary_key is not None: 
     569            column_kwargs['primary_key'] = primary_key 
     570        # by default, created columns will have an index. 
     571        column_kwargs.setdefault('index', True) 
     572        self.column_kwargs = column_kwargs 
     573 
     574        if field and not isinstance(field, list): 
     575            field = [field] 
     576        self.field = field or [] 
     577 
     578        # 2) handle constraint kwargs 
     579        constraint_kwargs = constraint_kwargs or {} 
     580        if use_alter is not None: 
     581            constraint_kwargs['use_alter'] = use_alter 
     582        if ondelete is not None: 
     583            constraint_kwargs['ondelete'] = ondelete 
     584        if onupdate is not None: 
     585            constraint_kwargs['onupdate'] = onupdate 
     586        self.constraint_kwargs = constraint_kwargs 
     587 
     588        # 3) misc arguments 
     589        if target_column and not isinstance(target_column, list): 
     590            target_column = [target_column] 
     591        self.target_column = target_column 
     592 
     593        self.foreign_key = [] 
     594        self.primaryjoin_clauses = [] 
     595 
     596        super(ManyToOne, self).__init__(of_kind, *args, **kwargs) 
    592597 
    593598    def match_type_of(self, other): 
     
    636641                    "relationships.") 
    637642        else: 
    638             fk_refcols = list() 
    639             fk_colnames = list() 
     643            fk_refcols = [] 
     644            fk_colnames = [] 
    640645 
    641646            if self.target_column is None: 
     
    741746    uselist = False 
    742747 
    743     def __init__(self, *args, **kwargs): 
    744         self.filter = kwargs.pop('filter', None) 
    745         if self.filter is not None: 
     748    def __init__(self, of_kind, filter=None, *args, **kwargs): 
     749        self.filter = filter 
     750        if filter is not None: 
    746751            # We set viewonly to True by default for filtered relationship, 
    747752            # unless manually overridden. 
     
    757762            if 'viewonly' not in kwargs: 
    758763                kwargs['viewonly'] = True 
    759         super(OneToOne, self).__init__(*args, **kwargs) 
     764        super(OneToOne, self).__init__(of_kind, *args, **kwargs) 
    760765 
    761766    def match_type_of(self, other): 
     
    806811    uselist = True 
    807812 
    808     def __init__(self, *args, **kwargs): 
    809         self.user_tablename = kwargs.pop('tablename', None) 
    810         self.local_colname = kwargs.pop('local_colname', []) 
    811         if self.local_colname and not isinstance(self.local_colname, list): 
    812             self.local_colname = [self.local_colname] 
    813         self.remote_colname = kwargs.pop('remote_colname', []) 
    814         if self.remote_colname and not isinstance(self.remote_colname, list): 
    815             self.remote_colname = [self.remote_colname] 
    816         self.ondelete = kwargs.pop('ondelete', None) 
    817         self.onupdate = kwargs.pop('onupdate', None) 
    818         if 'column_format' in kwargs: 
     813    def __init__(self, of_kind, tablename=None, 
     814                 local_colname=None, remote_colname=None, 
     815                 ondelete=None, onupdate=None, 
     816                 table=None, schema=None, 
     817                 column_format=None, 
     818                 *args, **kwargs): 
     819        self.user_tablename = tablename 
     820 
     821        if local_colname and not isinstance(local_colname, list): 
     822            local_colname = [local_colname] 
     823        self.local_colname = local_colname or [] 
     824        if remote_colname and not isinstance(remote_colname, list): 
     825            remote_colname = [remote_colname] 
     826        self.remote_colname = remote_colname or [] 
     827 
     828        self.ondelete = ondelete 
     829        self.onupdate = onupdate 
     830 
     831        self.secondary_table = table 
     832        self.schema = schema 
     833 
     834        if column_format: 
    819835            warnings.warn("The 'column_format' argument on ManyToMany " 
    820836                "relationships is deprecated. Please use the 'local_colname' " 
     
    823839                "options.M2MCOL_NAMEFORMAT if you want a custom format for " 
    824840                "all ManyToMany tables", DeprecationWarning, stacklevel=3) 
    825         self.column_format = kwargs.pop('column_format', 
    826                                         options.M2MCOL_NAMEFORMAT) 
    827  
    828         self.secondary_table = kwargs.pop('table', None) 
    829         self.schema = kwargs.pop('schema', None) 
    830  
    831         self.primaryjoin_clauses = list() 
    832         self.secondaryjoin_clauses = list() 
    833  
    834         super(ManyToMany, self).__init__(*args, **kwargs) 
     841        self.column_format = column_format or options.M2MCOL_NAMEFORMAT 
     842 
     843        self.primaryjoin_clauses = [] 
     844        self.secondaryjoin_clauses = [] 
     845 
     846        super(ManyToMany, self).__init__(of_kind, *args, **kwargs) 
    835847 
    836848    def match_type_of(self, other): 
     
    933945                target_fk_name = "%s_inverse_fk" % source_part 
    934946 
    935             columns = list() 
    936             constraints = list() 
     947            columns = [] 
     948            constraints = [] 
    937949 
    938950            joins = (self.primaryjoin_clauses, self.secondaryjoin_clauses) 
     
    941953              (1, e2_desc, target_fk_name, self.inverse, self.remote_colname)): 
    942954 
    943                 fk_colnames = list() 
    944                 fk_refcols = list() 
     955                fk_colnames = [] 
     956                fk_refcols = [] 
    945957 
    946958                if colnames: 
     
    10871099 
    10881100def rel_mutator_handler(target): 
    1089     def handler(entity, name, *args, **kwargs): 
    1090         if 'through' in kwargs and 'via' in kwargs: 
     1101    def handler(entity, name, of_kind=None, through=None, via=None, 
     1102                *args, **kwargs): 
     1103        if through and via: 
    10911104            setattr(entity, name, 
    1092                     association_proxy(kwargs.pop('through'), 
    1093                                       kwargs.pop('via'), 
    1094                                       **kwargs)) 
     1105                    association_proxy(through, via, **kwargs)) 
    10951106            return 
    1096         elif 'through' in kwargs or 'via' in kwargs: 
     1107        elif through or via: 
    10971108            raise Exception("'through' and 'via' relationship keyword " 
    10981109                            "arguments should be used in combination.") 
    1099         rel = target(kwargs.pop('of_kind'), *args, **kwargs) 
     1110        rel = target(of_kind, *args, **kwargs) 
    11001111        rel.attach(entity, name) 
    11011112    return handler