Skip to content

Routing (and a few other bits) #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 2, 2016
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
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
branch = True
omit =
test/*
neo4j/util.py
neo4j/v1/compat.py
neo4j/v1/ssl_compat.py
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ docs/build
dist
*.egg-info
build

*/run/*

neo4j-community-*
neo4j-enterprise-*
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "neokit"]
path = neokit
url = https://github.com/neo-technology/neokit.git
20 changes: 13 additions & 7 deletions examples/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@
# (* "good reason" is defined as knowing what you are doing)


auth_token = basic_auth("neo4j", "neo4j")
auth_token = basic_auth("neotest", "neotest")


# Deliberately shadow the built-in print function to
# mute noise from example code.
def print(*args, **kwargs):
pass


class FreshDatabaseTestCase(ServerTestCase):
Expand All @@ -48,7 +54,7 @@ class MinimalWorkingExampleTestCase(FreshDatabaseTestCase):

def test_minimal_working_example(self):
# tag::minimal-example[]
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neotest", "neotest"))
session = driver.session()

session.run("CREATE (a:Person {name:'Arthur', title:'King'})")
Expand All @@ -65,33 +71,33 @@ class ExamplesTestCase(FreshDatabaseTestCase):

def test_construct_driver(self):
# tag::construct-driver[]
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neotest", "neotest"))
# end::construct-driver[]
return driver

def test_configuration(self):
# tag::configuration[]
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"), max_pool_size=10)
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neotest", "neotest"), max_pool_size=10)
# end::configuration[]
return driver

@skipUnless(SSL_AVAILABLE, "Bolt over TLS is not supported by this version of Python")
def test_tls_require_encryption(self):
# tag::tls-require-encryption[]
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"), encrypted=True)
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neotest", "neotest"), encrypted=True)
# end::tls-require-encryption[]

@skipUnless(SSL_AVAILABLE, "Bolt over TLS is not supported by this version of Python")
def test_tls_trust_on_first_use(self):
# tag::tls-trust-on-first-use[]
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"), encrypted=True, trust=TRUST_ON_FIRST_USE)
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neotest", "neotest"), encrypted=True, trust=TRUST_ON_FIRST_USE)
# end::tls-trust-on-first-use[]
assert driver

@skip("testing verified certificates not yet supported ")
def test_tls_signed(self):
# tag::tls-signed[]
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"), encrypted=True, trust=TRUST_SIGNED_CERTIFICATES)
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neotest", "neotest"), encrypted=True, trust=TRUST_SIGNED_CERTIFICATES)
# end::tls-signed[]
assert driver

Expand Down
71 changes: 71 additions & 0 deletions neo4j/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
# limitations under the License.


try:
from collections.abc import MutableSet
except ImportError:
from collections import MutableSet, OrderedDict
else:
from collections import OrderedDict
import logging
from sys import stdout

Expand Down Expand Up @@ -55,6 +61,13 @@ def __init__(self, logger_name):
self.logger = logging.getLogger(self.logger_name)
self.formatter = ColourFormatter("%(asctime)s %(message)s")

def __enter__(self):
self.watch()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.stop()

def watch(self, level=logging.INFO, out=stdout):
self.stop()
handler = logging.StreamHandler(out)
Expand All @@ -81,3 +94,61 @@ def watch(logger_name, level=logging.INFO, out=stdout):
watcher = Watcher(logger_name)
watcher.watch(level, out)
return watcher


class RoundRobinSet(MutableSet):

def __init__(self, elements=()):
self._elements = OrderedDict.fromkeys(elements)
self._current = None

def __repr__(self):
return "{%s}" % ", ".join(map(repr, self._elements))

def __contains__(self, element):
return element in self._elements

def __next__(self):
current = None
if self._elements:
if self._current is None:
self._current = 0
else:
self._current = (self._current + 1) % len(self._elements)
current = list(self._elements.keys())[self._current]
return current

def __iter__(self):
return iter(self._elements)

def __len__(self):
return len(self._elements)

def add(self, element):
self._elements[element] = None

def clear(self):
self._elements.clear()

def discard(self, element):
try:
del self._elements[element]
except KeyError:
pass

def next(self):
return self.__next__()

def remove(self, element):
try:
del self._elements[element]
except KeyError:
raise ValueError(element)

def update(self, elements=()):
self._elements.update(OrderedDict.fromkeys(elements))

def replace(self, elements=()):
e = self._elements
e.clear()
e.update(OrderedDict.fromkeys(elements))
Loading