Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions neo4j/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@


from .meta import version as __version__

# Export current (v1) API. This should be updated to export the latest
# version of the API when a new one is added. This gives the option to
# `import neo4j.vX` for a specific version or `import neo4j` for the
# latest.
from .v1.constants import *
from .v1.exceptions import *
from .v1.session import *
from .v1.types import *
23 changes: 15 additions & 8 deletions neo4j/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,8 @@
# limitations under the License.


from __future__ import unicode_literals

import logging
from argparse import ArgumentParser
from json import loads as json_loads
from sys import stdout, stderr

from .v1.session import GraphDatabase, CypherError
from sys import stdout


class ColourFormatter(logging.Formatter):
Expand All @@ -50,7 +44,7 @@ def format(self, record):


class Watcher(object):
""" Log watcher for debug output.
""" Log watcher for monitoring driver and protocol activity.
"""

handlers = {}
Expand All @@ -74,3 +68,16 @@ def stop(self):
self.logger.removeHandler(self.handlers[self.logger_name])
except KeyError:
pass


def watch(logger_name, level=logging.INFO, out=stdout):
""" Quick wrapper for using the Watcher.

:param logger_name: name of logger to watch
:param level: minimum log level to show (default INFO)
:param out: where to send output (default stdout)
:return: Watcher instance
"""
watcher = Watcher(logger_name)
watcher.watch(level, out)
return watcher
2 changes: 1 addition & 1 deletion neo4j/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from .connection import ProtocolError
from .constants import *
from .exceptions import *
from .session import *
from .types import *
25 changes: 18 additions & 7 deletions neo4j/v1/connection.py → neo4j/v1/bolt.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
from socket import create_connection, SHUT_RDWR, error as SocketError
from struct import pack as struct_pack, unpack as struct_unpack, unpack_from as struct_unpack_from

import errno

from .constants import DEFAULT_PORT, DEFAULT_USER_AGENT, KNOWN_HOSTS, MAGIC_PREAMBLE, \
TRUST_DEFAULT, TRUST_ON_FIRST_USE
from .compat import hex2
Expand Down Expand Up @@ -239,6 +237,13 @@ def on_failure(metadata):
def __del__(self):
self.close()

@property
def healthy(self):
""" Return ``True`` if this connection is healthy, ``False`` if
unhealthy and ``None`` if closed.
"""
return None if self.closed else not self.defunct

def append(self, signature, fields=(), response=None):
""" Add a message to the outgoing queue.

Expand Down Expand Up @@ -333,6 +338,12 @@ def fetch(self):
handler(*fields)
raw.close()

def fetch_all(self):
while self.responses:
response = self.responses[0]
while not response.complete:
self.fetch()

def close(self):
""" Close the connection.
"""
Expand Down Expand Up @@ -389,26 +400,26 @@ def match_or_trust(self, host, der_encoded_certificate):
return True


def connect(host, port=None, ssl_context=None, **config):
def connect(host_port, ssl_context=None, **config):
""" Connect and perform a handshake and return a valid Connection object, assuming
a protocol version can be agreed.
"""

# Establish a connection to the host and port specified
# Catches refused connections see:
# https://docs.python.org/2/library/errno.html
port = port or DEFAULT_PORT
if __debug__: log_info("~~ [CONNECT] %s %d", host, port)
if __debug__: log_info("~~ [CONNECT] %s", host_port)
try:
s = create_connection((host, port))
s = create_connection(host_port)
except SocketError as error:
if error.errno == 111 or error.errno == 61:
raise ProtocolError("Unable to connect to %s on port %d - is the server running?" % (host, port))
raise ProtocolError("Unable to connect to %s on port %d - is the server running?" % host_port)
else:
raise

# Secure the connection if an SSL context has been provided
if ssl_context and SSL_AVAILABLE:
host, port = host_port
if __debug__: log_info("~~ [SECURE] %s", host)
try:
s = ssl_context.wrap_socket(s, server_hostname=host if HAS_SNI else None)
Expand Down
Loading