graphalchemy / graphalchemy /

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

from __future__ import absolute_import

from appspace.ext import factory, __
from stuf.utils import clsname, selfname

from .core import Server

conf = getattr(Server, 'S')

class Graph(Server):

    '''graph interface'''

    links = factory(, conf.appspace, conf.key.db)
    nodes = factory(conf.manager.node, conf.appspace, conf.key.db)

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

        @param url: database url
        super(Graph, self).__init__()
        Q = __(self)
        defaults = Q.defaults().one()
        restricted = Q.required().one()
        appspace = kw.pop('appspace', defaults.userspace)
        graph_manager = kw.pop('manager', defaults.manager.graph)
        database = kw.pop('database', defaults.backend)
        # database location in url format
        self.url = url
        # open database
        self._db = Q.apply(database, restricted.backends, url).one()
        Q.setting('manager.graph', graph_manager)
        Q.setting('userspace', appspace)
        Q.setting('database', database)
        # add manager instance to appspace, conf.userspace, self)
        # add data source to appspace, conf.userspace, self._db)

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

    def register(self, model):
        add model class to appspace

        @param model: graph element model
        __(self).app(selfname(model).lower(), conf.userspace, model)

__all__ = ['Graph']