Bugfix in shelve

This commit is contained in:
Anne de Jong 2020-03-09 13:29:14 +01:00
parent 47eebbcdf4
commit 09c6b2680b

View File

@ -39,60 +39,79 @@ class Shelve:
return default_value return default_value
def __enter__(self): def __enter__(self):
if self.shelve is None: self.incref()
assert self.refcount == 0
self.shelve = shelve.open(self.shelve_fn())
self.refcount += 1
return self return self
def store(self, key, val): def store(self, key, val):
self.shelve[key] = val self._shelve[key] = val
def deleteIfPresent(self, key): def deleteIfPresent(self, key):
try: try:
del self.shelve[key] del self._shelve[key]
except: except:
pass pass
def printAllKeys(self): def printAllKeys(self):
print(list(self.shelve.keys())) print(list(self.shelve.keys()))
@property def incref(self):
def shelve(self): if self.shelve is None:
raise NotImplementedError() assert self.refcount == 0
self.shelve = shelve.open(self.shelve_fn())
self.refcount += 1
def __exit__(self, type, value, traceback): def decref(self):
self.refcount -= 1 self.refcount -= 1
if self.refcount == 0: if self.refcount == 0:
self.shelve.close() self.shelve.close()
self.shelve = None self.shelve = None
def __exit__(self, type, value, traceback):
self.decref()
class lasp_shelve(Shelve): class lasp_shelve(Shelve):
refcount = 0 _refcount = 0
_shelve = None _shelve = None
@property
def refcount(self):
return lasp_shelve._refcount
@refcount.setter
def refcount(self, val):
lasp_shelve._refcount = val
@property @property
def shelve(self): def shelve(self):
return self._shelve return lasp_shelve._shelve
@shelve.setter @shelve.setter
def shelve(self, shelve): def shelve(self, shelve):
self._shelve = shelve lasp_shelve._shelve = shelve
def shelve_fn(self): def shelve_fn(self):
return os.path.join(lasp_appdir, 'config.shelve') return os.path.join(lasp_appdir, 'config.shelve')
class this_lasp_shelve(Shelve): class this_lasp_shelve(Shelve):
refcount = 0 _refcount = 0
_shelve = None _shelve = None
@property
def refcount(self):
return this_lasp_shelve._refcount
@refcount.setter
def refcount(self, val):
this_lasp_shelve._refcount = val
@property @property
def shelve(self): def shelve(self):
return self._shelve return this_lasp_shelve._shelve
@shelve.setter @shelve.setter
def shelve(self, shelve): def shelve(self, shelve):
self._shelve = shelve this_lasp_shelve._shelve = shelve
def shelve_fn(self): def shelve_fn(self):
node = platform.node() node = platform.node()