"""$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/base.py $
$Id: base.py 25817 2004-12-21 16:27:07Z dbinger $

Provides universal base classes which all classes in the Dulcinea
library should subclass.  Currently there are two such
universal base classes:
    DulcineaPersistent    for classes whose instances should always be
                          first-class, persistent database objects
    DulcineaBase          for classes whose instances may be transient,
                          but can still be persisted if they happen to
                          be attached to another persistent object

"""

import new
from types import ClassType
from durus.persistent import Persistent

# If you're wondering which of DulcineaBase and DulcineaPersistent to use
# for some class, then use DulcineaPersistent: that will make instances of
# that class "first-class" database objects, subject to the Durus persistence
# machinery.

class DulcineaBase:
    """The standard base class for classes whose instances do not need
    to be first-class database objects.  Instances of DulcineaBase can still
    be stored in the database if they happen to be attached to
    other persistent objects, but they don't really participate in
    persistence -- they just come along for the ride.

    Instance attributes: none

    """

    def copy (self):
        klass = self.__class__
        if type(klass) is ClassType:
            newself = new.instance(self.__class__, {})
        elif isinstance(klass, object):
            newself = klass.__new__(klass)
        else:
            raise RuntimeError, "unknown type of class object: %s" % `klass`
        newself.__dict__.update(self.__dict__)
        return newself

    def __copy__ (self):
        return self.copy()


class DulcineaPersistent (Persistent, DulcineaBase):
    """The standard base class for classes whose instances should be
    first-class database objects that participate fully in the
    persistence mechanism.  Instances of DulcineaPersistent still have to be
    referenced by an existing persistent object, i.e. they must
    ultimately connect back to one of the database root objects.

    Instance attributes: none
    """

