Skip to content

Utils

A collection of Helper functions.

contains_path

contains_path(path, sub_path)

Check if a subpath is included in another path.

Parameters:

Name Type Description Default
path PurePath

Path you want to check

required
sub_path PurePath

Subpath that should be included in path.

required

Returns:

Type Description
bool

True if path contains subpath

Source code in b4_backup/utils.py
def contains_path(path: PurePath, sub_path: PurePath) -> bool:
    """
    Check if a subpath is included in another path.

    Args:
        path: Path you want to check
        sub_path: Subpath that should be included in path.

    Returns:
        True if path contains subpath
    """
    return any(
        slice == sub_path.parts
        for slice in zip(*[path.parts[i:] for i in range(len(sub_path.parts))])
    )

load_config

load_config(config_path=DEFAULT_CONFIG, overrides=None)

Reads the config file and returns a config dataclass.

Parameters:

Name Type Description Default
config_path Path

Path of the config file

DEFAULT_CONFIG
overrides list[str] | None

A list of dot list entries, which can override the values in the config. Used for CLI.

None

Returns:

Type Description
BaseConfig

Config object

Source code in b4_backup/utils.py
def load_config(
    config_path: Path = DEFAULT_CONFIG, overrides: list[str] | None = None
) -> BaseConfig:
    """
    Reads the config file and returns a config dataclass.

    Args:
        config_path: Path of the config file
        overrides:
            A list of dot list entries, which can override the values in the config.
            Used for CLI.

    Returns:
        Config object
    """
    overrides = overrides or []

    config_path = config_path.expanduser()
    config_path.parent.mkdir(exist_ok=True, parents=True)
    _ = config_path.exists() or config_path.touch()

    if not OmegaConf.has_resolver("from_file"):
        OmegaConf.register_new_resolver("from_file", resolve_from_file)
        OmegaConf.register_new_resolver("parent_dir", resolve_parent_dir)

    base_conf = OmegaConf.merge(
        OmegaConf.structured(BaseConfig),
        OmegaConf.load(config_path),
        OmegaConf.from_dotlist(overrides),
    )

    # Templates shouldn't fail, if there is a value missing
    base_conf.backup_targets[DEFAULT].source = "NONE"

    # pylance doesn't understand here that it's actually a config_schema.BaseConfig type
    base_conf_instance: BaseConfig = OmegaConf.to_container(  # type: ignore
        base_conf, structured_config_mode=SCMode.INSTANTIATE, resolve=True
    )

    base_conf_instance.config_path = config_path

    # retention rulesets shouldn't do a nested merge
    # That's why I do a shallow update here manually
    _copy_from_default_retention(base_conf_instance)

    return base_conf_instance

resolve_from_file

resolve_from_file(path)

This resolver (from_file) can be used in OmegaConf configs to use the raw content of a file as an input.

Parameters:

Name Type Description Default
path str

Input file path

required

Returns:

Type Description
str

File content

Source code in b4_backup/utils.py
def resolve_from_file(path: str) -> str:
    """
    This resolver (from_file) can be used in OmegaConf configs to use the raw content of a file as an input.

    Args:
        path: Input file path

    Returns:
        File content
    """
    return Path(path).read_text(encoding="utf8").strip()

resolve_parent_dir

resolve_parent_dir(path)

This resolver (parent_dir) can be used in OmegaConf configs to return the parent directory or the directory of a file.

Parameters:

Name Type Description Default
path str

Name of the file or directory

required

Returns:

Type Description
str

Parent directory

Source code in b4_backup/utils.py
def resolve_parent_dir(path: str) -> str:
    """
    This resolver (parent_dir) can be used in OmegaConf configs to return the parent directory or the directory of a file.

    Args:
        path: Name of the file or directory

    Returns:
        Parent directory
    """
    return str(PurePath(path).parent)

rich_handler

rich_handler()

Used in the logging config to use a customized RichHandler.

Source code in b4_backup/utils.py
def rich_handler() -> RichHandler:
    """Used in the logging config to use a customized RichHandler."""
    return RichHandler(console=CONSOLE)