lib.config package

Holds, validates and handles faceswap configuration items, ensuring type correctness. Handles interfacing with saved config .ini files

lib.config.config Module

Default configurations for faceswap. Handles parsing and validating of Faceswap Configs and interfacing with configparser.ConfigParser

class lib.config.config.FaceswapConfig(config_file: str | None = None)

Config Items

Parameters:

config_file (str | None)

add_item(section: str, title: str, config_item: ConfigItem) None

Add a default item to a config section

Parameters:
  • section (str) – The section of the config to add the item to

  • title (str) – The name of the config item

  • config_item (ConfigItem) – The default config item object to add to the config

Return type:

None

add_section(title: str, info: str) None

Add a default section to config file

Parameters:
  • title (str) – The title for the section

  • info (str) – The helptext for the section

Return type:

None

save_config() None

Update the ini file with the currently stored app values and save the config file.

Return type:

None

sections: dict[str, ConfigSection]

The Faceswap config sections and options

Type:

dict[str, ConfigSection]

set_defaults(helptext: str = '') None

Override for plugin specific config defaults.

This method should always be overridden to add the help text for the global plugin group. If helptext is not provided, then it is assumed that there is no global section for this plugin group.

The default action will parse the child class’ module for ConfigItem objects and add them to this plugin group’s “global” section of sections.

The name of each config option will be the variable name found in the module.

It will then parse the child class’ module for subclasses of GlobalSection objects and add each of these sections to this plugin group’s sections, adding any ConfigItem within the GlobalSection to that sub-section.

The section name will be the name of the GlobalSection subclass, lowercased

Parameters:

helptext (str) – The help text to display for the plugin group

Raises:

ValueError – If the plugin group’s help text has not been provided

Return type:

None

lib.config.config.generate_configs(force: bool = False) None

Generate config files if they don’t exist.

This script is run prior to anything being set up, so don’t use logging Generates the default config files for plugins in the faceswap config folder

Logic:
  • Scan the plugins path for files named <parent_folder>_config.py>

  • Import the discovered module and look for instances of FaceswapConfig

  • If exists initialize the class

Parameters:

force (bool) – Force the loading of all plugin configs even if their .ini files pre-exist

Return type:

None

lib.config.config.get_configs() dict[str, FaceswapConfig]

Get all of the FaceswapConfig options. Loads any configs that have not been loaded and return a dictionary of all configs.

Returns:

All of the loaded faceswap config objects

Return type:

dict[str, FaceswapConfig]

Functions

generate_configs([force])

Generate config files if they don't exist.

get_configs()

Get all of the FaceswapConfig options.

Classes

FaceswapConfig([config_file])

Config Items


lib.config.ini Module

Handles interfacing between Faceswap Configs and ConfigParser .ini files

class lib.config.ini.ConfigFile(plugin_group: str, ini_path: str | None = None)

Handles the interfacing between saved faceswap .ini configs and internal Config objects

Parameters:
  • plugin_group (str) – The plugin group that is requesting a config file

  • ini_path (str | None) – Optional path to a .ini config file. None for default location. Default: None

format_help(helptext: str, is_section: bool = False) str

Format comments for insertion into a config ini file

Parameters:
  • helptext (str) – The help text to be formatted

  • is_section (bool) – True if the help text pertains to a section. False if it pertains to an option. Default: True

Return type:

The formatted help text

load() None

Load values from the saved config ini file into our Config object

Return type:

None

on_load(app_config: dict[str, ConfigSection]) None

Check whether there has been any change between the current application config and the loaded ini config. If so, update the relevant object(s) appropriately. This check will also create new config.ini files if they do not pre-exist

Parameters:

app_config (dict[str, ConfigSection]) – The latest configuration settings from the application. Section name is key

Return type:

None

save() None

Save a config file

Return type:

None

update_from_app(app_config: dict[str, ConfigSection]) None

Update the config.ini file to those values that are currently in Faceswap’s app config

Parameters:

app_config (dict[str, ConfigSection]) – The latest configuration settings from the application. Section name is key

Return type:

None

Classes

ConfigFile(plugin_group[, ini_path])

Handles the interfacing between saved faceswap .ini configs and internal Config objects


lib.config.objects Module

Dataclass objects for holding and validating Faceswap Config item

class lib.config.objects.ConfigItem(datatype: type[~lib.config.objects.T], default: ~lib.config.objects.T, group: str, info: str, choices: list[str] | ~typing.Literal['colorchooser'] = <factory>, gui_radio: bool = False, min_max: tuple[~lib.config.objects.T, ~lib.config.objects.T] | None = None, rounding: int = -1, fixed: bool = True)

A dataclass for storing config items loaded from config.ini files and dynamically assigning and validating that the correct datatype is used.

The value loaded from the .ini config file can be accessed with either:

