WhakerKit 2.0

https://sourceforge.net/projects/whakerkit/

Module whakerkit.config

Class WhakerKitSettings

Description

Manage configuration settings for WhakerKit.

This class handles loading settings from a configuration file and provides access to key attributes such as secret keys and additional configuration options.

This class also defines default naming conventions and rules for folders and files, including separators, minimum name lengths, and invalid characters. Once initialized, the settings become immutable, enforcing consistency throughout the application. The class also supports context management, allowing temporary changes using the 'with' statement. These attributes are:

  • FOLDERNAMESEPARATOR (str): Character used to separate parts of a folder name.
  • FIELDSNAMESEPARATOR (str): Character used to separate fields within a folder name.
  • MINFILENAME_LENGTH (int): Minimum length required for a valid file name.
  • INVALIDCHARSFOR_FOLDERS (str): String of characters that are disallowed in folder names.
  • INVALIDCHARSFOR_FIELDS (str): String of characters that are disallowed in file names.
  • DOWNLOADS_FILENAME (str): Default name for the downloads file.
  • DESCRIPTION_FILENAME (str): Default name for the description file.
Example
 >>> with WhakerKitSettings() as settings:
 >>> print(settings.name)
 > "WhakerKit"
 >>> settings.foo = 'bar'   # raise AttributeError
 >>> del settings.name      # raise AttributeError

Constructor

Initialize the global application settings.

Load the settings from the given configuration file. If the file or directory does not exist, necessary directories will be created. Attributes are frozen after initialization to prevent modification.

Sets default values for folder and file name separators, as well as restrictions on file name lengths and characters. After initialization, the settings are frozen to prevent modifications unless explicitly unfrozen.

Parameters
  • config_filename: (str) Path to the configuration file.
  • root_path: (str | os.PathLike): Project root. Converted to an absolute, normalised path.
Raises
  • TypeError: if root_path is not a valid path or os.PathLike.
View Source
def __init__(self, config_filename: str | None=None, root_path: str | None=None):
    """Initialize the global application settings.

    Load the settings from the given configuration file. If the file or
    directory does not exist, necessary directories will be created.
    Attributes are frozen after initialization to prevent modification.

    Sets default values for folder and file name separators, as well as
    restrictions on file name lengths and characters. After initialization,
    the settings are frozen to prevent modifications unless explicitly
    unfrozen.

    :param config_filename: (str) Path to the configuration file.
    :param root_path: (str | os.PathLike): Project root.
        Converted to an absolute, normalised path.
    :raises: TypeError: if root_path is not a valid path or os.PathLike.

    """
    self._is_frozen = False
    self.load(config_filename, root_path)
    if self.__pathman is None:
        uploads_dir = self.uploads
    else:
        uploads_dir = self.__pathman.get_root_path() + '/' + self.uploads
    logging.debug(f'Directory with documents: {uploads_dir}')
    if os.path.exists(uploads_dir) is False:
        logging.error(f"The directory with documents can't be accessed: {uploads_dir}")
    self._is_frozen = True

Public functions

load

Load the dictionary of settings from a file or set default values.

The configuration file must contain a JSON dictionary with the 'WhakerKit' key holding the application's settings. If the file is missing, log an error and use default values for each required attribute.

Parameters
  • filename: (str) Path to the configuration file.
  • root_path: (str | os.PathLike): Project root. Converted to an absolute, normalised path.
Raises
  • TypeError: if root_path is not a valid path or os.PathLike.
View Source
def load(self, filename: str | None=None, root_path: str | None=None) -> None:
    """Load the dictionary of settings from a file or set default values.

        The configuration file must contain a JSON dictionary with the
        'WhakerKit' key holding the application's settings. If the file is
        missing, log an error and use default values for each required
        attribute.

        :param filename: (str) Path to the configuration file.
        :param root_path: (str | os.PathLike): Project root.
            Converted to an absolute, normalised path.
        :raises: TypeError: if root_path is not a valid path or os.PathLike.

        """
    config = dict()
    if filename is not None:
        if os.path.exists(filename) is False:
            logging.warning('No such file or directory: {:s}'.format(filename))
        else:
            with open(filename, encoding='utf-8') as cfg:
                full_config = json.load(cfg)
                if 'WhakerKit' in full_config:
                    config = full_config['WhakerKit']
    self.__dict__ = dict(base_dir=os.path.dirname(WHAKERKIT), path=WHAKERKIT, whakerexa=config.get('whakerexa', './whakerexa/wexa_statics/'), uploads=config.get('uploads', 'uploads'), name=config.get('name', 'WhakerKitApp'), secret_key=config.get('secret_key', ''), jwt_validity=config.get('jwt_validity', '30'), domain=config.get('domain', None), lang=config.get('lang', 'en'), FOLDER_NAME_SEPARATOR='.', FIELDS_NAME_SEPARATOR='_', MIN_FILE_NAME_LENGTH=4, INVALID_CHARS_FOR_FOLDERS='/\\.$@#%&*()[]{}<>:;,?"\'`!^+=|~', INVALID_CHARS_FOR_FIELDS='/$@#%&*()[]{}<>:;,?"\'`!^+=|~', DOWNLOADS_FILENAME='downloads.txt', DESCRIPTION_FILENAME='description.txt')
    self.__pathman = None
    if root_path is not None:
        self.__pathman = PathManager(root_path)
        self.__dict__['path'] = self.__pathman.compute_relative_path(WHAKERKIT) + '/'
    if 'addons' in config:
        for key in config['addons']:
            if key not in self:
                self.__dict__[key] = config['addons'][key]

