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

View File

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

View File

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

View File

@ -7,6 +7,7 @@ import tempfile
import time import time
import unittest import unittest
from pybitmessage.storage import storage
from pybitmessage.addresses import calculateInventoryHash from pybitmessage.addresses import calculateInventoryHash
from .partial import TestPartialRun from .partial import TestPartialRun
@ -42,3 +43,16 @@ class TestFilesystemInventory(TestPartialRun):
super(TestFilesystemInventory, cls).tearDownClass() super(TestFilesystemInventory, cls).tearDownClass()
cls.inventory.flush() cls.inventory.flush()
shutil.rmtree(os.path.join(cls.home, cls.inventory.topDir)) 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()