예제 #1
0
파일: __init__.py 프로젝트: motey/py2neo
    def __init__(self, profile=None, **settings):
        # TODO: recognise IPv6 addresses explicitly
        self.__protocol = DEFAULT_PROTOCOL
        self.__secure = DEFAULT_SECURE
        self.__verify = DEFAULT_VERIFY
        self.__user = DEFAULT_USER
        self.__password = DEFAULT_PASSWORD
        self.__address = Address.parse("")

        self._apply_env_vars()

        if profile is None:
            pass
        elif isinstance(profile, string_types):
            self._apply_uri(profile)
        elif isinstance(profile, self.__class__):
            self._apply_settings(**{k: profile[k] for k in self._hash_keys})
        elif isinstance(profile, Mapping):
            self._apply_settings(**profile)
        else:
            raise TypeError("Profile %r is neither a ConnectionProfile "
                            "nor a string URI" % profile)

        self._apply_settings(**settings)

        if not self.address.port:
            addr = list(self.address)
            if self.protocol == "http":
                addr[1] = DEFAULT_HTTPS_PORT if self.secure else DEFAULT_HTTP_PORT
            else:
                addr[1] = DEFAULT_BOLT_PORT
            self.__address = Address(addr)
예제 #2
0
def test_parsing_ipv6_address_with_named_port():
    a = Address.parse("[::1]:http")
    assert isinstance(a, IPv6Address)
    assert a.host == "::1"
    assert a.port == "http"
    assert a.port_number == 80
    assert repr(a)
    assert str(a) == "[::1]:http"
예제 #3
0
def test_parsing_simple_ipv6_address():
    a = Address.parse("[::1]:80")
    assert isinstance(a, IPv6Address)
    assert a.host == "::1"
    assert a.port == 80
    assert a.port_number == 80
    assert repr(a)
    assert str(a) == "[::1]:80"
예제 #4
0
def test_parsing_address_with_named_bolt_port():
    a = Address.parse("localhost:bolt")
    assert isinstance(a, IPv4Address)
    assert a.host == "localhost"
    assert a.port == "bolt"
    assert a.port_number == 7687
    assert repr(a)
    assert str(a) == "localhost:bolt"
예제 #5
0
def test_parsing_address_with_named_port():
    a = Address.parse("localhost:http")
    assert isinstance(a, IPv4Address)
    assert a.host == "localhost"
    assert a.port == "http"
    assert a.port_number == 80
    assert repr(a)
    assert str(a) == "localhost:http"
예제 #6
0
def test_parsing_simple_address():
    a = Address.parse("localhost:80")
    assert isinstance(a, IPv4Address)
    assert a.host == "localhost"
    assert a.port == 80
    assert a.port_number == 80
    assert repr(a)
    assert str(a) == "localhost:80"
예제 #7
0
파일: __init__.py 프로젝트: motey/py2neo
    def _apply_settings(self, uri=None, scheme=None, protocol=None, secure=None, verify=None,
                        address=None, host=None, port=None, port_number=None,
                        auth=None, user=None, password=None, **other):
        if uri:
            self._apply_uri(uri)

        if scheme:
            self._apply_scheme(scheme)
        if protocol:
            self._apply_protocol(protocol)
        if secure is not None:
            self.__secure = secure
        if verify is not None:
            self.__verify = verify

        if isinstance(address, tuple):
            self.__address = Address(address)
        elif address:
            self.__address = Address.parse(address)
        if host and port:
            self.__address = Address.parse("%s:%s" % (host, port))
        elif host:
            self.__address = Address.parse("%s:%s" % (host, self.port))
        elif port:
            self.__address = Address.parse("%s:%s" % (self.host, port))

        if isinstance(auth, tuple):
            self.__user, self.__password = auth
        elif auth:
            self.__user, _, self.__password = auth.partition(":")
        if user:
            self.__user = user
        if password:
            self.__password = password

        if other:
            raise ValueError("The following settings are not supported: %r" % other)
