Added a machine-local LASP shelve, next to the overall (multiple machine-wide) one

This commit is contained in:
Anne de Jong 2020-03-01 15:06:01 +01:00
parent 3d5e9290ff
commit 945a960f1c

View File

@ -1,19 +1,25 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os
import platform
import shelve
import appdirs
import numpy as np import numpy as np
from .wrappers import Window as wWindow from .wrappers import Window as wWindow
import appdirs, os, shelve
""" """
Common definitions used throughout the code. Common definitions used throughout the code.
""" """
__all__ = ['P_REF', 'FreqWeighting', 'TimeWeighting', 'getTime', __all__ = [
'getFreq', 'lasp_shelve', 'P_REF', 'FreqWeighting', 'TimeWeighting', 'getTime', 'getFreq',
'W_REF', 'U_REF', 'I_REF'] 'lasp_shelve', 'this_lasp_shelve', 'W_REF', 'U_REF', 'I_REF'
]
lasp_appdir = appdirs.user_data_dir('Lasp', 'ASCEE') lasp_appdir = appdirs.user_data_dir('Lasp', 'ASCEE')
if not os.path.exists(lasp_appdir): if not os.path.exists(lasp_appdir):
try: try:
os.mkdir(lasp_appdir) os.mkdir(lasp_appdir)
@ -29,7 +35,8 @@ class lasp_shelve:
def __enter__(self): def __enter__(self):
if lasp_shelve.shelve is None: if lasp_shelve.shelve is None:
assert lasp_shelve.refcount == 0 assert lasp_shelve.refcount == 0
lasp_shelve.shelve = shelve.open(os.path.join(lasp_appdir, 'config.shelve')) lasp_shelve.shelve = shelve.open(
os.path.join(lasp_appdir, 'config.shelve'))
lasp_shelve.refcount += 1 lasp_shelve.refcount += 1
return lasp_shelve.shelve return lasp_shelve.shelve
@ -39,6 +46,27 @@ class lasp_shelve:
lasp_shelve.shelve.close() lasp_shelve.shelve.close()
lasp_shelve.shelve = None lasp_shelve.shelve = None
class this_lasp_shelve:
refcount = 0
shelve = None
def __enter__(self):
if this_lasp_shelve.shelve is None:
assert lasp_shelve.refcount == 0
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):
this_lasp_shelve.refcount -= 1
if this_lasp_shelve.refcount == 0:
this_lasp_shelve.shelve.close()
this_lasp_shelve.shelve = None
# Reference sound pressure level # Reference sound pressure level
P_REF = 2e-5 P_REF = 2e-5