Skip to content

Commit

Permalink
Move Singleton from Backend to Repository
Browse files Browse the repository at this point in the history
  • Loading branch information
romaryd committed Jan 5, 2018
1 parent 8d28b51 commit 8c1c78c
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 7 deletions.
3 changes: 0 additions & 3 deletions jsonrepo/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
Author: Romary Dupuis <romary@me.com>
Copyright (C) 2017 Romary Dupuis
"""
from six import add_metaclass
from singleton import Singleton


@add_metaclass(Singleton)
class Backend(object):
""" Basic backend class """
def __init__(self, prefix):
Expand Down
3 changes: 3 additions & 0 deletions jsonrepo/backends/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def history(self, key, _from='-', _to='+', _desc=True):
return []

def latest(self, key):
self.logger.debug('Storage - get latest for {}'.format(
self.prefixed(key)
))
response = self.dynamodb_server.query(
KeyConditionExpression=Key(self._key)
.eq(self.prefixed(key)),
Expand Down
3 changes: 3 additions & 0 deletions jsonrepo/backends/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def history(self, key, _from='-', _to='+', _desc=True):
return [self.get(key, kid) for kid in res]

def latest(self, key):
self.logger.debug('Storage - get latest for {}'.format(
self.prefixed(key)
))
if self.prefixed(key) not in self.cache:
return None
if len(self.cache[self.prefixed(key)]) == 0:
Expand Down
3 changes: 3 additions & 0 deletions jsonrepo/backends/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ def history(self, key, _from='-', _to='+', _desc=True):
return res

def latest(self, key):
self.logger.debug('Storage - get latest for {}'.format(
self.prefixed(key)
))
res = self.redis_server.zrevrangebylex(
self.prefixed(key),
'+', '-',
Expand Down
4 changes: 4 additions & 0 deletions jsonrepo/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def from_json(cls, json_dump):
How to get a context from a json dump
"""
context = cls()
if json_dump is None:
return None
ctxt = json.loads(json_dump)
for k in ctxt:
context[k] = ctxt[k]
Expand All @@ -73,6 +75,8 @@ class NamedtupleRecord(Record):
"""
@classmethod
def from_json(cls, json_dump):
if json_dump is None:
return None
kwargs = json.loads(json_dump)
return cls(**kwargs)

Expand Down
7 changes: 5 additions & 2 deletions jsonrepo/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
Author: Romary Dupuis <romary@me.com>
Copyright (C) 2017 Romary Dupuis
"""
from six import add_metaclass
from singleton import Singleton
from loggingmixin import LoggingMixin
from jsonrepo.mixin import StorageMixin
from jsonrepo.record import Record


@add_metaclass(Singleton)
class Repository(StorageMixin, LoggingMixin):
"""
Definition of a repository
Expand Down Expand Up @@ -41,13 +44,13 @@ def save(self, key, sort_key, _object):

def history(self, key, _from='-', _to='+', _desc=True):
"""
Saves a context object
Retrives a list of records according to a datetime range
"""
return [self.klass.from_json(_object)
for _object in self.storage.history(key, _from, _to, _desc)]

def latest(self, key):
"""
Saves a context object
Get the most recent record for a specific key
"""
return self.klass.from_json(self.storage.latest(key))
4 changes: 2 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def test_singleton(self):
"""
repo1 = MyRepository()
repo2 = MyRepository()
self.assertEqual(repo1.storage, repo2.storage)
self.assertEqual(id(repo1.storage), id(repo2.storage))
self.assertEqual(repo1, repo2)
self.assertEqual(id(repo1), id(repo2))

def test_save_record(self):
"""
Expand Down

0 comments on commit 8c1c78c

Please sign in to comment.