-
Notifications
You must be signed in to change notification settings - Fork 572
Expand file tree
/
Copy pathinventory.py
More file actions
56 lines (47 loc) · 1.67 KB
/
inventory.py
File metadata and controls
56 lines (47 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""The Inventory"""
# TODO make this dynamic, and watch out for frozen, like with messagetypes
import storage.filesystem
import storage.sqlite
from bmconfigparser import config
def create_inventory_instance(backend="sqlite"):
"""
Create an instance of the inventory class
defined in `storage.<backend>`.
"""
return getattr(
getattr(storage, backend),
"{}Inventory".format(backend.title()))()
class Inventory(object):
"""
Inventory class which uses storage backends
to manage the inventory.
"""
def __init__(self):
self._moduleName = config.safeGet("inventory", "storage")
self._realInventory = create_inventory_instance(self._moduleName)
self.numberOfInventoryLookupsPerformed = 0
def __getattr__(self, attr):
"""cheap inheritance copied from asyncore"""
if attr == "__contains__":
self.numberOfInventoryLookupsPerformed += 1
try:
realRet = getattr(self._realInventory, attr)
except AttributeError:
raise AttributeError(
"%s instance has no attribute '%s'" %
(self.__class__.__name__, attr)
)
else:
return realRet
def __contains__(self, key):
"""
Look up inventory item by hash.
This method is needed due to how new-style classes work.
"""
self.numberOfInventoryLookupsPerformed += 1
return key in self._realInventory
def __getitem__(self, key):
"""hint for pylint, this is dictionary like object"""
return self._realInventory[key]
def __setitem__(self, key, value):
self._realInventory[key] = value