Source

graphalchemy / graphalchemy / mixins / collectors.py

Full commit
# -*- coding: utf-8 -*-
'''graph element collection collector'''

from stuf.utils import lazy

from graphalchemy.core import octopus

__all__ = ('LinksMixin', 'NodesMixin')


class _CollectorsMixin(octopus.context.thing):

    '''element collections collector'''

    def __init__(self, this=None, model=None):
        '''
        init

        @param this: graph object (default: None)
        '''
        super(_CollectorsMixin, self).__init__()
        self._this = this
        self._model = model

    @lazy
    def both(self):
        '''all elements in all directions'''
        return self._collection(self._this, self._model)


class LinksMixin(_CollectorsMixin):

    '''links collections collector'''

    @lazy
    def ins(self):
        '''incoming links'''
        return self._collection(self._this, self._model, direction='incoming')

    @lazy
    def outs(self):
        '''outgoing links'''
        return self._collection(self._this, self._model, direction='outgoing')


class NodesMixin(_CollectorsMixin):

    '''nodes collection collector'''

    @lazy
    def ins(self):
        '''list of nodes with outgoing links to this node'''
        return self._collection(
            self._this,
            self._model,
            direction='outgoing',
            keys=('_hash',),
            link_end='end',
        )

    @lazy
    def outs(self):
        '''nodes with incoming links to a node'''
        return self._collection(
            self._this,
            self._model,
            direction='incoming',
            keys=('_hash',),
            link_end='start',
        )