"""
$Id: link_triple.py 29610 2007-03-14 21:12:59Z dbinger $
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/link_triple.py $
"""
from durus.persistent import PersistentObject
from qp.lib.spec import add_getters_and_setters
from qp.lib.spec import string, Specified
from qp.pub.common import get_publisher

def get_link_triple_db():
    return get_publisher().get_connection().get_root().get('link_triple_db')


class LinkTripleDatabase (PersistentObject, Specified):

    links_is = [(string, string, (string, None))]

    def __init__(self):
        self.links = []

    def __getitem__(self, index):
        return self.links[index]

    def __setitem__(self, index, link):
        self._p_note_change()
        self.links[index] = link

    def __delitem__(self, index):
        self._p_note_change()
        del self.links[index]

    def get(self, index, default=None):
        if 0 <= index < len(self.links):
            return self.links[index]
        else:
            return default

    def insert_link(self, link, index=None):
        self._p_note_change()
        if index is None:
            index = len(self.links)
        assert 0 <= index <= len(self.links), index
        links_after = self.links[index:]
        self.links = self.links[:index]
        self.links.append(link)
        self.links.extend(links_after)

add_getters_and_setters(LinkTripleDatabase)
