0001from metasqlobject.eventhub import Signal
0002
0003class ClassCreateSignal(Signal):
0004    """
0005    Signal raised after class creation.  The sender is the superclass
0006    (in case of multiple superclasses, the first superclass).  The
0007    arguments are ``(new_class_name, bases, new_attrs, post_funcs,
0008    early_funcs)``.  ``new_attrs`` is a dictionary and may be modified
0009    (but ``new_class_name`` and ``bases`` are immutable).
0010    ``post_funcs`` is an initially-empty list that can have callbacks
0011    appended to it.
0012
0013    Note: at the time this event is called, the new class has not yet
0014    been created.  The functions in ``post_funcs`` will be called
0015    after the class is created, with the single arguments of
0016    ``(new_class)``.  Also, ``early_funcs`` will be called at the
0017    soonest possible time after class creation (``post_funcs`` is
0018    called after the class's ``__classinit__``).
0019    """
0020
0021# @@: Should there be a class reload event?  This would allow modules
0022# to be reloaded, possibly.  Or it could even be folded into
0023# ClassCreateSignal, since anything that listens to that needs to pay
0024# attention to reloads (or else it is probably buggy).
0025
0026class RowCreateSignal(Signal):
0027    """
0028    Called before an instance is created, with the class as the
0029    sender.  Called with the arguments ``(kwargs, post_funcs)``.
0030    There may be a ``connection`` argument.  ``kwargs``may be usefully
0031    modified.  ``post_funcs`` is a list of callbacks, intended to have
0032    functions appended to it, and are called with the arguments
0033    ``(new_instance)``.
0034
0035    Note: this is not called when an instance is created from an
0036    existing database row.
0037    """
0038
0039# @@: An event for getting a row?  But for each row, when doing a
0040# select?  For .sync, .syncUpdate, .expire?
0041
0042class RowDestroySignal(Signal):
0043    """
0044    Called before an instance is deleted.  Sender is the instance's
0045    class.  Arguments are ``(instance, post_funcs)``.  You cannot
0046    cancel the delete, but you can raise an exception (which will
0047    probably cancel the delete, but also cause an uncaught exception
0048    if not expected).
0049
0050    Note: this is not called when an instance is destroyed through
0051    garbage collection.
0052
0053    post_funcs are called like post_func(soclass, id_that_was_deleted)
0054
0055    @@: Should this allow ``instance`` to be a primary key, so that a
0056    row can be deleted without first fetching it?
0057    """
0058
0059class RowUpdateSignal(Signal):
0060    """
0061    Called when an instance is updated through a call to ``.set()``.
0062    The arguments are ``(instance, kwargs)``.  ``kwargs`` can be
0063    modified.  This is run *before* the instance is updated; if you
0064    want to look at the current values, simply look at ``instance``.
0065    """
0066
0067class AddColumnSignal(Signal):
0068    """
0069    Called when a column is added to a class, with arguments ``(cls,
0070    connection, column_name, column_definition, changeSchema,
0071    post_funcs)``.  This is called *after* the column has been added,
0072    and is called for each column after class creation.
0073
0074    post_funcs are called with ``(cls, so_column_obj)``
0075    """
0076
0077class DeleteColumnSignal(Signal):
0078    """
0079    Called when a column is removed from a class, with the arguments
0080    ``(cls, connection, column_name, so_column_obj, post_funcs)``.
0081    Like ``AddColumnSignal`` this is called after the action has been
0082    performed, and is called for subclassing (when a column is
0083    implicitly removed by setting it to ``None``).
0084
0085    post_funcs are called with ``(cls, so_column_obj)``
0086    """
0087
0088# @@: Signals for indexes and joins?  These are mostly event consumers,
0089# though.
0090
0091class CreateTableSignal(Signal):
0092    """
0093    Called when a table is created.  If ``ifNotExists==True`` and the
0094    table exists, this event is not called.
0095
0096    Called with ``(cls, connection, extra_sql, post_funcs)``.
0097    ``extra_sql`` is a list (which can be appended to) of extra SQL
0098    statements to be run after the table is created.  ``post_funcs``
0099    functions are called with ``(cls, connection)`` after the table
0100    has been created.  Those functions are *not* called simply when
0101    constructing the SQL.
0102    """
0103
0104class DropTableSignal(Signal):
0105    """
0106    Called when a table is dropped.  If ``ifExists==True`` and the
0107    table doesn't exist, this event is not called.
0108
0109    Called with ``(cls, connection, cascade, post_funcs)``.
0110    ``post_funcs`` functions are called with ``(cls, connection)``
0111    after the table has been dropped.  
0112    """
0113
0114__all__ = []
0115for name, value in globals().items():
0116    if isinstance(value, type) and issubclass(value, Signal):
0117        __all__.append(name)