@@ -0,0 +1,3026 @@
+# used to define the default session used by models when first instantiated.%0A# this can be overridden in the models by passing a session arguement to the%0A# model when initialized.%0Adefault_session = None%0A%0A%0Aclass SessionDirtyException(Exception):%0A    pass%0A%0A%0Aclass Session(object):%0A    %22%22%22%0A        The Session class is used to maintain a set of connections based on a specific%0A        configuration.  The configuration itself should implement attribute access, and it%0A        is assumed that the config is actually a class itself.%0A%0A        The reason it assumes the config is a class, is so that it can pass itself to the%0A        config's __init__ or __call__, allowing the config to do things like toggle to a%0A        set of backup servers, and invalidating the connections currently in use.%0A    %22%22%22%0A    @property%0A    def is_dirty(self):%0A        %22%22%22%0A            Returns whether any of the currently open connections have uncommitted changes.%0A        %22%22%22%0A        dirty = False%0A%0A        for connection in self.connections:%0A            if connection.is_dirty:%0A                dirty = True%0A%0A        return dirty%0A%0A    @property%0A    def config(self):%0A        %22%22%22%0A            Returns the current configuration for this session, returns None if one hasn't%0A            been specified yet.%0A        %22%22%22%0A        return getattr(self, '_config', None)%0A%0A    @config.setter%0A    def config(self, config):%0A        %22%22%22%0A            Attempts to change the config file in use on a session, if the session has%0A            already been used to push data to the servers, but the transaction has not%0A            been committed, raises an exception.%0A        %22%22%22%0A        if self.is_dirty:%0A            raise SessionDirtyException()%0A%0A        self._config = config(self)%0A%0A    def __init__(self, config, default=False):%0A        %22%22%22%0A            The session object takes two parameters, the config file to be used,%0A            and whether or not this session should become the default session. If%0A            the default session is already set and is marked as dirty, raises an%0A            exception.%0A        %22%22%22%0A        self.config = config%0A%0A        if default:%0A            if not getattr(default_session, 'is_dirty', False):%0A                # pull in the global default_session var, so we can modify it.  The%0A                # purpose of this var is to maintain state for multiple models, after%0A                # the initial declaration, so this actually makes sense here.%0A                global default_session%0A%0A                default_session = self%0A%0A            else:%0A                raise SessionDirtyException()%0A%0A    def commit(self):%0A        %22%22%22%0A            Commits any currently uncommitted changes.%0A        %22%22%22%0A        for connection in self.connections:%0A            if connection.is_dirty:%0A                connection.commit()%0A%0A    def rollback(self):%0A        %22%22%22%0A            Rolls back any currently uncommitted changes.%0A        %22%22%22%0A        for connection in self.connections:%0A            if connection.is_dirty:%0A                connection.rollback()%0A

add initial session handler