Exemple #1
0
 def __init__(self, uri, **config):
     parsed = urlparse(uri)
     self.address = (parsed.hostname, parsed.port or DEFAULT_PORT)
     self.security_plan = security_plan = SecurityPlan.build(**config)
     self.encrypted = security_plan.encrypted
     pool = ConnectionPool(lambda a: connect(a, security_plan.ssl_context, **config))
     Driver.__init__(self, pool)
 def from_uri(cls, uri, default_port=0):
     parsed = urlparse(uri)
     if parsed.netloc.startswith("["):
         return IPv6SocketAddress(parsed.hostname, parsed.port
                                  or default_port, 0, 0)
     else:
         return IPv4SocketAddress(parsed.hostname, parsed.port
                                  or default_port)
Exemple #3
0
    def _check_uri(cls, uri):
        """ Check whether a URI is compatible with a :class:`.Driver`
        subclass. When called from a subclass, execution simply passes
        through if the URI scheme is valid for that class. If invalid,
        a `ValueError` is raised.

        :param uri: URI to check for compatibility
        :raise: `ValueError` if URI scheme is incompatible
        """
        parsed = urlparse(uri)
        if parsed.scheme != cls.uri_scheme:
            raise ValueError("%s objects require the %r URI scheme" % (cls.__name__, cls.uri_scheme))
Exemple #4
0
    def parse_routing_context(cls, uri):
        query = urlparse(uri).query
        if not query:
            return {}

        context = {}
        parameters = parse_qs(query, True)
        for key in parameters:
            value_list = parameters[key]
            if len(value_list) != 1:
                raise ValueError("Duplicated query parameters with key '%s', value '%s' found in URL '%s'" % (key, value_list, uri))
            value = value_list[0]
            if not value:
                raise ValueError("Invalid parameters:'%s=%s' in URI '%s'." % (key, value, uri))
            context[key] = value
        return context
Exemple #5
0
    def driver(cls, uri, **config):
        """ Acquire a :class:`.Driver` instance for the given URI and
        configuration. The URI scheme determines the Driver implementation
        that will be returned. Options are:

            ``bolt``
              Returns a :class:`.DirectDriver`.

            ``bolt+routing``
              Returns a :class:`.RoutingDriver`.

        :param uri: URI for a graph database service
        :param config: configuration and authentication details (valid keys are listed below)

            `auth`
              An authentication token for the server, for example
              ``("neo4j", "password")``.

            `der_encoded_server_certificate`
              The server certificate in DER format, if required.

            `encrypted`
              A boolean flag to determine whether encryption should be used.
              Defaults to :const:`True`.

            `trust`
              Trust level: one of :attr:`.TRUST_ALL_CERTIFICATES` (default) or
              :attr:`.TRUST_SYSTEM_CA_SIGNED_CERTIFICATES`.

            `user_agent`
              A custom user agent string, if required.

              for more config options see neo4j.config.default_config

        """
        parsed = urlparse(uri)
        try:
            driver_class = cls.uri_schemes[parsed.scheme]
        except KeyError:
            raise ProtocolError("URI scheme %r not supported" % parsed.scheme)
        else:
            return driver_class(uri, **config)
Exemple #6
0
 def on_refresh(self, data):
     if data is None:
         self.invalidate.fire()
         return
     self.mode = data.system.dbms.mode
     if self.mode in (u"CORE", u"READ_REPLICA"):
         overview = data.cluster_overview
         widths = [0]
         for role in self.servers:
             self.servers[role] = [
                 urlparse(server[u"addresses"][0]).netloc
                 for server in overview.data if server[u"role"] == role
             ]
             widths.extend(map(len, self.servers[role]))
         self.max_width = max(widths)
     else:
         self.servers[u"LEADER"] = [self.address]
         self.max_width = len(self.address)
     self.padding = 6 if self.max_width % 2 == 0 else 5
     self.max_width += self.padding
     self.invalidate.fire()
Exemple #7
0
    def __init__(self, uri, **config):
        parsed = urlparse(uri)
        initial_address = (parsed.hostname, parsed.port or DEFAULT_PORT)
        self.security_plan = security_plan = SecurityPlan.build(**config)
        self.encrypted = security_plan.encrypted
        if not security_plan.routing_compatible:
            # this error message is case-specific as there is only one incompatible
            # scenario right now
            raise ValueError("TRUST_ON_FIRST_USE is not compatible with routing")

        def connector(a):
            return connect(a, security_plan.ssl_context, **config)

        pool = RoutingConnectionPool(connector, initial_address)
        try:
            pool.update_routing_table()
        except:
            pool.close()
            raise
        else:
            Driver.__init__(self, pool)
Exemple #8
0
 def first_address(server):
     return urlparse(server[u"addresses"][0]).netloc
 def connector(address, error_handler):
     return connect(address, security_plan.ssl_context,
                    urlparse(uri).hostname, error_handler, **config)
Exemple #10
0
 def __new__(cls, uri, **config):
     parsed = urlparse(uri)
     for subclass in Driver.__subclasses__():
         if parsed.scheme == subclass.uri_scheme:
             return subclass(uri, **config)
     raise ValueError("URI scheme %r not supported" % parsed.scheme)