boundattributes.py :  » Database » SQLObject » SQLObject-0.12.4 » sqlobject » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » SQLObject 
SQLObject » SQLObject 0.12.4 » sqlobject » boundattributes.py
"""
Bound attributes are attributes that are bound to a specific class and
a specific name.  In SQLObject a typical example is a column object,
which knows its name and class.

A bound attribute should define a method ``__addtoclass__(added_class,
name)`` (attributes without this method will simply be treated as
normal).  The return value is ignored; if the attribute wishes to
change the value in the class, it must call ``setattr(added_class,
name, new_value)``.

BoundAttribute is a class that facilitates lazy attribute creation.

``bind_attributes(cls, new_attrs)`` is a function that looks for
attributes with this special method.  ``new_attrs`` is a dictionary,
as typically passed into ``__classinit__`` with declarative (calling
``bind_attributes`` in ``__classinit__`` would be typical).

Note if you do this that attributes defined in a superclass will not
be rebound in subclasses.  If you want to rebind attributes in
subclasses, use ``bind_attributes_local``, which adds a
``__bound_attributes__`` variable to your class to track these active
attributes.
"""

__all__ = ['BoundAttribute', 'BoundFactory', 'bind_attributes',
           'bind_attributes_local']

import declarative
import events

class BoundAttribute(declarative.Declarative):

    """
    This is a declarative class that passes all the values given to it
    to another object.  So you can pass it arguments (via
    __init__/__call__) or give it the equivalent of keyword arguments
    through subclassing.  Then a bound object will be added in its
    place.

    To hook this other object in, override ``make_object(added_class,
    name, **attrs)`` and maybe ``set_object(added_class, name,
    **attrs)`` (the default implementation of ``set_object``
    just resets the attribute to whatever ``make_object`` returned).

    Also see ``BoundFactory``.
    """

    _private_variables = (
        '_private_variables',
        '_all_attributes',
        '__classinit__',
        '__addtoclass__',
        '_add_attrs',
        'set_object',
        'make_object',
        'clone_in_subclass',
        )

    _all_attrs = ()
    clone_for_subclass = True

    def __classinit__(cls, new_attrs):
        declarative.Declarative.__classinit__(cls, new_attrs)
        cls._all_attrs = cls._add_attrs(cls, new_attrs)

    def __instanceinit__(self, new_attrs):
        declarative.Declarative.__instanceinit__(self, new_attrs)
        self.__dict__['_all_attrs'] = self._add_attrs(self, new_attrs)

    def _add_attrs(this_object, new_attrs):
        private = this_object._private_variables
        all_attrs = list(this_object._all_attrs)
        for key in new_attrs.keys():
            if key.startswith('_') or key in private:
                continue
            if key not in all_attrs:
                all_attrs.append(key)
        return tuple(all_attrs)
    _add_attrs = staticmethod(_add_attrs)

    def __addtoclass__(self, cls, added_class, attr_name):
        me = self or cls
        attrs = {}
        for name in me._all_attrs:
            attrs[name] = getattr(me, name)
        attrs['added_class'] = added_class
        attrs['attr_name'] = attr_name
        obj = me.make_object(**attrs)

        if self.clone_for_subclass:
            def on_rebind(new_class_name, bases, new_attrs,
                          post_funcs, early_funcs):
                def rebind(new_class):
                    me.set_object(
                        new_class, attr_name,
                        me.make_object(**attrs))
                post_funcs.append(rebind)
            events.listen(receiver=on_rebind, soClass=added_class,
                          signal=events.ClassCreateSignal, weak=False)

        me.set_object(added_class, attr_name, obj)

    __addtoclass__ = declarative.classinstancemethod(__addtoclass__)

    def set_object(cls, added_class, attr_name, obj):
        setattr(added_class, attr_name, obj)

    set_object = classmethod(set_object)

    def make_object(cls, added_class, attr_name, *args, **attrs):
        raise NotImplementedError

    make_object = classmethod(make_object)

    def __setattr__(self, name, value):
        self.__dict__['_all_attrs'] = self._add_attrs(self, {name: value})
        self.__dict__[name] = value

class BoundFactory(BoundAttribute):

    """
    This will bind the attribute to whatever is given by
    ``factory_class``.  This factory should be a callable with the
    signature ``factory_class(added_class, attr_name, *args, **kw)``.

    The factory will be reinvoked (and the attribute rebound) for
    every subclassing.
    """

    factory_class = None
    _private_variables = (
        BoundAttribute._private_variables + ('factory_class',))

    def make_object(cls, added_class, attr_name, *args, **kw):
        return cls.factory_class(added_class, attr_name, *args, **kw)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.