Source

graphalchemy / graphalchemy / managers / writers.py

Full commit
# -*- coding: utf-8 -*-
'''graph writer managers'''

from graphalchemy.core import defer
from graphalchemy.mixins.writers import Writer


class WriterMixin(Writer):

    '''graph link writer manager'''

    @defer
    def update(self, this, **data):
        '''
        update a graph element's properties

        @param this: graph object
        @param **data: data
        '''
        self.w.update(this, data)
        this._refresh()
        return this

    @defer
    def delete(self, this, indices=None):
        '''
        delete graph element

        @param this: graph object
        @param indicies: graph indices (default: None)
        '''
        self.w.delete(this, indices)


class Links(WriterMixin):

    '''graph link writer manager'''

    @defer
    def create(self, link, start, end, **data):
        '''
        create a new graph link

        @param link: link name
        @param start: start node for link
        @param end: end node for link
        @param **data: data
        '''
        data.update(_created=self.now(), _uuid=self.Q.uuid())
        thing = self.w.create(link, start, end, data)
        thing._refresh()
        return thing


class Nodes(WriterMixin):

    '''graph node writer manager'''

    @defer
    def create(self, **data):
        '''
        create a new graph node

        @param **data: data
        '''
        data.update(_created=self.now(), _uuid=self.Q.uuid())
        thing = self.w.create(data)
        thing._refresh()
        return thing