Source

graphalchemy / graphalchemy / models / finders.py

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

from twoq import port
from appspace import NoAppError

from graphalchemy.core import octopus, join

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


class Nodes(octopus.context.Thing):

    '''node model finder'''

    model = join(conf.direct.element.node, conf.models)

    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 element is not None:
            try:
                return self.Q.app(element['_model'], self.G.userspace)
            except (NoAppError, KeyError):
                pass
        elif port.isstring(model):
            return self.Q.app(model, self.G.userspace)
        return model if model else self.model

nodes = Nodes()