예제 #1
0
    def __init__(self):
        super(RedNode, self).__init__()
        neighbours = Neighbours()
        neighbours.append_store(rdf_files.schema)
        neighbours.append_store(rdf_files.syntax)
        neighbours.append_store(rdf_files.builtin)
        self.neighbourhood = RedNeighbourhood(self, neighbours)
        self.neighbours = neighbours

        self.uri = None
        self.node = Node()
        self.node_store = NodeStore(self.node)
        filename, uri = "node.rdf", "http://redfoot.net/2002/05/11/"
        self.node.load(filename, uri, 1)
        self.neighbours.append_store(self.node_store)
예제 #2
0
class RedNode(Visit, NeighbourManager, AutoSave, TripleStore):
    """
    A RedNode is a store that is queryable via high level queries, can
    manage its neighbour connections, [automatically] save to RDF/XML
    syntax, and load from RDF/XML syntax.

    A RedNode contains a neighbourhood store for querying the nodes'
    entire neighbourhood, which includes itself and its neighbours in
    that order.
    """

    def __init__(self):
        super(RedNode, self).__init__()
        neighbours = Neighbours()
        neighbours.append_store(rdf_files.schema)
        neighbours.append_store(rdf_files.syntax)
        neighbours.append_store(rdf_files.builtin)
        self.neighbourhood = RedNeighbourhood(self, neighbours)
        self.neighbours = neighbours

        self.uri = None
        self.node = Node()
        self.node_store = NodeStore(self.node)
        filename, uri = "node.rdf", "http://redfoot.net/2002/05/11/"
        self.node.load(filename, uri, 1)
        self.neighbours.append_store(self.node_store)

    def __get_uri(self):
        if not self.__uri:
            from socket import getfqdn

            self.__uri = "http://%s/" % getfqdn()
            print "WARNING: rednode has no uri... using computed value of '%s'" % self.__uri
        return self.__uri

    def __set_uri(self, value):
        self.__uri = value

    uri = property(__get_uri, __set_uri)

    def make_statement(self, context_uri):
        return lambda s, p, o: self.node.make_statement(URIRef(context_uri), s, p, o)

    def retract_statement(self, context_uri):
        return lambda s, p, o: self.node.retract_statement(URIRef(context_uri), s, p, o)

    def load(self, location, uri=None, create=0):
        super(RedNode, self).load(location, uri, create)

        def _listen_on(o):
            host = self.get_first_value(o, HOST, None)
            port = self.get_first_value(o, PORT, None)
            if host or host == "" and port:
                self.node.listen_on(host, int(port))

        for object in self.objects(URIRef(uri), LISTEN_ON):
            _listen_on(object)

        def _server(o):
            host = self.get_first_value(o, HOST, None)
            port = self.get_first_value(o, PORT, None)
            if host or host == "" and port:
                from redfootlib.server import RedServer

                server = RedServer(host, int(port))
                server.run(background=1)

                def _add_app(o):
                    app_class = self.get_first_value(o, APP_CLASS, None)
                    if app_class:
                        app = self.get_app_instance(app_class)
                        server.add_app(app)

                for object in self.objects(o, APP):
                    _add_app(object)

        for object in self.objects(URIRef(uri), SERVER):
            _server(object)

    def save(self, location=None, uri=None):
        super(RedNode, self).save(location, uri)
        for store in self.neighbours.stores():
            if hasattr(store, "save"):
                try:
                    store.save()
                except Exception, e:
                    from traceback import print_exc

                    print_exc()
                    print "... while trying to save: ", store
        print "Saving node_store..."
        self.node.save()
        print "done."