Skip to content

Commit

Permalink
refactor: use latest google-cloud-datastore in datastore_adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
JorinTielen committed Dec 4, 2018
1 parent 88859f6 commit dfa8a61
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
10 changes: 3 additions & 7 deletions anom/adapters/datastore_adapter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging

from functools import partial
from gcloud_requests import DatastoreRequestsProxy, enter_transaction, exit_transaction
from google.cloud import datastore
from threading import local

Expand Down Expand Up @@ -40,7 +39,6 @@ def begin(self):
_logger.debug("Beginning transaction...")
self.ds_transaction.begin()
self.adapter.client._push_batch(self.ds_transaction)
enter_transaction()

def commit(self):
try:
Expand All @@ -56,7 +54,6 @@ def rollback(self):

def end(self):
_logger.debug("Ending transaction...")
exit_transaction()
self.adapter.client._pop_batch()
self.adapter._transactions.remove(self)

Expand Down Expand Up @@ -99,11 +96,9 @@ class DatastoreAdapter(Adapter):
def __init__(self, *, project=None, credentials=None):
self.project = project
self.credentials = credentials
self.proxy = DatastoreRequestsProxy(credentials=credentials)
self.client = datastore.Client(
credentials=self.credentials,
project=self.project,
_http=self.proxy,
_use_grpc=False,
)

Expand Down Expand Up @@ -171,12 +166,13 @@ def query(self, query, options):

result_iterator = query.fetch(
limit=options.batch_size,
offset=options.offset,
start_cursor=options.cursor,
)

entities = []
for entity in result_iterator:
for i, entity in enumerate(result_iterator):
if i < options.offset:
continue
key, data = self._convert_key_from_datastore(entity.key), None
if not options.keys_only:
data = self._prepare_to_load(entity)
Expand Down
2 changes: 2 additions & 0 deletions anom/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ def prepare_to_load(self, entity, value):
# loaded as ints in microseconds.
if value is not None and isinstance(value, int):
value = datetime.fromtimestamp(value / 1000000, tz.tzutc())
elif value is not None and isinstance(value, datetime):
value = value.replace(tzinfo=tz.tzutc())

return super().prepare_to_load(entity, value)

Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
google-cloud-datastore>=1.0,<1.1
gcloud-requests==1.1.9
google-cloud-datastore>=1.0,<2.0
msgpack-python==0.4.8
python-dateutil==2.6.0

0 comments on commit dfa8a61

Please sign in to comment.