def __init__(self, address, sock, protocol_version, error_handler, **config):
        self.address = address
        self.socket = sock
        self.protocol_version = protocol_version
        self.error_handler = error_handler
        self.server = ServerInfo(SocketAddress.from_socket(sock))
        self.input_buffer = ChunkedInputBuffer()
        self.output_buffer = ChunkedOutputBuffer()
        self.packer = Packer(self.output_buffer)
        self.unpacker = Unpacker()
        self.responses = deque()
        self._max_connection_lifetime = config.get("max_connection_lifetime", default_config["max_connection_lifetime"])
        self._creation_timestamp = perf_counter()

        # Determine the user agent and ensure it is a Unicode value
        user_agent = config.get("user_agent", default_config["user_agent"])
        if isinstance(user_agent, bytes):
            user_agent = user_agent.decode("UTF-8")
        self.user_agent = user_agent

        # Determine auth details
        auth = config.get("auth")
        if not auth:
            self.auth_dict = {}
        elif isinstance(auth, tuple) and 2 <= len(auth) <= 3:
            from neo4j.v1 import basic_auth
            self.auth_dict = vars(basic_auth(*auth))
        else:
            try:
                self.auth_dict = vars(auth)
            except (KeyError, TypeError):
                raise TypeError("Cannot determine auth details from %r" % auth)

        # Pick up the server certificate, if any
        self.der_encoded_server_certificate = config.get("der_encoded_server_certificate")
Beispiel #2
0
    def test_dehydration_2d(self):
        coordinates = (.1, 0)
        p = CartesianPoint(coordinates)

        dehydrator = DataDehydrator()
        buffer = io.BytesIO()
        packer = Packer(buffer)
        packer.pack(dehydrator.dehydrate((p, ))[0])
        self.assertEqual(
            buffer.getvalue(),
            b"\xB3X" + b"\xC9" + struct.pack(">h", 7203) + b"".join(
                map(lambda c: b"\xC1" + struct.pack(">d", c), coordinates)))
    def test_dehydration_3d(self):
        coordinates = (1, -2, 3.1)
        p = WGS84Point(coordinates)

        dehydrator = DataDehydrator()
        buffer = io.BytesIO()
        packer = Packer(buffer)
        packer.pack(dehydrator.dehydrate((p, ))[0])
        self.assertEqual(
            buffer.getvalue(),
            b"\xB4Y" + b"\xC9" + struct.pack(">h", 4979) + b"".join(
                map(lambda c: b"\xC1" + struct.pack(">d", c), coordinates)))
Beispiel #4
0
    def test_dehydration(self):
        MyPoint = point_type("MyPoint", ["x", "y"], {2: 1234})
        coordinates = (.1, 0)
        p = MyPoint(coordinates)

        dehydrator = DataDehydrator()
        buffer = io.BytesIO()
        packer = Packer(buffer)
        packer.pack(dehydrator.dehydrate((p, ))[0])
        self.assertEqual(
            buffer.getvalue(),
            b"\xB3X" + b"\xC9" + struct.pack(">h", 1234) + b"".join(
                map(lambda c: b"\xC1" + struct.pack(">d", c), coordinates)))
Beispiel #5
0
    def __init__(self,
                 unresolved_address,
                 sock,
                 max_connection_lifetime,
                 *,
                 auth=None,
                 user_agent=None,
                 routing_context=None):
        self.unresolved_address = unresolved_address
        self.socket = sock
        self.server_info = ServerInfo(Address(sock.getpeername()),
                                      self.PROTOCOL_VERSION)
        self.outbox = Outbox()
        self.inbox = Inbox(self.socket, on_error=self._set_defunct)
        self.packer = Packer(self.outbox)
        self.unpacker = Unpacker(self.inbox)
        self.responses = deque()
        self._max_connection_lifetime = max_connection_lifetime
        self._creation_timestamp = perf_counter()
        self.supports_multiple_results = False
        self.supports_multiple_databases = False
        self._is_reset = True
        self.routing_context = routing_context

        # Determine the user agent
        if user_agent:
            self.user_agent = user_agent
        else:
            self.user_agent = get_user_agent()

        # Determine auth details
        if not auth:
            self.auth_dict = {}
        elif isinstance(auth, tuple) and 2 <= len(auth) <= 3:
            from neo4j import Auth
            self.auth_dict = vars(Auth("basic", *auth))
        else:
            try:
                self.auth_dict = vars(auth)
            except (KeyError, TypeError):
                raise AuthError("Cannot determine auth details from %r" % auth)

        # Check for missing password
        try:
            credentials = self.auth_dict["credentials"]
        except KeyError:
            pass
        else:
            if credentials is None:
                raise AuthError("Password cannot be None")
 def assert_packable(cls, value, packed_value):
     stream_out = BytesIO()
     packer = Packer(stream_out)
     packer.pack(value)
     packed = stream_out.getvalue()
     try:
         assert packed == packed_value
     except AssertionError:
         raise AssertionError("Packed value %r is %r instead of expected %r" %
                              (value, packed, packed_value))
     unpacked = Unpacker(UnpackableBuffer(packed)).unpack()
     try:
         assert unpacked == value
     except AssertionError:
         raise AssertionError("Unpacked value %r is not equal to original %r" % (unpacked, value))
