# configTools: Tools for interfacing with Python configuration

Code of the config system; not related to fontTools or fonts in particular.

The options that are specific to fontTools are in `fontTools.config`.

To create your own config system, you need to create an instance of `Options`, and a subclass of `AbstractConfig` with its `options` class variable set to your instance of Options.

class fontTools.misc.configTools.AbstractConfig(values: AbstractConfig | Dict[Option | str, Any] | Mapping[str, Any] = {}, parse_values: bool = False, skip_unknown: bool = False)

  Bases: `MutableMapping`

  Create a set of config values, optionally pre-filled with values from the given dictionary or pre-existing config object.

  The class implements the MutableMapping protocol keyed by option name (str). For convenience its methods accept either Option or str as the key parameter.

  See also

  `set()`

  This config class is abstract because it needs its `options` class var to be set to an instance of `Options` before it can be instanciated and used.

      class MyConfig(AbstractConfig):
          options = Options()

      MyConfig.register_option( "test:option_name", "This is an option", 0, int, lambda v: isinstance(v, int))

      cfg = MyConfig({"test:option_name": 10})

  options: ClassVar[Options]
  classmethod register_option(name: str, help: str, default: Any, parse: Callable[[str], Any], validate: Callable[[Any], bool] | None = None) → Option

    Register an available option in this config system.

  set(option_or_name: Option | str, value: Any, parse_values: bool = False, skip_unknown: bool = False)

    Set the value of an option.

    Parameters:

      - option_or_name (*) – an Option object or its name (str).

      - value (*) – the value to be assigned to given option.

      - parse_values (*) – parse the configuration value from a string into its proper type, as per its Option object. The default behavior is to raise ConfigValueValidationError when the value is not of the right type. Useful when reading options from a file type that doesn’t support as many types as Python.

      - skip_unknown (*) – skip unknown configuration options. The default behaviour is to raise ConfigUnknownOptionError. Useful when reading options from a configuration file that has extra entries (e.g. for a later version of fontTools)

  get(option_or_name: ~fontTools.misc.configTools.Option | str, default: ~typing.Any = <object object>) → Any

    Get the value of an option. The value which is returned is the first provided among:

    1. a user-provided value in the options’s `self._values` dict

    2. a caller-provided default value to this method call

    3. the global default for the option provided in `fontTools.config`

    This is to provide the ability to migrate progressively from config options passed as arguments to fontTools APIs to config options read from the current TTFont, e.g.

        def fontToolsAPI(font, some_option):
            value = font.cfg.get("someLib.module:SOME_OPTION", some_option)
            # use value

    That way, the function will work the same for users of the API that still pass the option to the function call, but will favour the new config mechanism if the given font specifies a value for that option.

  copy()

exception fontTools.misc.configTools.ConfigAlreadyRegisteredError(name)

  Bases: `ConfigError`

  Raised when a module tries to register a configuration option that already exists.

  Should not be raised too much really, only when developing new fontTools modules.

exception fontTools.misc.configTools.ConfigError

  Bases: `Exception`

  Base exception for the config module.

exception fontTools.misc.configTools.ConfigUnknownOptionError(option_or_name)

  Bases: `ConfigError`

  Raised when a configuration option is unknown.

exception fontTools.misc.configTools.ConfigValueParsingError(name, value)

  Bases: `ConfigError`

  Raised when a configuration value cannot be parsed.

exception fontTools.misc.configTools.ConfigValueValidationError(name, value)

  Bases: `ConfigError`

  Raised when a configuration value cannot be validated.

class fontTools.misc.configTools.Option(name: 'str', help: 'str', default: 'Any', parse: 'Callable[[str], Any]', validate: 'Optional[Callable[[Any], bool]]' = None)

  Bases: `object`

  name: str

    MY_OPTION).

    Type:

      Unique name identifying the option (e.g. package.module

  help: str

    Help text for this option.

  default: Any

    Default value for this option.

  parse: Callable[[str], Any]

    Turn input (e.g. string) into proper type. Only when reading from file.

  validate: Callable[[Any], bool] | None = None

    Return true if the given value is an acceptable value.

  static parse_optional_bool(v: str) → bool | None
  static validate_optional_bool(v: Any) → bool

class fontTools.misc.configTools.Options(other: Options = None)

  Bases: `Mapping`

  Registry of available options for a given config system.

  Define new options using the `register()` method.

  Access existing options using the Mapping interface.

  register(name: str, help: str, default: Any, parse: Callable[[str], Any], validate: Callable[[Any], bool] | None = None) → Option

    Create and register a new option.

  register_option(option: Option) → Option

    Register a new option.

  is_registered(option: Option) → bool

    Return True if the same option object is already registered.