User configuration management

The guidata.userconfig module provides user configuration file (.ini file) management features based on ConfigParser (standard Python library).

It is the exact copy of the open-source package userconfig (MIT license).

This module provides the following functions and classes:

guidata.userconfig.get_home_dir() str

Return user home directory

guidata.userconfig.get_config_basedir() str

Return user configuration base directory.

class guidata.userconfig.UserConfig(defaults)

UserConfig class, based on ConfigParser name: name of the config options: dictionary containing options or list of tuples (section_name, options)

Note that “get” and “set” arguments number and type differ from the overriden methods

update_defaults(defaults)

Update the default configuration

Parameters:

defaults – dict section -> dict {option: default value}

save()

Save the configuration.

set_application(name, version, load=True, raw_mode=False)

Set the application name and version

Parameters:
  • name – name of the application

  • version – current version in format “X.Y.Z”

  • load – If True, load the configuration from dict

  • raw_mode – If True, enable raw mode of ConfigParser

check_default_values()

Check the static options for forbidden data types

get_version(version='0.0.0')

Return configuration (not application!) version

set_version(version='0.0.0', save=True)

Set configuration (not application!) version

get_path(basename: str) str

Return filename path inside configuration directory

filename() str

Return configuration file name

cleanup()

Remove .ini file associated to config

set_as_defaults()

Set defaults from the current config

reset_to_defaults(save=True, verbose=False)

Reset config to Default values

get_default(section, option)

Get Default value for a given (section, option)

Useful for type checking in ‘get’ method

get(section, option, default: ~typing.Any = <class 'guidata.userconfig.NoDefault'>, raw=None, **kwargs)

Get an option section=None: attribute a default section name default: default value (if not specified, an exception will be raised if option doesn’t exist)

get_section(section)

Returns configuration values of the given section.

The returned dict includes unset default values.

Parameters:

section – section name

Returns:

dict option name -> value

set_default(section, option, default_value)

Set Default value for a given (section, option) -> called when a new (section, option) is set and no default exists

set(section, option, value, verbose=False, save=True)

Set an option section=None: attribute a default section name

remove_section(section)

Remove the given section and save the configuration.

remove_option(section, option)

Remove the given option from the given section and save the configuration.