Beispiel #7
0
    def __init__(self,
                 unresolved_address,
                 sock,
                 *,
                 auth=None,
                 protocol_version=None,
                 **config):
        self.config = PoolConfig.consume(config)
        self.protocol_version = protocol_version
        self.unresolved_address = unresolved_address
        self.socket = sock
        self.server = ServerInfo(Address(sock.getpeername()), protocol_version)
        self.outbox = Outbox()
        self.inbox = Inbox(BufferedSocket(self.socket, 32768),
                           on_error=self._set_defunct)
        self.packer = Packer(self.outbox)
        self.unpacker = Unpacker(self.inbox)
        self.responses = deque()
        self._max_connection_lifetime = self.config.max_age
        self._creation_timestamp = perf_counter()

        # Determine the user agent
        user_agent = self.config.user_agent
        if user_agent:
            self.user_agent = user_agent
        else:
            self.user_agent = get_user_agent()

        # Determine auth details
        if not auth:
            self.auth_dict = {}
        elif isinstance(auth, tuple) and 2 <= len(auth) <= 3:
            from neo4j import Auth
            self.auth_dict = vars(Auth("basic", *auth))
        else:
            try:
                self.auth_dict = vars(auth)
            except (KeyError, TypeError):
                raise AuthError("Cannot determine auth details from %r" % auth)

        # Check for missing password
        try:
            credentials = self.auth_dict["credentials"]
        except KeyError:
            pass
        else:
            if credentials is None:
                raise AuthError("Password cannot be None")
 def test_list_stream(self):
     packed_value = b"\xD7\x01\x02\x03\xDF"
     unpacked_value = [1, 2, 3]
     stream_out = BytesIO()
     packer = Packer(stream_out)
     packer.pack_list_stream_header()
     packer.pack(1)
     packer.pack(2)
     packer.pack(3)
     packer.pack_end_of_stream()
     packed = stream_out.getvalue()
     try:
         assert packed == packed_value
     except AssertionError:
         raise AssertionError("Packed value is %r instead of expected %r" %
                              (packed, packed_value))
     unpacked = Unpacker(UnpackableBuffer(packed)).unpack()
     try:
         assert unpacked == unpacked_value
     except AssertionError:
         raise AssertionError("Unpacked value %r is not equal to expected %r" %
                              (unpacked, unpacked_value))
    def __init__(self, sock, **config):
        self.socket = sock
        self.server = ServerInfo(SocketAddress.from_socket(sock))
        self.input_buffer = ChunkedInputBuffer()
        self.output_buffer = ChunkedOutputBuffer()
        self.packer = Packer(self.output_buffer)
        self.unpacker = Unpacker()
        self.responses = deque()

        # Determine the user agent and ensure it is a Unicode value
        user_agent = config.get("user_agent", DEFAULT_USER_AGENT)
        if isinstance(user_agent, bytes):
            user_agent = user_agent.decode("UTF-8")
        self.user_agent = user_agent

        # Determine auth details
        auth = config.get("auth")
        if not auth:
            self.auth_dict = {}
        elif isinstance(auth, tuple) and 2 <= len(auth) <= 3:
            from neo4j.v1 import basic_auth
            self.auth_dict = vars(basic_auth(*auth))
        else:
            try:
                self.auth_dict = vars(auth)
            except (KeyError, TypeError):
                raise TypeError("Cannot determine auth details from %r" % auth)

        # Pick up the server certificate, if any
        self.der_encoded_server_certificate = config.get(
            "der_encoded_server_certificate")

        response = InitResponse(self)
        self.append(INIT, (self.user_agent, self.auth_dict), response=response)
        self.sync()

        self._supports_statement_reuse = self.server.supports_statement_reuse()
        self.packer.supports_bytes = self.server.supports_bytes()
 def test_map_stream(self):
     packed_value = b"\xDB\x81A\x01\x81B\x02\xDF"
     unpacked_value = {u"A": 1, u"B": 2}
     stream_out = BytesIO()
     packer = Packer(stream_out)
     packer.pack_map_stream_header()
     packer.pack(u"A")
     packer.pack(1)
     packer.pack(u"B")
     packer.pack(2)
     packer.pack_end_of_stream()
     packed = stream_out.getvalue()
     try:
         assert packed == packed_value
     except AssertionError:
         raise AssertionError("Packed value is %r instead of expected %r" %
                              (packed, packed_value))
     unpacked = Unpacker(UnpackableBuffer(packed)).unpack()
     try:
         assert unpacked == unpacked_value
     except AssertionError:
         raise AssertionError("Unpacked value %r is not equal to expected %r" %
                              (unpacked, unpacked_value))
 def packb(cls, *values):
     stream = BytesIO()
     packer = Packer(stream)
     for value in values:
         packer.pack(value)
     return stream.getvalue()
 def test_map_size_overflow(self):
     stream_out = BytesIO()
     packer = Packer(stream_out)
     with raises(OverflowError):
         packer.pack_map_header(2 ** 32)
 def encode_message(cls, tag, *fields):
     b = BytesIO()
     packer = Packer(b)
     for field in fields:
         packer.pack(field)
     return bytearray([0xB0 + len(fields), tag]) + b.getvalue()