graphalchemy / graphalchemy / graphs.py

# -*- 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)
        Q = self.Q
        # update with database information
        Q.settings.update(defaults.engines[database])
        # freeze settings
        self.freeze(
            database=database,
            # store database configuration
            url=url,
             # userspace name
            userspace=userspace,
            # store graph manager name
            manager=graph_manager,
        )
        G = self.G
        # open database
        self._db = Q.apply(G.connection, G.backends, url)
        # add data source to appspace
        Q.add(self._db, G.key.backend, userspace)
        # pipe close
        self.pipe('abort', G.close, G.appspace)
        # add manager instance to appspace
        Q.add(self, graph_manager, userspace)

    def __repr__(self):
        return '{name}@{url}'.format(
            name=clsname(self._db).split('.')[-1], url=self.url,
        )
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.