def open(cls, profile=None, user_agent=None, on_bind=None, on_unbind=None, on_release=None, on_broken=None): """ Open a Bolt connection to a server. :param profile: :class:`.ConnectionProfile` detailing how and where to connect :param user_agent: :param on_bind: :param on_unbind: :param on_release: :param on_broken: :returns: :class:`.Bolt` connection object :raises: :class:`.ConnectionUnavailable` if a connection cannot be opened, or a protocol version cannot be agreed """ if profile is None: profile = ConnectionProfile(scheme="bolt") try: wire = cls._connect(profile, on_broken=on_broken) protocol_version = cls._handshake(wire) subclass = cls._get_subclass(protocol_version) if subclass is None: raise TypeError("Unable to agree supported protocol version") bolt = subclass(wire, profile, (user_agent or bolt_user_agent()), on_bind=on_bind, on_unbind=on_unbind, on_release=on_release) bolt.__local_port = wire.local_address.port_number bolt._hello() return bolt except (TypeError, WireError) as error: raise_from(ConnectionUnavailable("Cannot open connection to %r" % profile), error)
def open(cls, profile=None, user_agent=None, on_bind=None, on_unbind=None, on_release=None, on_broken=None): """ Open an HTTP connection to a server. :param profile: :class:`.ConnectionProfile` detailing how and where to connect :param user_agent: :param on_bind: :param on_unbind: :param on_release: :param on_broken: :returns: :class:`.HTTP` connection object :raises: :class:`.ConnectionUnavailable` if a connection cannot be opened """ if profile is None: profile = ConnectionProfile(scheme="http") try: http = cls(profile, (user_agent or http_user_agent()), on_bind=on_bind, on_unbind=on_unbind, on_release=on_release) http._hello() return http except HTTPError as error: raise_from( ConnectionUnavailable("Cannot open connection to %r", profile), error)
def test_profile_from_profile(): prof1 = ConnectionProfile(password="******") prof2 = ConnectionProfile(prof1) data = dict(prof2) assert data == { 'address': IPv4Address(('localhost', 7687)), 'auth': ('neo4j', 'secret'), 'host': 'localhost', 'password': '******', 'port': 7687, 'port_number': 7687, 'protocol': 'bolt', 'scheme': 'bolt', 'secure': False, 'verify': True, 'uri': 'bolt://neo4j@localhost:7687', 'user': '******', }
def __init__(self, profile=None, **settings): from py2neo.client import Connector from py2neo.client.config import ConnectionProfile profile = ConnectionProfile(profile, **settings) connector_settings = { "user_agent": settings.get("user_agent"), "init_size": settings.get("init_size"), "max_size": settings.get("max_size"), "max_age": settings.get("max_age"), "routing": settings.get("routing"), } self._connector = Connector(profile, **connector_settings) self._graphs = {}
def __init__(self, profile, user_agent=None, max_size=None, max_age=None, on_bind=None, on_unbind=None, on_broken=None): self._profile = profile or ConnectionProfile() self._user_agent = user_agent self._max_size = max_size or self.default_max_size self._max_age = max_age or self.default_max_age self._on_bind = on_bind self._on_unbind = on_unbind self._on_broken = on_broken self._in_use_list = deque() self._quarantine = deque() self._free_list = deque() self._supports_multi = False
def __init__(self, profile, user_agent, max_size, max_age, on_bind, on_unbind): self._profile = profile or ConnectionProfile() self._user_agent = user_agent self._max_size = max_size or self.default_max_size self._max_age = max_age or self.default_max_age self._on_bind = on_bind self._on_unbind = on_unbind self._in_use_list = deque() self._quarantine = deque() self._free_list = deque() self._waiting_list = WaitingList( ) # TODO: consider moving this to Connector self._supports_multi = False
def test_https_default_port(): data = dict(ConnectionProfile("https://host")) assert data == { 'address': IPv4Address(('host', 7473)), 'auth': ('neo4j', 'password'), 'host': 'host', 'password': '******', 'port': 7473, 'port_number': 7473, 'protocol': 'http', 'scheme': 'https', 'secure': True, 'verify': True, 'uri': 'https://neo4j@host:7473', 'user': '******', }
def test_uri_and_port(): data = dict(ConnectionProfile("bolt://*****:*****@host:8888', 'user': '******', }
def test_https_uri_without_secure(): data = dict(ConnectionProfile("https://*****:*****@host:9999', 'user': '******', }
def test_explicit_auth_tuple(): data = dict(ConnectionProfile(auth=("bob", "secret"))) assert data == { 'address': IPv4Address(('localhost', 7687)), 'auth': ('bob', 'secret'), 'host': 'localhost', 'password': '******', 'port': 7687, 'port_number': 7687, 'protocol': 'bolt', 'scheme': 'bolt', 'secure': False, 'verify': True, 'uri': 'bolt://bob@localhost:7687', 'user': '******', }
def __new__(cls, profile=None, **settings): profile = ConnectionProfile(profile, **settings) try: inst = cls._instances[profile] except KeyError: inst = super(GraphService, cls).__new__(cls) connector_settings = { "user_agent": settings.get("user_agent"), "init_size": settings.get("init_size"), "max_size": settings.get("max_size"), "max_age": settings.get("max_age"), } inst._connector = Connector.open(profile, **connector_settings) inst._graphs = {} cls._instances[profile] = inst return inst
def __init__(self, profile=None, **settings): from py2neo.client import Connector from py2neo.client.config import ConnectionProfile profile = ConnectionProfile(profile, **settings) connector_settings = { "user_agent": settings.get("user_agent"), "init_size": settings.get("init_size"), "max_size": settings.get("max_size"), "max_age": settings.get("max_age"), "routing": settings.get("routing"), } if connector_settings["init_size"] is None and not connector_settings["routing"]: # Ensures credentials are checked on construction connector_settings["init_size"] = 1 self._connector = Connector(profile, **connector_settings) self._graphs = {}
def test_bolt_uri_only(): data = dict(ConnectionProfile("bolt://*****:*****@host:9999', 'user': '******', }
def test_default_profile(): data = dict(ConnectionProfile()) assert data == { 'address': IPv4Address(('localhost', 7687)), 'auth': ('neo4j', 'password'), 'host': 'localhost', 'password': '******', 'port': 7687, 'port_number': 7687, 'protocol': 'bolt', 'scheme': 'bolt', 'secure': False, 'verify': True, 'uri': 'bolt://neo4j@localhost:7687', 'user': '******', }
def __init__(self, profile, user_agent=None, init_size=None, max_size=None, max_age=None, routing=False): self._profile = ConnectionProfile(profile) self._user_agent = user_agent self._init_size = init_size self._max_size = max_size self._max_age = max_age self._transactions = {} self._pools = {} self._add_pools(self._profile) if routing: self._routers = [] self._routing_tables = {} self._refresh_routing_table(None) else: self._routers = None self._routing_tables = None
def _get_routing_info(self, graph_name, query, parameters): # This import is not ideal as it breaks abstraction layers, # but it's necessary until the ROUTE message is available. from py2neo import ClientError try: result = self.auto_run(graph_name, query, parameters) self.pull(result) for ttl, address_data in result.records(): addresses = {} for a in address_data: addresses[a["role"]] = [ConnectionProfile(address=address) for address in a["addresses"]] return addresses["ROUTE"], addresses["READ"], addresses["WRITE"], ttl except ClientError as error: if error.title == "ProcedureNotFound": raise_from(TypeError("Neo4j service does not support routing"), error) else: raise
def _get_routing_info(self, graph_name, query, parameters): try: result = self.auto_run(graph_name, query, parameters) self.pull(result) for ttl, address_data in result.records(): addresses = {} for a in address_data: addresses[a["role"]] = [ConnectionProfile(self.profile, address=address) for address in a["addresses"]] return (addresses.get("ROUTE", []), addresses.get("READ", []), addresses.get("WRITE", []), ttl) except Neo4jError as error: if error.title == "ProcedureNotFound": raise_from(TypeError("Neo4j service does not support routing"), error) else: raise
def __new__(cls, profile=None, **settings): from py2neo.client import Connector from py2neo.client.config import ConnectionProfile profile = ConnectionProfile(profile, **settings) try: inst = cls._instances[profile] except KeyError: inst = super(GraphService, cls).__new__(cls) connector_settings = { "user_agent": settings.get("user_agent"), "init_size": settings.get("init_size"), "max_size": settings.get("max_size"), "max_age": settings.get("max_age"), "routing": settings.get("routing"), } inst._connector = Connector(profile, **connector_settings) inst._graphs = {} cls._instances[profile] = inst return inst
def test_loading_profile_from_file(): filename = path.join(path.dirname(__file__), "..", "..", "resources", "example.ini") prof = ConnectionProfile.from_file(filename, "Neo4j") data = dict(prof) assert data == { 'secure': False, 'verify': True, 'scheme': 'bolt', 'user': '******', 'password': '******', 'address': IPv4Address(('graph.mystery.inc', 7777)), 'auth': ('shaggy', 'velma'), 'host': 'graph.mystery.inc', 'port': 7777, 'port_number': 7777, 'protocol': 'bolt', 'uri': 'bolt://[email protected]:7777', }
def test_hash_equality(): prof1 = ConnectionProfile() prof2 = ConnectionProfile() assert hash(prof1) == hash(prof2)
def test_profile_from_invalid_argument(): with raises(TypeError): _ = ConnectionProfile(object())
from py2neo.client.config import ConnectionProfile from py2neo.ogm import Repository from .models import Movie, Person HOME = dirname(__file__) STATIC = path.join(HOME, "static") VIEWS = path.join(HOME, "views") CONFIG = path.join(HOME, "movies.ini") # Update the template search path used by Bottle TEMPLATE_PATH.append(VIEWS) # Load the connection details from the config file profile = ConnectionProfile.from_file(CONFIG, "Neo4j") # Set up a link to the local graph database. repo = Repository(profile) @get("/static/<filename>") def get_static(filename): """ Static file accessor. """ return static_file(filename, root=STATIC) @get("/") def get_index(): """ Index page.
def test_auth_env_var(): from py2neo.client import config config.NEO4J_AUTH = "alice:python" prof = ConnectionProfile() assert prof.auth == ("alice", "python")
def test_uri_env_var(): from py2neo.client import config config.NEO4J_URI = "http://alice@somewhere:8899" prof = ConnectionProfile() assert prof.uri == "http://alice@somewhere:8899"
def test_profile_repr(): prof = ConnectionProfile() assert repr(prof).startswith("ConnectionProfile")
def test_get_non_existent_key(): prof = ConnectionProfile() with raises(KeyError): _ = prof["routing"]
def test_explicit_address_string(): profile = ConnectionProfile(address="victor:4291") assert profile.host == "victor" assert profile.port == 4291
def test_equality(): prof1 = ConnectionProfile() prof2 = ConnectionProfile() assert prof1 == prof2
def test_length(): prof1 = ConnectionProfile() assert len(prof1) == 12