configTools
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] = {}, parse_values: bool = False, skip_unknown: bool = False)[source]
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
This config class is abstract because it needs its
options
class var to be set to an instance ofOptions
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})
- classmethod register_option(name: str, help: str, default: Any, parse: Callable[[str], Any], validate: Callable[[Any], bool] | None = None) Option [source]
Register an available option in this config system.
- set(option_or_name: Option | str, value: Any, parse_values: bool = False, skip_unknown: bool = False)[source]
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 [source]
Get the value of an option. The value which is returned is the first provided among:
a user-provided value in the options’s
self._values
dicta caller-provided default value to this method call
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.
- exception fontTools.misc.configTools.ConfigAlreadyRegisteredError(name)[source]
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[source]
Bases:
Exception
Base exception for the config module.
- exception fontTools.misc.configTools.ConfigUnknownOptionError(option_or_name)[source]
Bases:
ConfigError
Raised when a configuration option is unknown.
- exception fontTools.misc.configTools.ConfigValueParsingError(name, value)[source]
Bases:
ConfigError
Raised when a configuration value cannot be parsed.
- exception fontTools.misc.configTools.ConfigValueValidationError(name, value)[source]
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)[source]
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.
- class fontTools.misc.configTools.Options(other: Options | None = None)[source]
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.