Source

graphalchemy / graphalchemy / finders.py

Full commit
# -*- coding: utf-8 -*-
'''graph model finder'''

from __future__ import absolute_import

from appspace.ext import direct
from appspace.error import NoAppError

from .core import Server

conf = getattr(Server, 'S')


class Finder(Server):

    '''model finder'''

    def __call__(self, model=None, element=None):
        '''
        get default model

        @param model: model class or model identifier (default: None)
        @param element: graph database element
        '''
        if model is None:
            if element is not None:
                try:
                    model = self.A[conf.userspace][element['_model']]
                except (NoAppError, KeyError):
                    pass
        elif isinstance(model, basestring):
            model = self.A[conf.userspace][model.lower()]
        if not model:
            model = self._default
        return model


class Links(Finder):

    '''link model finder'''

    _default = direct(conf.generic.element.link, conf.generics)


links = Links()


class Nodes(Finder):

    '''node model finder'''

    _default = direct(conf.generic.element.node, conf.generics)


nodes = Nodes()

__all__ = ('links', 'nodes')