예제 #8
0
    def open(cls, address, timeout=None, keep_alive=False, on_broken=None):
        """ Open a connection to a given network :class:`.Address`.

        :param address:
        :param timeout:
        :param keep_alive:
        :param on_broken: callback for when the wire is broken after a
            successful connection has first been established (this does
            not trigger if the connection never opens successfully)
        :returns: :class:`.Wire` object
        :raises WireError: if connection fails to open
        """
        from socket import socket, SOL_SOCKET, SO_KEEPALIVE
        address = Address(address)
        s = socket(family=address.family)
        if keep_alive:
            s.setsockopt(SOL_SOCKET, SO_KEEPALIVE, 1)
        s.settimeout(timeout)
        try:
            s.connect(address)
        except (IOError, OSError) as error:
            raise_from(WireError("Cannot connect to %r" % (address, )), error)
        return cls(s, on_broken=on_broken)
예제 #9
0
    def __init__(self, spec, image, auth, user):
        self.spec = spec
        self.image = image
        self.address = Address(("localhost", self.spec.bolt_port))
        self.auth = auth
        self.profiles = {
            "bolt":
            ConnectionProfile(scheme="bolt",
                              port=self.spec.bolt_port,
                              auth=self.auth),
            "http":
            ConnectionProfile(scheme="http",
                              port=self.spec.http_port,
                              auth=self.auth),
            "https":
            ConnectionProfile(scheme="https",
                              port=self.spec.https_port,
                              auth=self.auth),
        }
        environment = {}
        if self.auth:
            environment["NEO4J_AUTH"] = "/".join(self.auth)
        environment["NEO4J_ACCEPT_LICENSE_AGREEMENT"] = "yes"
        for key, value in self.spec.config.items():
            fixed_key = "NEO4J_" + key.replace("_", "__").replace(".", "_")
            environment[fixed_key] = value
        for key, value in self.spec.env.items():
            environment[key] = value
        ports = {
            "7474/tcp": self.spec.http_port,
            "7473/tcp": self.spec.https_port,
            "7687/tcp": self.spec.bolt_port
        }
        if self.spec.debug_opts.port is not None:
            ports["5100/tcp"] = self.spec.debug_opts.port
        if self.spec.dir_spec:
            volumes = self.spec.dir_spec.volumes(self.spec.name)
            for path in volumes:
                try:
                    makedirs(path)
                except OSError:
                    pass
        else:
            volumes = None
        try:
            user = int(user)
        except TypeError:
            user = None
        except ValueError:
            # Note: this will only work on Unix.
            from pwd import getpwnam
            user = getpwnam(user).pw_uid

        def create_container(img):
            return docker.containers.create(
                img,
                detach=True,
                environment=environment,
                hostname=self.spec.fq_name,
                name=self.spec.fq_name,
                network=self.spec.service_name,
                ports=ports,
                user=user,
                volumes=volumes,
            )

        try:
            self.container = create_container(self.image)
        except ImageNotFound:
            log.info("Downloading Docker image %r", self.image)
            docker.images.pull(self.image)
            self.container = create_container(self.image)
예제 #10
0
def test_address_from_address():
    a1 = Address.parse("[::1]:http")
    a2 = Address(a1)
    assert a2 is a1
예제 #11
0
def test_address_with_unknown_port_name():
    a = Address(("localhost", "x"))
    with raises(ValueError):
        _ = a.port_number
예제 #12
0
def test_address_with_wrong_number_of_parts():
    with raises(ValueError):
        _ = Address((1, 2, 3, 4, 5))
예제 #13
0
 def remote_address(self):
     """ The remote :class:`.Address` to which this connection is bound.
     """
     return Address(self.__socket.getpeername())
예제 #14
0
 def local_address(self):
     """ The local :class:`.Address` to which this connection is bound.
     """
     return Address(self.__socket.getsockname())