Esempio n. 1
0
def test_auto_run_with_pull_all(service_profile):
    cx = Connector(service_profile)
    result = cx.auto_run("UNWIND range(1, 5) AS n RETURN n")
    assert result.take() == [1]
    assert result.take() == [2]
    assert result.take() == [3]
    assert result.take() == [4]
    assert result.take() == [5]
    assert result.take() is None
    cx.close()
Esempio n. 2
0
def test_auto_run_with_pull_3_then_pull_all(service_profile):
    cx = Connector(service_profile)
    try:
        result = cx.auto_run("UNWIND range(1, 5) AS n RETURN n", pull=3)
    except IndexError as error:
        skip(str(error))
    else:
        assert result.take() == [1]
        assert result.take() == [2]
        assert result.take() == [3]
        assert result.take() is None
        cx.pull(result)
        assert result.take() == [4]
        assert result.take() == [5]
        assert result.take() is None
    finally:
        cx.close()
Esempio n. 3
0
class BoltRouter(WireRequestHandler):
    def __init__(self, *args, **kwargs):
        self.client = None
        self.target = None
        self.user_agent = None
        self.auth = {}
        self.transaction = None
        self.results = {}
        WireRequestHandler.__init__(self, *args, **kwargs)

    def setup(self):
        self.client = Bolt.accept(self.wire, min_protocol_version=(4, 0))

    def handle(self):
        try:
            while True:
                tag, args = self.client.read_message()
                try:
                    handler = self.handlers[tag]
                except KeyError:
                    raise Exception("Unknown message tag %r" % tag)
                else:
                    handler(self, *args)
        except ConnectionBroken as error:
            print("[%s] Client gone" % (self.wire.remote_address, ))

    def finish(self):
        self.client.close()
        self.target.close()

    def process_01(self, *args):
        for arg in args:
            if isinstance(arg, dict):
                self.auth = arg
                if "user_agent" in self.auth:
                    self.user_agent = self.auth.pop("user_agent")
            else:
                self.user_agent = arg
        self.target = Connector(ServiceProfile(
            target,
            user=self.auth["principal"],
            password=self.auth["credentials"]),
                                init_size=1)
        self.client.write_message(
            0x70,
            [{
                "server": self.target.server_agent,
                "proxy": "py2neo.proxy/0.0.0",  # TODO: hardcoded
                "connection_id": "proxy-0"
            }])  # TODO: hardcoded
        self.client.send()

    def process_02(self, *_):
        self.target.close()
        self.client.close()

    def process_0f(self, *_):
        self.results.clear()
        self.transaction = None
        # TODO: self.error = None
        self.client.write_message(0x70, [{}])
        self.client.send()

    def process_10(self, cypher, parameters, metadata=None):
        try:
            graph_name = metadata["db"]
        except (KeyError, TypeError):
            graph_name = None
        try:
            mode = metadata["mode"]
        except (KeyError, TypeError):
            mode = None
        self.results[-1] = result = self.target.auto_run(
            cypher, parameters, graph_name=graph_name, readonly=(mode == "r"))
        self.client.write_message(0x70, [{"fields": result.fields()}])
        self.client.send()

    def process_2f(self, args=None, *_):
        result = self.results[-1]
        while result.take() is not None:
            pass
        self.client.write_message(0x70, [{}])
        self.client.send()

    def process_3f(self, args=None, *_):
        result = self.results[-1]
        while True:
            record = result.take()
            if record is None:
                break
            self.client.write_message(0x71, record)
        self.client.write_message(0x70, [{}])
        self.client.send()

    handlers = {
        0x01: process_01,
        0x02: process_02,
        0x0F: process_0f,
        0x10: process_10,
        0x2F: process_2f,
        0x3F: process_3f
    }