Configuration for SPPAS application.
An instance of this class has to be created for any application using the SPPAS API.
Members are:
- log_level
- splash_delay
- features dictionary
Configuration for SPPAS application.
An instance of this class has to be created for any application using the SPPAS API.
Members are:
Create a new sppasAppConfig instance.
The configuration is set to its default values and updated with the content of a configuration file (if existing).
The configuration is saved the 1st time this class is instantiated and when this class is deleted.
def __init__(self):
"""Create a new sppasAppConfig instance.
The configuration is set to its default values and updated
with the content of a configuration file (if existing).
The configuration is saved the 1st time this class is instantiated
and when this class is deleted.
"""
super(sppasAppConfig, self).__init__()
self.__log_level = sppasAppConfig.DEFAULT_LOG_LEVEL
self.__feat_ids = dict()
self.load()
Return the level for logging messages, ranging 0-50.
def get_log_level(self):
"""Return the level for logging messages, ranging 0-50."""
return self.__log_level
Set the log level for the application.
def set_log_level(self, value):
"""Set the log level for the application.
:param value: (int) Logging ranges from 0 (any) to 50 (critical only).
"""
value = int(value)
if value < 0:
value = 0
if value > 50:
value = 50
self.__log_level = value
Return the name of the config file.
@staticmethod
def cfg_filename():
"""Return the name of the config file."""
return os.path.join(paths.basedir, sppasAppConfig.APP_CONFIG_FILENAME)
Load the configuration from a file.
def load(self):
"""Load the configuration from a file."""
cfg = self.cfg_filename()
if os.path.exists(cfg) is False:
self.save()
else:
with open(self.cfg_filename()) as cfg:
try:
d = json.load(cfg)
except json.decoder.JSONDecodeError:
self.save()
else:
self.__log_level = d.get('log_level', sppasAppConfig.DEFAULT_LOG_LEVEL)
deps = d.get('features', dict())
for key in deps:
self.__feat_ids[key] = deps[key]
Save into a JSON file.
def save(self):
"""Save into a JSON file."""
self.__hide(False)
with open(self.cfg_filename(), 'w') as f:
d = dict()
d['log_level'] = self.__log_level
d['features'] = self.__feat_ids
f.write(json.dumps(d, indent=2))
self.__hide(True)
Return the list of feature identifiers currently known.
def get_feature_ids(self):
"""Return the list of feature identifiers currently known."""
return list(self.__feat_ids.keys())
Return True if a feature was successfully installed by SPPAS.
def feature_installed(self, key):
"""Return True if a feature was successfully installed by SPPAS.
:param key: (str) Identifier of a feature.
"""
if key not in self.__feat_ids:
return False
return self.__feat_ids[key]
Add or update a feature.
This change is set to the current dict but is not saved in the configuration file.
def set_feature(self, key, value):
"""Add or update a feature.
This change is set to the current dict but is not saved in the
configuration file.
:param key: (str) Identifier of a feature
:param value: (bool) Installed or disabled
"""
self.__feat_ids[key] = bool(value)
Hide or un-hide a file.
def __hide(self, value):
"""Hide or un-hide a file.
:param value: (bool) Hide the config filename
"""
filename = self.cfg_filename()
system = sys.platform
if system == 'win32':
oper = '+'
if value is False:
oper = '-'
p = os.popen('attrib ' + oper + 'h ' + filename)
p.close()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
pass