0001"""
0002Bound attributes are attributes that are bound to a specific class and
0003a specific name. In SQLObject a typical example is a column object,
0004which knows its name and class.
0005
0006A bound attribute should define a method ``__addtoclass__(added_class,
0007name)`` (attributes without this method will simply be treated as
0008normal). The return value is ignored; if the attribute wishes to
0009change the value in the class, it must call ``setattr(added_class,
0010name, new_value)``.
0011
0012BoundAttribute is a class that facilitates lazy attribute creation.
0013
0014``bind_attributes(cls, new_attrs)`` is a function that looks for
0015attributes with this special method. ``new_attrs`` is a dictionary,
0016as typically passed into ``__classinit__`` with declarative (calling
0017``bind_attributes`` in ``__classinit__`` would be typical).
0018
0019Note if you do this that attributes defined in a superclass will not
0020be rebound in subclasses. If you want to rebind attributes in
0021subclasses, use ``bind_attributes_local``, which adds a
0022``__bound_attributes__`` variable to your class to track these active
0023attributes.
0024"""
0025
0026__all__ = ['BoundAttribute', 'BoundFactory', 'bind_attributes',
0027 'bind_attributes_local']
0028
0029import declarative
0030import events
0031
0032class BoundAttribute(declarative.Declarative):
0033
0034 """
0035 This is a declarative class that passes all the values given to it
0036 to another object. So you can pass it arguments (via
0037 __init__/__call__) or give it the equivalent of keyword arguments
0038 through subclassing. Then a bound object will be added in its
0039 place.
0040
0041 To hook this other object in, override ``make_object(added_class,
0042 name, **attrs)`` and maybe ``set_object(added_class, name,
0043 **attrs)`` (the default implementation of ``set_object``
0044 just resets the attribute to whatever ``make_object`` returned).
0045
0046 Also see ``BoundFactory``.
0047 """
0048
0049 _private_variables = (
0050 '_private_variables',
0051 '_all_attributes',
0052 '__classinit__',
0053 '__addtoclass__',
0054 '_add_attrs',
0055 'set_object',
0056 'make_object',
0057 'clone_in_subclass',
0058 )
0059
0060 _all_attrs = ()
0061 clone_for_subclass = True
0062
0063 def __classinit__(cls, new_attrs):
0064 declarative.Declarative.__classinit__(cls, new_attrs)
0065 cls._all_attrs = cls._add_attrs(cls, new_attrs)
0066
0067 def __instanceinit__(self, new_attrs):
0068 declarative.Declarative.__instanceinit__(self, new_attrs)
0069 self.__dict__['_all_attrs'] = self._add_attrs(self, new_attrs)
0070
0071 @staticmethod
0072 def _add_attrs(this_object, new_attrs):
0073 private = this_object._private_variables
0074 all_attrs = list(this_object._all_attrs)
0075 for key in new_attrs.keys():
0076 if key.startswith('_') or key in private:
0077 continue
0078 if key not in all_attrs:
0079 all_attrs.append(key)
0080 return tuple(all_attrs)
0081
0082 @declarative.classinstancemethod
0083 def __addtoclass__(self, cls, added_class, attr_name):
0084 me = self or cls
0085 attrs = {}
0086 for name in me._all_attrs:
0087 attrs[name] = getattr(me, name)
0088 attrs['added_class'] = added_class
0089 attrs['attr_name'] = attr_name
0090 obj = me.make_object(**attrs)
0091
0092 if self.clone_for_subclass:
0093 def on_rebind(new_class_name, bases, new_attrs,
0094 post_funcs, early_funcs):
0095 def rebind(new_class):
0096 me.set_object(
0097 new_class, attr_name,
0098 me.make_object(**attrs))
0099 post_funcs.append(rebind)
0100 events.listen(receiver=on_rebind, soClass=added_class,
0101 signal=events.ClassCreateSignal, weak=False)
0102
0103 me.set_object(added_class, attr_name, obj)
0104
0105 @classmethod
0106 def set_object(cls, added_class, attr_name, obj):
0107 setattr(added_class, attr_name, obj)
0108
0109 @classmethod
0110 def make_object(cls, added_class, attr_name, *args, **attrs):
0111 raise NotImplementedError
0112
0113 def __setattr__(self, name, value):
0114 self.__dict__['_all_attrs'] = self._add_attrs(self, {name: value})
0115 self.__dict__[name] = value
0116
0117class BoundFactory(BoundAttribute):
0118
0119 """
0120 This will bind the attribute to whatever is given by
0121 ``factory_class``. This factory should be a callable with the
0122 signature ``factory_class(added_class, attr_name, *args, **kw)``.
0123
0124 The factory will be reinvoked (and the attribute rebound) for
0125 every subclassing.
0126 """
0127
0128 factory_class = None
0129 _private_variables = (
0130 BoundAttribute._private_variables + ('factory_class',))
0131
0132 def make_object(cls, added_class, attr_name, *args, **kw):
0133 return cls.factory_class(added_class, attr_name, *args, **kw)