コード例 #1
0
    def command_start(self, iq, initial_session):
        """
        Starting point for creating a new node.
        :param iq:
        :param initial_session:
        :return:
        """
        if not initial_session['payload']:
            initial_session['notes'] = [('error', 'Cannot execute without a payload')]
        else:
            logger.info('Update Node iq: %s' % iq)
            logger.info('Initial_session: %s' % initial_session)

            payload = StoragePayload(initial_session['payload'])

            logger.debug('relationships: %s' % payload.references)
            logger.debug('properties: %s' % payload.properties)
            logger.debug('types: %s' % payload.types)
            logger.debug('about: %s' % payload.about)

            node = self._command_handler.get_node(payload.about)

            self._command_handler.update_node(node, payload.references, payload.properties)

            # Build up the form response containing the newly created uri
            result = ResultCollectionPayload()
            result.append(ResultPayload(about=str(node.uri), types=node.labels))

            initial_session['payload'] = result.populate_payload()

        return initial_session
コード例 #2
0
    def test_execute_cypher(self):

        self.storage_client._store_found('[email protected]/storage')

        payload = StoragePayload()
        payload.add_property(NEO4J.cypher, 'Match (n) RETURN n LIMIT 25')

        promise = self.storage_client.execute_cypher(payload)

        def handle_result(result):
            self.session['result'] = result

        promise.then(handle_result)

        self.assertTrue(hasattr(promise, 'then'))

        self.send("""
                <iq type="set" to="[email protected]/storage" id="1">
                    <command xmlns="http://jabber.org/protocol/commands"
                        node="cypher"
                        action="execute">
                        <x xmlns="jabber:x:data" type="form">
                            <field var="http://www.neo4j.com/terms/#cypher" type="list-multi">
                                <value>Match (n) RETURN n LIMIT 25</value>
                                <validate xmlns="http://jabber.org/protocol/xdata-validate" datatype="xs:string" />
                            </field>
                        </x>
                    </command>
                </iq>
            """)

        self.assertNotIn('result', self.session)

        result_payload = ResultCollectionPayload()
        result_payload.append(ResultPayload(about='http://www.example.org/instance/01',
                                            types=[FOAF.Person]))

        self.recv("""
                <iq type='result' from='[email protected]/storage' to='tester@localhost/full' id='1'>
                    <command xmlns='http://jabber.org/protocol/commands'
                           sessionid='list:20020923T213616Z-700'
                           node='cypher'
                           status='completed'>
                            %s
                    </command>
                </iq>
            """ % result_payload.populate_payload())

        time.sleep(0.2)

        self.assertIn('result', self.session)

        result = self.session['result']

        self.assertEqual(1, len(result.results))

        self.assertEqual(result.results[0].about, 'http://www.example.org/instance/01')
        self.assertEquals(result.results[0].types[0], str(FOAF.Person))
コード例 #3
0
    def command_start(self, request, initial_session):
        """
        Starting point for creating a new node.
        :param request:
        :param initial_session:
        :return:
        """
        if not initial_session["payload"]:
            initial_session["notes"] = [("error", "Cannot execute without a payload")]
        else:
            payload = StoragePayload(initial_session["payload"])
            cypher_statement = payload.properties.get(str(NEO4J.cypher), None)

            if cypher_statement:
                records = self._command_handler.execute_cypher(cypher_statement[0])
            else:
                records = None

            # Build up the form response containing the newly created uri
            result_collection_payload = ResultCollectionPayload()

            translation_map = CypherFlags.TRANSLATION_KEY.fetch_from(payload.flags)
            translation_map = json.loads(translation_map)

            if records:
                for record in records.records:
                    about = None
                    labels = None
                    columns = {}

                    for key, value in translation_map.iteritems():
                        if key == str(RDF.about):
                            node = record[value]
                            about = node.uri
                            labels = node.labels
                        else:
                            columns[key] = record[value]

                    result_payload = ResultPayload(about=about, types=labels)

                    for key, value in columns.iteritems():
                        result_payload.add_column(key, value)

                    result_collection_payload.append(result_payload)

            initial_session["payload"] = result_collection_payload.populate_payload()

        return initial_session
