Improved shelve API.
This commit is contained in:
parent
945a960f1c
commit
271e46e4ad
@ -97,21 +97,18 @@ class DAQConfiguration:
|
|||||||
Returns a list of currently available configurations
|
Returns a list of currently available configurations
|
||||||
"""
|
"""
|
||||||
with lasp_shelve() as sh:
|
with lasp_shelve() as sh:
|
||||||
return sh['daqconfigs'] if 'daqconfigs' in sh.keys() else {}
|
return sh.load('daqconfigs', {})
|
||||||
|
|
||||||
def saveConfig(self, name):
|
def saveConfig(self, name):
|
||||||
with lasp_shelve() as sh:
|
with lasp_shelve() as sh:
|
||||||
if 'daqconfigs' in sh.keys():
|
cur_configs = self.loadConfigs()
|
||||||
cur_configs = sh['daqconfigs']
|
|
||||||
else:
|
|
||||||
cur_configs = {}
|
|
||||||
cur_configs[name] = self
|
cur_configs[name] = self
|
||||||
sh['daqconfigs'] = cur_configs
|
sh.store('daqconfigs', cur_configs)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def deleteConfig(name):
|
def deleteConfig(name):
|
||||||
with lasp_shelve() as sh:
|
with lasp_shelve() as sh:
|
||||||
cur_configs = sh['daqconfigs']
|
cur_configs = DAQConfiguration.loadConfigs()
|
||||||
del cur_configs[name]
|
del cur_configs[name]
|
||||||
sh['daqconfigs'] = cur_configs
|
sh.store('daqconfigs', cur_configs)
|
||||||
|
|
||||||
|
@ -27,44 +27,76 @@ if not os.path.exists(lasp_appdir):
|
|||||||
print('Fatal error: could not create application directory')
|
print('Fatal error: could not create application directory')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
class Shelve:
|
||||||
class lasp_shelve:
|
def load(self, key, default_value):
|
||||||
refcount = 0
|
"""
|
||||||
shelve = None
|
Load data from a given key, if key is not found, returns the
|
||||||
|
default value if key is not found
|
||||||
|
"""
|
||||||
|
if key in self.shelve.keys():
|
||||||
|
return self.shelve[key]
|
||||||
|
else:
|
||||||
|
return default_value
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
if lasp_shelve.shelve is None:
|
if self.shelve is None:
|
||||||
assert lasp_shelve.refcount == 0
|
assert self.refcount == 0
|
||||||
lasp_shelve.shelve = shelve.open(
|
self.shelve = shelve.open(self.shelve_fn())
|
||||||
os.path.join(lasp_appdir, 'config.shelve'))
|
self.refcount += 1
|
||||||
lasp_shelve.refcount += 1
|
return self
|
||||||
return lasp_shelve.shelve
|
|
||||||
|
def store(self, key, val):
|
||||||
|
self.shelve[key] = val
|
||||||
|
|
||||||
|
def deleteIfPresent(self, key):
|
||||||
|
try:
|
||||||
|
del self.shelve[key]
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def printAllKeys(self):
|
||||||
|
print(list(self.shelve.keys()))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def shelve(self):
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
def __exit__(self, type, value, traceback):
|
def __exit__(self, type, value, traceback):
|
||||||
lasp_shelve.refcount -= 1
|
self.refcount -= 1
|
||||||
if lasp_shelve.refcount == 0:
|
if self.refcount == 0:
|
||||||
lasp_shelve.shelve.close()
|
self.shelve.close()
|
||||||
lasp_shelve.shelve = None
|
self.shelve = None
|
||||||
|
|
||||||
|
class lasp_shelve(Shelve):
|
||||||
class this_lasp_shelve:
|
|
||||||
refcount = 0
|
refcount = 0
|
||||||
shelve = None
|
_shelve = None
|
||||||
|
|
||||||
def __enter__(self):
|
@property
|
||||||
if this_lasp_shelve.shelve is None:
|
def shelve(self):
|
||||||
assert lasp_shelve.refcount == 0
|
return self._shelve
|
||||||
node = platform.node()
|
|
||||||
this_lasp_shelve.shelve = shelve.open(
|
|
||||||
os.path.join(lasp_appdir, f'{node}_config.shelve'))
|
|
||||||
this_lasp_shelve.refcount += 1
|
|
||||||
return this_lasp_shelve.shelve
|
|
||||||
|
|
||||||
def __exit__(self, type, value, traceback):
|
@shelve.setter
|
||||||
this_lasp_shelve.refcount -= 1
|
def shelve(self, shelve):
|
||||||
if this_lasp_shelve.refcount == 0:
|
self._shelve = shelve
|
||||||
this_lasp_shelve.shelve.close()
|
|
||||||
this_lasp_shelve.shelve = None
|
def shelve_fn(self):
|
||||||
|
return os.path.join(lasp_appdir, 'config.shelve')
|
||||||
|
|
||||||
|
class this_lasp_shelve(Shelve):
|
||||||
|
refcount = 0
|
||||||
|
_shelve = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def shelve(self):
|
||||||
|
return self._shelve
|
||||||
|
|
||||||
|
@shelve.setter
|
||||||
|
def shelve(self, shelve):
|
||||||
|
self._shelve = shelve
|
||||||
|
|
||||||
|
def shelve_fn(self):
|
||||||
|
node = platform.node()
|
||||||
|
return os.path.join(lasp_appdir, f'{node}_config.shelve')
|
||||||
|
|
||||||
|
|
||||||
# Reference sound pressure level
|
# Reference sound pressure level
|
||||||
|
Loading…
Reference in New Issue
Block a user