sppas 5.1.0

https://sppas.org/

Module sppas.core.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
  • interoperability
  • features dictionary

Constructor

__init__

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.__update_info = None
    self.__log_level = sppasAppConfig.DEFAULT_LOG_LEVEL
    self.__interoperability = sppasAppConfig.DEFAULT_INTEROPERABILITY
    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

get_interoperability

Return True if a file is written without loss of information.

View Source
def get_interoperability(self):
    """Return True if a file is written without loss of information."""
    return self.__interoperability

set_interoperability

Set whether a file is written without loss of information.

When it is False, what a format can't hold is lost: it's the choice of the user, the default is to preserve it.

Parameters
  • value: (bool) Preserve what the destination format can't hold.
View Source
def set_interoperability(self, value):
    """Set whether a file is written without loss of information.

        When it is False, what a format can't hold is lost: it's the choice
        of the user, the default is to preserve it.

        :param value: (bool) Preserve what the destination format can't hold.

        """
    self.__interoperability = bool(value)

set_update_info

Store update check result.

Parameters
  • info
View Source
def set_update_info(self, info):
    """Store update check result."""
    self.__update_info = info

get_update_info

Return update check result.

View Source
def get_update_info(self):
    """Return update check result."""
    return self.__update_info

cfg_filename

Return the name of the config file.

The file says which features are installed, and they are installed in the environment SPPAS is running in: two installed versions have their own, and neither writes into the other. It is stored with the other files of the user, and not in the environment: an environment created by somebody to install SPPAS with pip is theirs, and SPPAS leaves no file of its own in it.

The name carries the environment: the name of its directory, and a short digest of its full path -- two environments named alike are still told apart. It begins with a dot, like every other file SPPAS leaves among the files of the user: they are its own, not theirs.

View Source
@staticmethod
def cfg_filename():
    """Return the name of the config file.

        The file says which features are installed, and they are installed
        in the environment SPPAS is running in: two installed versions have
        their own, and neither writes into the other. It is stored with the
        other files of the user, and not in the environment: an environment
        created by somebody to install SPPAS with pip is theirs, and SPPAS
        leaves no file of its own in it.

        The name carries the environment: the name of its directory, and a
        short digest of its full path -- two environments named alike are
        still told apart. It begins with a dot, like every other file SPPAS
        leaves among the files of the user: they are its own, not theirs.

        """
    prefix = os.path.abspath(sys.prefix)
    digest = hashlib.sha1(prefix.encode('utf-8')).hexdigest()[:8]
    name = '.{:s}-{:s}{:s}'.format(os.path.basename(prefix), digest, sppasAppConfig.APP_CONFIG_FILENAME)
    return os.path.join(paths.ext_dir, name)

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)
                self.__interoperability = d.get('interoperability', sppasAppConfig.DEFAULT_INTEROPERABILITY)
                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['interoperability'] = self.__interoperability
        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