craftr.core.settings

Settings Objects

class Settings(, metaclass=abc.ABCMeta)

Interface similar to a mapping but it provides additional helpers to read values of various types.

get_instance

 | get_instance(type: t.Type[T], key: str, default: str) -> T

Locates a class using the string stored under the specified key or via the default string. If the class provides a from_settings() method, that method will be used to create an instance of the class by passing the settings object itself, otherwise the class will be instantiated without arguments.

Note: Due to https://github.com/python/mypy/issues/5374, uses of this function may need to be annotated with # type: ignore.

create_instance

 | create_instance(type: t.Type[T], fqn: str, config_key: str = '<notset>') -> T

Creates an instance of the specified type from a fully qualified name. If the class implements the IHasFromSettings protocol, the method will be called to create an instance.

parse

 | @staticmethod
 | parse(lines: t.Iterable[str], on_invalid_line: t.Optional[t.Callable[[int, str], None]] = None) -> 'Settings'

Parses a list of key=value lines and returns it as a Settings object. Lines starting with the hashsign (#) are skipped. If provided, lines that do not conform to the key=value format are passed to on_invalid_line and are skipped.