2019-05-07 14:25:25 +02:00
|
|
|
"""The Inventory singleton"""
|
2017-01-10 21:15:35 +01:00
|
|
|
|
2017-05-27 19:03:27 +02:00
|
|
|
# TODO make this dynamic, and watch out for frozen, like with messagetypes
|
|
|
|
import storage.sqlite
|
|
|
|
import storage.filesystem
|
2019-05-07 14:25:25 +02:00
|
|
|
from bmconfigparser import BMConfigParser
|
|
|
|
from singleton import Singleton
|
|
|
|
|
2017-01-10 21:15:35 +01:00
|
|
|
|
|
|
|
@Singleton
|
2017-05-27 19:03:27 +02:00
|
|
|
class Inventory():
|
2019-05-07 14:25:25 +02:00
|
|
|
"""
|
|
|
|
Inventory singleton class which uses storage backends
|
|
|
|
to manage the inventory.
|
|
|
|
"""
|
2017-01-10 21:15:35 +01:00
|
|
|
def __init__(self):
|
2017-05-27 19:03:27 +02:00
|
|
|
self._moduleName = BMConfigParser().safeGet("inventory", "storage")
|
2019-05-07 14:25:25 +02:00
|
|
|
self._inventoryClass = getattr(
|
|
|
|
getattr(storage, self._moduleName),
|
|
|
|
"{}Inventory".format(self._moduleName.title())
|
|
|
|
)
|
2017-05-27 19:03:27 +02:00
|
|
|
self._realInventory = self._inventoryClass()
|
2017-05-31 10:15:47 +02:00
|
|
|
self.numberOfInventoryLookupsPerformed = 0
|
2017-05-27 19:03:27 +02:00
|
|
|
|
|
|
|
# cheap inheritance copied from asyncore
|
|
|
|
def __getattr__(self, attr):
|
2019-05-07 14:25:25 +02:00
|
|
|
if attr == "__contains__":
|
|
|
|
self.numberOfInventoryLookupsPerformed += 1
|
2017-05-27 19:03:27 +02:00
|
|
|
try:
|
|
|
|
realRet = getattr(self._realInventory, attr)
|
|
|
|
except AttributeError:
|
2019-05-07 14:25:25 +02:00
|
|
|
raise AttributeError(
|
|
|
|
"%s instance has no attribute '%s'" %
|
|
|
|
(self.__class__.__name__, attr)
|
|
|
|
)
|
2017-05-27 19:03:27 +02:00
|
|
|
else:
|
|
|
|
return realRet
|
2019-05-07 14:25:25 +02:00
|
|
|
|
|
|
|
# hint for pylint: this is dictionary like object
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self._realInventory[key]
|