Skip to content

Commit af67939

Browse files
committed
Merge pull request #73 from neo4j/1.1-decoupling
Decoupling connection and session plus a bit of refactoring
2 parents 01970c4 + 681ca97 commit af67939

File tree

7 files changed

+313
-247
lines changed

7 files changed

+313
-247
lines changed

neo4j/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,12 @@
2020

2121

2222
from .meta import version as __version__
23+
24+
# Export current (v1) API. This should be updated to export the latest
25+
# version of the API when a new one is added. This gives the option to
26+
# `import neo4j.vX` for a specific version or `import neo4j` for the
27+
# latest.
28+
from .v1.constants import *
29+
from .v1.exceptions import *
30+
from .v1.session import *
31+
from .v1.types import *

neo4j/util.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,8 @@
1919
# limitations under the License.
2020

2121

22-
from __future__ import unicode_literals
23-
2422
import logging
25-
from argparse import ArgumentParser
26-
from json import loads as json_loads
27-
from sys import stdout, stderr
28-
29-
from .v1.session import GraphDatabase, CypherError
23+
from sys import stdout
3024

3125

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

5145

5246
class Watcher(object):
53-
""" Log watcher for debug output.
47+
""" Log watcher for monitoring driver and protocol activity.
5448
"""
5549

5650
handlers = {}
@@ -74,3 +68,16 @@ def stop(self):
7468
self.logger.removeHandler(self.handlers[self.logger_name])
7569
except KeyError:
7670
pass
71+
72+
73+
def watch(logger_name, level=logging.INFO, out=stdout):
74+
""" Quick wrapper for using the Watcher.
75+
76+
:param logger_name: name of logger to watch
77+
:param level: minimum log level to show (default INFO)
78+
:param out: where to send output (default stdout)
79+
:return: Watcher instance
80+
"""
81+
watcher = Watcher(logger_name)
82+
watcher.watch(level, out)
83+
return watcher

neo4j/v1/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# See the License for the specific language governing permissions and
1919
# limitations under the License.
2020

21-
from .connection import ProtocolError
2221
from .constants import *
22+
from .exceptions import *
2323
from .session import *
2424
from .types import *

neo4j/v1/connection.py renamed to neo4j/v1/bolt.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
from socket import create_connection, SHUT_RDWR, error as SocketError
3232
from struct import pack as struct_pack, unpack as struct_unpack, unpack_from as struct_unpack_from
3333

34-
import errno
35-
3634
from .constants import DEFAULT_PORT, DEFAULT_USER_AGENT, KNOWN_HOSTS, MAGIC_PREAMBLE, \
3735
TRUST_DEFAULT, TRUST_ON_FIRST_USE
3836
from .compat import hex2
@@ -239,6 +237,13 @@ def on_failure(metadata):
239237
def __del__(self):
240238
self.close()
241239

240+
@property
241+
def healthy(self):
242+
""" Return ``True`` if this connection is healthy, ``False`` if
243+
unhealthy and ``None`` if closed.
244+
"""
245+
return None if self.closed else not self.defunct
246+
242247
def append(self, signature, fields=(), response=None):
243248
""" Add a message to the outgoing queue.
244249
@@ -333,6 +338,12 @@ def fetch(self):
333338
handler(*fields)
334339
raw.close()
335340

341+
def fetch_all(self):
342+
while self.responses:
343+
response = self.responses[0]
344+
while not response.complete:
345+
self.fetch()
346+
336347
def close(self):
337348
""" Close the connection.
338349
"""
@@ -389,26 +400,26 @@ def match_or_trust(self, host, der_encoded_certificate):
389400
return True
390401

391402

392-
def connect(host, port=None, ssl_context=None, **config):
403+
def connect(host_port, ssl_context=None, **config):
393404
""" Connect and perform a handshake and return a valid Connection object, assuming
394405
a protocol version can be agreed.
395406
"""
396407

397408
# Establish a connection to the host and port specified
398409
# Catches refused connections see:
399410
# https://docs.python.org/2/library/errno.html
400-
port = port or DEFAULT_PORT
401-
if __debug__: log_info("~~ [CONNECT] %s %d", host, port)
411+
if __debug__: log_info("~~ [CONNECT] %s", host_port)
402412
try:
403-
s = create_connection((host, port))
413+
s = create_connection(host_port)
404414
except SocketError as error:
405415
if error.errno == 111 or error.errno == 61:
406-
raise ProtocolError("Unable to connect to %s on port %d - is the server running?" % (host, port))
416+
raise ProtocolError("Unable to connect to %s on port %d - is the server running?" % host_port)
407417
else:
408418
raise
409419

410420
# Secure the connection if an SSL context has been provided
411421
if ssl_context and SSL_AVAILABLE:
422+
host, port = host_port
412423
if __debug__: log_info("~~ [SECURE] %s", host)
413424
try:
414425
s = ssl_context.wrap_socket(s, server_hostname=host if HAS_SNI else None)

0 commit comments

Comments
 (0)