SPPAS 4.20

Module sppas.config

Class sppasAppConfig

Description

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

Constructor

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.

View Source
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()

Public functions

get_log_level

Return the level for logging messages, ranging 0-50.

View Source
def get_log_level(self):
    """Return the level for logging messages, ranging 0-50."""
    return self.__log_level
set_log_level

Set the log level for the application.

Parameters
  • value: (int) Logging ranges from 0 (any) to 50 (critical only).
View Source
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
cfg_filename

Return the name of the config file.

View Source
@staticmethod
def cfg_filename():
    """Return the name of the config file."""
    return os.path.join(paths.basedir, sppasAppConfig.APP_CONFIG_FILENAME)
load

Load the configuration from a file.

View Source
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

Save into a JSON file.

View Source
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)
get_feature_ids

Return the list of feature identifiers currently known.

View Source
def get_feature_ids(self):
    """Return the list of feature identifiers currently known."""
    return list(self.__feat_ids.keys())
feature_installed

Return True if a feature was successfully installed by SPPAS.

Parameters
  • key: (str) Identifier of a feature.
View Source
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]
set_feature

Add or update a feature.

This change is set to the current dict but is not saved in the

configuration file.

Parameters
  • key: (str) Identifier of a feature
  • value: (bool) Installed or disabled
View Source
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)

Protected functions

__hide

Hide or un-hide a file.

Parameters
  • value: (bool) Hide the config filename
View Source
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()

Overloads

__enter__
View Source
def __enter__(self):
    return self
__exit__
View Source
def __exit__(self, exc_type, exc_value, traceback):
    pass