>>> conf.value
>>> conf()
>>> conf.get()
Parameters:
  • datatype (type[lib.config.objects.T]) – A python type class. This limits the type of data that can be provided in the .ini file and ensures that the value is returned to faceswap is correct. Valid datatypes are: int, float, str, bool or list. Note that list items must all be strings.

  • default (lib.config.objects.T) – The default value for this option. It must be of the same type as datatype.

  • group (str) – The group that this config item exists within in the config section

  • info (str) – A description of what this option does.

  • choices (list[str] | Literal['colorchooser']) – If this option’s datatype is a str then valid selections can be defined here, empty list for any value. If the option’s datatype is a list, then this option must be populated with the valid selections. This validates the option and also enables a combobox / radio option in the GUI. If the default value is a hex color value, then this should be the literal “colorchooser” to present a color choosing interface in the GUI. Ignored for all other datatypes Default: [] (empty list: no options)

  • gui_radio (bool) – If choices are defined, this indicates that the GUI should use radio buttons rather than a combobox to display this option. Default: False

  • min_max (tuple[lib.config.objects.T, lib.config.objects.T] | None) – For int and float datatype this is required otherwise it is ignored. Should be a tuple of min and max accepted values of the same datatype as the option value. This is used for controlling the GUI slider range. Values are not enforced. Default: None

  • rounding (int) – For int and float :attr:datatypes this is required to be > 0 otherwise it is ignored. Used for the GUI slider. For `float, this is the number of decimal places to display. For int this is the step size. Default: -1 (ignored)

  • fixed (bool) – [train only]. Training configurations are fixed when the model is created, and then reloaded from the state file. Marking an item as fixed=``False`` indicates that this value can be changed for existing models, and will override the value saved in the state file with the updated value in config. Default: True

__call__() T

Obtain the currently stored configuration value

Returns:

  • The config value for this item loaded from the config .ini file. String values will always

  • be lowercase, regardless of what is loaded from Config

Return type:

T

choices: list[str] | Literal['colorchooser'] = <dataclasses._MISSING_TYPE object>

If this option’s datatype is a str then valid selections may be defined here, Empty list if any value is valid. If the datatype is a list then valid choices will be populated here. If the default value is a hex color code, then the literal “colorchooser” will display a color choosing interface in the GUI.

datatype: type[T] = <dataclasses._MISSING_TYPE object>

A python type class. The datatype of the config value. One of int, float, str, bool or list. list will only contain str items

default: T = <dataclasses._MISSING_TYPE object>

The default value for this option. It is of the same type as datatype

fixed: bool = True

Only used for train.model configurations. Options marked as fixed=``False`` indicates that this value can be changed for existing models, otherwise the option set when the model commenced training is fixed and cannot be changed. Default: True

get() T

Obtain the currently stored configuration value

Returns:

  • The config value for this item loaded from the config .ini file. String values will always

  • be lowercase, regardless of what is loaded from Config

Return type:

T

group: str = <dataclasses._MISSING_TYPE object>

The group that this config option belongs to

gui_radio: bool = False

indicates that the GUI should use radio buttons rather than a combobox to display this option if choices is populated

property helptext: str

Description of the config option with additional formatting and helptext added from the item parameters

info: str = <dataclasses._MISSING_TYPE object>

A description of what this option does

property ini_value: str

The current value of the ConfigItem as a string for writing to a .ini file

min_max: tuple[T, T] | None = None

For int and float datatype this will be populated otherwise it will be None. Used for controlling the GUI slider range. Values are not enforced.

property name: str

The name associated with this option

rounding: int = -1

For int and float datatypes this will be > 0 otherwise it will be -1. Used for the GUI slider. For float, this is the number of decimal places to display. For int this is the step size.

set(value: T) None

Set the item’s option value

Parameters:

value (T) – The value to set this item to. Must be of type datatype

Raises:

ValueError – If the given value does not pass type and content validation checks

Return type:

None

set_name(name: str) None

Set the logging name for this object for display purposes

Parameters:

name (str) – The name to assign to this option

Return type:

None

property value: T

The config value for this item loaded from the config .ini file. String values will always be lowercase, regardless of what is loaded from Config

class lib.config.objects.ConfigSection(helptext: str, options: dict[str, ConfigItem])

Dataclass for holding information about configuration sections and the contained configuration items

Parameters:
  • helptext (str) – The helptext to be displayed for the configuration section

  • options (dict[str, lib.config.objects.ConfigItem]) – Dictionary of configuration option name to the options for the section

class lib.config.objects.GlobalSection(helptext: str)

A dataclass for holding and identifying global sub-sections for plugin groups. Any global subsections must inherit from this.

Parameters:

helptext (str) – The helptext to be displayed for the global configuration section

Classes

ConfigItem(datatype, default, group, info, ...)

A dataclass for storing config items loaded from config.ini files and dynamically assigning and validating that the correct datatype is used.

ConfigSection(helptext, options)

Dataclass for holding information about configuration sections and the contained configuration items

GlobalSection(helptext)

A dataclass for holding and identifying global sub-sections for plugin groups.

Variables

T

Type variable.

Class Inheritance Diagram

Inheritance diagram of lib.config.objects.ConfigItem, lib.config.objects.ConfigSection, lib.config.objects.GlobalSection