Source

graphalchemy / graphalchemy / graphs.py

Full commit
# -*- coding: utf-8 -*-
'''graph interface'''

from urllib import url2pathname

from stuf.utils import clsname

from graphalchemy.core import octopus, extend

__all__ = ['Graph']
# settings
conf = octopus.G


class Graph(octopus.workflow.Manager):

    '''graph interface'''

    session = extend(conf.session, conf.appspace)

    def __init__(self, url, **kw):
        '''
        init

        @param url: database url
        '''
        super(Graph, self).__init__(**kw)
        # default settings
        defaults = self.defaults
        # get appspace name
        userspace = kw.pop('appspace', defaults.userspace)
        # graph managers
        graph_manager = kw.pop('manager', defaults.manager.graph)
        # database configuration in url format
        database, split = url.split(':')
        # database path
        url = self.url = url2pathname(split)
        # database settings
        G = self.G
        dbsettings = G.backends[database]
        # freeze settings
        self.freeze({
            # database connector
            'connecton': dbsettings.connection,
            # database closer
            'close': dbsettings.close,
            # raw database services
            'raw': dbsettings.raw,
            # database transaction
            'transaction': dbsettings.transaction,
            # database link read manager
            'read.link': dbsettings.read.link,
            # database node read manager
            'read.node': dbsettings.read.node,
            # database link write manager
            'write.link': dbsettings.write.link,
            # database node write manager
            'write.node': dbsettings.write.node,
            # store database source
            'database': database,
            # store database configuration
            'url': url,
             # userspace name
            'userspace': userspace,
            # store graph manager name
            'manager.graph': graph_manager,
        })
        # open database
        self._db = self.Q.apply(G.connection, G.backends, url)
        # add data source to appspace
        self.Q.add(self._db, G.key.backend, userspace)
        # pipe close
        self.pipe('abort', G.close, G.appspace)
        # add manager instance to appspace
        self.Q.add(self, graph_manager, userspace)

    def __repr__(self):
        return '{name}@{url}'.format(
            name=clsname(self._db).split('.')[-1], url=self.url,
        )