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
56 changes: 43 additions & 13 deletions neo4j/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

__all__ = [
"__version__",
"READ_ACCESS",
"WRITE_ACCESS",
"GraphDatabase",
"Driver",
"BoltDriver",
Expand All @@ -32,18 +30,50 @@
]

from logging import getLogger
from urllib.parse import urlparse, parse_qs

from neo4j.addressing import Address
from neo4j.api import *
from neo4j.conf import Config, PoolConfig
from urllib.parse import (
urlparse,
parse_qs,
)

from neo4j.addressing import (
Address,
IPv4Address,
IPv6Address,
)
from neo4j.api import (
Auth,
AuthToken,
basic_auth,
kerberos_auth,
custom_auth,
Bookmark,
ServerInfo,
Version,
READ_ACCESS,
WRITE_ACCESS,
)
from neo4j.conf import (
Config,
PoolConfig,
)
from neo4j.meta import (
experimental,
get_user_agent,
version as __version__,
)
from neo4j.data import (
Record,
)
from neo4j.work.simple import (
Transaction,
Result,
ResultSummary,
Query,
Session,
SessionConfig,
unit_of_work,
)
from neo4j.exceptions import ServiceUnavailable
from neo4j.meta import experimental, get_user_agent, version as __version__


READ_ACCESS = "READ"
WRITE_ACCESS = "WRITE"


log = getLogger("neo4j")

Expand Down
4 changes: 4 additions & 0 deletions neo4j/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,7 @@ def from_bytes(cls, b):
if b[0] != 0 or b[1] != 0:
raise ValueError("First two bytes must contain zero")
return Version(b[-1], b[-2])


READ_ACCESS = "READ"
WRITE_ACCESS = "WRITE"
7 changes: 4 additions & 3 deletions neo4j/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ def ensure_routing_table_is_fresh(self, access_mode):

:return: `True` if an update was required, `False` otherwise.
"""
from neo4j import READ_ACCESS
from neo4j.api import READ_ACCESS
if self.routing_table.is_fresh(readonly=(access_mode == READ_ACCESS)):
return False
with self.refresh_lock:
Expand All @@ -676,7 +676,7 @@ def ensure_routing_table_is_fresh(self, access_mode):
return True

def _select_address(self, access_mode=None):
from neo4j import READ_ACCESS
from neo4j.api import READ_ACCESS
""" Selects the address with the fewest in-use connections.
"""
self.ensure_routing_table_is_fresh(access_mode)
Expand All @@ -695,7 +695,8 @@ def _select_address(self, access_mode=None):
return choice(addresses_by_usage[min(addresses_by_usage)])

def acquire(self, access_mode=None, timeout=None):
from neo4j import READ_ACCESS, WRITE_ACCESS
from neo4j.api import WRITE_ACCESS
from neo4j.api import READ_ACCESS
if access_mode is None:
access_mode = WRITE_ACCESS
if access_mode not in (READ_ACCESS, WRITE_ACCESS):
Expand Down
38 changes: 28 additions & 10 deletions neo4j/time/hydration.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,32 @@
# limitations under the License.


from datetime import time, datetime, timedelta

from pytz import FixedOffset, timezone, utc
from datetime import (
time,
datetime,
timedelta,
)

from neo4j.packstream import Structure
from neo4j.time import Duration, Date, Time, DateTime
from neo4j.time import (
Duration,
Date,
Time,
DateTime,
)


def get_date_unix_epoch():
return Date(1970, 1, 1)


def get_date_unix_epoch_ordinal():
return get_date_unix_epoch().to_ordinal()


UNIX_EPOCH_DATE = Date(1970, 1, 1)
UNIX_EPOCH_DATE_ORDINAL = UNIX_EPOCH_DATE.to_ordinal()
UNIX_EPOCH_DATETIME_UTC = DateTime(1970, 1, 1, 0, 0, 0, utc)
def get_datetime_unix_epoch_utc():
from pytz import utc
return DateTime(1970, 1, 1, 0, 0, 0, utc)


def hydrate_date(days):
Expand All @@ -38,7 +53,7 @@ def hydrate_date(days):
:param days:
:return: Date
"""
return Date.from_ordinal(UNIX_EPOCH_DATE_ORDINAL + days)
return Date.from_ordinal(get_date_unix_epoch_ordinal() + days)


def dehydrate_date(value):
Expand All @@ -48,7 +63,7 @@ def dehydrate_date(value):
:type value: Date
:return:
"""
return Structure(b"D", value.toordinal() - UNIX_EPOCH_DATE.toordinal())
return Structure(b"D", value.toordinal() - get_date_unix_epoch().toordinal())


def hydrate_time(nanoseconds, tz=None):
Expand All @@ -58,6 +73,7 @@ def hydrate_time(nanoseconds, tz=None):
:param tz:
:return: Time
"""
from pytz import FixedOffset
seconds, nanoseconds = map(int, divmod(nanoseconds, 1000000000))
minutes, seconds = map(int, divmod(seconds, 60))
hours, minutes = map(int, divmod(minutes, 60))
Expand Down Expand Up @@ -98,11 +114,12 @@ def hydrate_datetime(seconds, nanoseconds, tz=None):
:param tz:
:return: datetime
"""
from pytz import FixedOffset, timezone
minutes, seconds = map(int, divmod(seconds, 60))
hours, minutes = map(int, divmod(minutes, 60))
days, hours = map(int, divmod(hours, 24))
seconds = (1000000000 * seconds + nanoseconds) / 1000000000
t = DateTime.combine(Date.from_ordinal(UNIX_EPOCH_DATE_ORDINAL + days), Time(hours, minutes, seconds))
t = DateTime.combine(Date.from_ordinal(get_date_unix_epoch_ordinal() + days), Time(hours, minutes, seconds))
if tz is None:
return t
if isinstance(tz, int):
Expand Down Expand Up @@ -131,6 +148,7 @@ def seconds_and_nanoseconds(dt):
tz = value.tzinfo
if tz is None:
# without time zone
from pytz import utc
value = utc.localize(value)
seconds, nanoseconds = seconds_and_nanoseconds(value)
return Structure(b"d", seconds, nanoseconds)
Expand Down
2 changes: 1 addition & 1 deletion neo4j/work/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from time import perf_counter, sleep
from warnings import warn

from neo4j import READ_ACCESS, WRITE_ACCESS
from neo4j.api import READ_ACCESS, WRITE_ACCESS
from neo4j.conf import DeprecatedAlias
from neo4j.data import DataHydrator, DataDehydrator
from neo4j.exceptions import (
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_bookmarking.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from uuid import uuid4

from neo4j import WRITE_ACCESS, READ_ACCESS
from neo4j.api import READ_ACCESS, WRITE_ACCESS
from neo4j.graph import Node


Expand Down
2 changes: 1 addition & 1 deletion tests/stub/test_bookmarking.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

from neo4j import (
GraphDatabase,
READ_ACCESS,
)
from neo4j.api import READ_ACCESS

from tests.stub.conftest import StubCluster

Expand Down
3 changes: 1 addition & 2 deletions tests/stub/test_routingdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@

from neo4j import (
GraphDatabase,
READ_ACCESS,
WRITE_ACCESS,
Neo4jDriver,
)
from neo4j.api import READ_ACCESS, WRITE_ACCESS

from neo4j._exceptions import BoltRoutingError

Expand Down
Loading