Simplify storage abstract base definition

This commit is contained in:
Lee Miller 2022-09-21 01:02:36 +03:00
parent 81f574c618
commit 8af1a13e10
Signed by untrusted user: lee.miller
GPG Key ID: 4F97A5EA88F4AB63
4 changed files with 42 additions and 49 deletions

View File

@ -16,7 +16,6 @@ logger = logging.getLogger('default')
class FilesystemInventory(InventoryStorage):
"""Filesystem for inventory storage"""
# pylint: disable=too-many-ancestors, abstract-method
topDir = "inventory"
objectDir = "objects"
metadataFilename = "metadata"
@ -46,6 +45,9 @@ class FilesystemInventory(InventoryStorage):
return True
return False
def __delitem__(self, hash_):
raise NotImplementedError
def __getitem__(self, hashval):
for streamDict in self._inventory.values():
try:

View File

@ -9,7 +9,7 @@ from helper_sql import SqlBulkExecute, sqlExecute, sqlQuery
from .storage import InventoryItem, InventoryStorage
class SqliteInventory(InventoryStorage): # pylint: disable=too-many-ancestors
class SqliteInventory(InventoryStorage):
"""Inventory using SQLite"""
def __init__(self):
super(SqliteInventory, self).__init__()

View File

@ -2,77 +2,54 @@
Storing inventory items
"""
from abc import ABCMeta, abstractmethod
from collections import namedtuple
try:
from collections import MutableMapping # pylint: disable=deprecated-class
except ImportError:
from collections.abc import MutableMapping
import six
InventoryItem = namedtuple('InventoryItem', 'type stream payload expires tag')
class Storage(object): # pylint: disable=too-few-public-methods
"""Base class for storing inventory
(extendable for other items to store)"""
pass
class InventoryStorage(MutableMapping):
"""
Base class for storing inventory
(extendable for other items to store)
"""
class InventoryStorage(Storage, MutableMapping):
"""Module used for inventory storage"""
def __init__(self): # pylint: disable=super-init-not-called
def __init__(self):
self.numberOfInventoryLookupsPerformed = 0
def __contains__(self, _):
raise NotImplementedError
def __getitem__(self, _):
raise NotImplementedError
def __setitem__(self, _, value):
raise NotImplementedError
def __delitem__(self, _):
raise NotImplementedError
def __iter__(self):
raise NotImplementedError
def __len__(self):
raise NotImplementedError
@abstractmethod
def __contains__(self, item):
pass
@abstractmethod
def by_type_and_tag(self, objectType, tag):
"""Return objects filtered by object type and tag"""
raise NotImplementedError
pass
@abstractmethod
def unexpired_hashes_by_stream(self, stream):
"""Return unexpired inventory vectors filtered by stream"""
raise NotImplementedError
pass
@abstractmethod
def flush(self):
"""Flush cache"""
raise NotImplementedError
pass
@abstractmethod
def clean(self):
"""Free memory / perform garbage collection"""
raise NotImplementedError
pass
class MailboxStorage(Storage, MutableMapping):
"""Method for storing mails"""
def __delitem__(self, key):
raise NotImplementedError
def __getitem__(self, key):
raise NotImplementedError
def __iter__(self):
raise NotImplementedError
def __len__(self):
raise NotImplementedError
def __setitem__(self, key, value):
raise NotImplementedError
@six.add_metaclass(ABCMeta)
class MailboxStorage(MutableMapping):
"""An abstract class for storing mails. TODO"""
pass

View File

@ -7,6 +7,7 @@ import tempfile
import time
import unittest
from pybitmessage.storage import storage
from pybitmessage.addresses import calculateInventoryHash
from .partial import TestPartialRun
@ -42,3 +43,16 @@ class TestFilesystemInventory(TestPartialRun):
super(TestFilesystemInventory, cls).tearDownClass()
cls.inventory.flush()
shutil.rmtree(os.path.join(cls.home, cls.inventory.topDir))
class TestStorageAbstract(unittest.TestCase):
"""A test case for refactoring of the storage abstract classes"""
def test_inventory_storage(self):
"""Check inherited abstract methods"""
with self.assertRaisesRegexp(
TypeError, "^Can't instantiate abstract class.*"
"methods __contains__, __delitem__, __getitem__, __iter__,"
" __len__, __setitem__"
): # pylint: disable=abstract-class-instantiated
storage.InventoryStorage()