get_root_path

Return the absolute path of the hosting.

If the abssolute path was not defined, it returns the absolute path of the WhakerKit root package.

View Source
def get_root_path(self) -> str:
    """Return the absolute path of the hosting.

        If the abssolute path was not defined, it returns the absolute path
        of the WhakerKit root package.

        """
    if self.__pathman is None:
        return WHAKERKIT
    return self.__pathman.get_root_path()

set_lang

Set the language of the application.

Currently supported languages are 'fr' for French and 'en' for English.

Parameters
  • language: (str) The language of the application.
Raises
  • ValueError: Invalid given language.
View Source
def set_lang(self, language: str):
    """Set the language of the application.

        Currently supported languages are 'fr' for French and 'en' for English.

        :param language: (str) The language of the application.
        :raises: ValueError: Invalid given language.

        """
    language = str(language).lower()
    if language not in ('fr', 'en'):
        raise ValueError(f'Invalid language: {language}')
    self.unfreeze()
    self.__dict__['lang'] = str(language)
    self.freeze()

freeze

Freeze the settings to make them immutable.

Once frozen, any attempt to modify or delete attributes will raise an AttributeError.

View Source
def freeze(self):
    """Freeze the settings to make them immutable.

        Once frozen, any attempt to modify or delete attributes will raise
        an AttributeError.

        """
    super().__setattr__('_is_frozen', True)

unfreeze

Unfreeze the settings, allowing temporary modifications.

This allows attributes to be modified, but should be used with caution.

View Source
def unfreeze(self):
    """Unfreeze the settings, allowing temporary modifications.

         This allows attributes to be modified, but should be used with caution.

         """
    super().__setattr__('_is_frozen', False)

Overloads

__setattr__

Override to prevent setting attributes when frozen.

If the settings are frozen, any attempt to set attributes will raise an AttributeError.

Parameters
  • key: (str) The attribute name.
  • value: The value to be assigned to the attribute.
Raises
  • AttributeError: if the settings are frozen.
View Source
def __setattr__(self, key: str, value):
    """Override to prevent setting attributes when frozen.

        If the settings are frozen, any attempt to set attributes will raise
        an AttributeError.

        :param key: (str) The attribute name.
        :param value: The value to be assigned to the attribute.
        :raises: AttributeError: if the settings are frozen.

        """
    if getattr(self, '_is_frozen', False):
        raise AttributeError(f'{self.__class__.__name__} object is immutable')
    super().__setattr__(key, value)

__delattr__

Override to prevent deletion of attributes.

All attempts to delete attributes will raise an AttributeError, whether the settings are frozen or not.

Parameters
  • key: (str) The attribute name.
Raises
  • AttributeError: when attempting to delete an attribute.
View Source
def __delattr__(self, key):
    """Override to prevent deletion of attributes.

        All attempts to delete attributes will raise an AttributeError, whether
        the settings are frozen or not.

        :param key: (str) The attribute name.
        :raises: AttributeError: when attempting to delete an attribute.

        """
    raise AttributeError(f'{self.__class__.__name__} object does not allow attribute deletion')

__enter__

Support 'with' context management.

Allows the use of the with statement for managing configuration settings within a context block.

Returns
  • (WhakerKitSettings) The current settings instance.
View Source
def __enter__(self):
    """Support 'with' context management.

        Allows the use of the `with` statement for managing configuration
        settings within a context block.

        :return: (WhakerKitSettings) The current settings instance.

        """
    return self

__exit__

Exit the context block.

This method is required for context management but does not perform any special operations upon exiting.

Parameters
  • exc_type
  • exc_value
  • traceback
View Source
def __exit__(self, exc_type, exc_value, traceback):
    """Exit the context block.

        This method is required for context management but does not perform any
        special operations upon exiting.

        """
    pass

__iter__

Iterate over the class attributes.

Returns
  • (iterator) An iterator over the attribute names in the settings.
View Source
def __iter__(self):
    """Iterate over the class attributes.

        :return: (iterator) An iterator over the attribute names in the settings.

        """
    for item in self.__dict__.keys():
        yield item