コード例 #4
0
ファイル: find_node.py プロジェクト: rerobins/rho_neo_backend
    def command_start(self, iq, initial_session):
        """
        Starting point for creating a new node.
        :param iq:
        :param initial_session:
        :return:
        """
        if not initial_session['payload']:
            initial_session['notes'] = [('error', 'Cannot execute without a payload')]
        else:
            payload = StoragePayload(initial_session['payload'])

            if logger.isEnabledFor(logging.DEBUG):
                logger.debug('Find Node iq: %s' % iq)
                logger.debug('Initial_session: %s' % initial_session)
                logger.debug('about: %s' % payload.about)
                logger.debug('relationships: %s' % payload.references)
                logger.debug('properties: %s' % payload.properties)
                logger.debug('types: %s' % payload.types)

            created = False
            nodes = self._command_handler.find_nodes(payload.types, **payload.properties)

            if not nodes and FindFlags.CREATE_IF_MISSING.fetch_from(payload.flags):
                node = self._command_handler.create_node(types=payload.types, properties=payload.properties,
                                                         relationships=payload.references)
                created = True
                nodes.append(node)

            # Build up the form response containing the newly created uri
            result_collection_payload = ResultCollectionPayload()
            for node in nodes:
                payload = ResultPayload(about=node.uri, types=node.labels)
                if created:
                    payload.add_flag(FindResults.CREATED, True)

                result_collection_payload.append(payload)

            initial_session['payload'] = result_collection_payload.populate_payload()

        return initial_session
コード例 #5
0
    def test_update_node(self):

        self.storage_client._store_found('[email protected]/storage')

        payload = StoragePayload()
        payload.about = 'http://www.example.org/instance/01'
        payload.add_type(FOAF.Person)
        payload.add_property(FOAF.name, 'Robert')

        promise = self.storage_client.update_node(payload)

        def handle_result(result):
            self.session['result'] = result

        promise.then(handle_result)

        self.assertTrue(hasattr(promise, 'then'))

        self.send("""
                <iq type="set" to="[email protected]/storage" id="1">
                    <command xmlns="http://jabber.org/protocol/commands"
                        node="update_node"
                        action="execute">
                        <x xmlns="jabber:x:data" type="form">
                            <field var="http://www.w3.org/1999/02/22-rdf-syntax-ns#about" type="text-single">
                                <value>http://www.example.org/instance/01</value>
                            </field>
                            <field var="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" type="list-multi">
                                <value>http://xmlns.com/foaf/0.1/Person</value>
                            </field>
                            <field var="http://xmlns.com/foaf/0.1/name" type="list-multi">
                                <value>Robert</value>
                                <validate xmlns="http://jabber.org/protocol/xdata-validate" datatype="xs:string" />
                            </field>
                        </x>
                    </command>
                </iq>
            """)

        self.assertNotIn('result', self.session)

        result_payload = ResultCollectionPayload()
        result_payload.append(ResultPayload(about='http://www.example.org/instance/01',
                                            types=[FOAF.Person]))

        self.recv("""
                <iq type='result' from='[email protected]/storage' to='tester@localhost/full' id='1'>
                    <command xmlns='http://jabber.org/protocol/commands'
                           sessionid='list:20020923T213616Z-700'
                           node='update_node'
                           status='completed'>
                            %s
                    </command>
                </iq>
            """ % result_payload.populate_payload())

        time.sleep(0.2)

        self.assertIn('result', self.session)

        result = self.session['result']

        self.assertEqual(1, len(result.results))

        self.assertEqual(result.results[0].about, 'http://www.example.org/instance/01')
        self.assertEquals(result.results[0].types[0], str(FOAF.Person))