mlsimkit.common.cli.ResourcePath

class mlsimkit.common.cli.ResourcePath(search_paths=None, *args, **kwargs)

A Click option type that resolves paths from the provided search paths, including paths from importlib.resources.

This class inherits from click.Path and extends its functionality to support resolving paths from package resources or folder paths. If the provided value is not a valid existing regular file path, it attempts to resolve the path using the specified search paths, which are package paths relative to importlib.resources.

If require=true and the file does not exist after all search paths then a BadParameter is raised.

Parameters:

search_paths (Optional[Sequence[str]]) – A sequence of package paths to search for the provided value. If not provided, the class will only attempt to resolve regular file paths.

search_paths

The sequence of package paths to search for the provided value.

Type:

Sequence[str]

convert(value: str, param: click.Parameter | None, ctx: click.Context | None) str | None

Converts the provided value to a valid file path.

If the value is a valid existing regular file path, it returns the resolved path. If the value is not a valid regular file path, it attempts to resolve the path using the specified search paths. If the path is found in one of the search paths and the resolved path exists, it returns the resolved path.

If the value cannot be resolved as a valid existing file path, and the option is required, it raises a click.exceptions.BadParameter exception. If the option is not required, it returns None.

Parameters:
  • value (str) – The value to convert to a file path.

  • param (Optional[click.Parameter]) – The Click parameter object associated with the value.

  • ctx (Optional[click.Context]) – The Click context object associated with the value.

Returns:

The resolved file path if the value is a valid existing file path or can be resolved using the search paths. None if the value cannot be resolved and the option is not required.

Return type:

Union[str, None]

__init__(search_paths=None, *args, **kwargs)

Methods

__init__([search_paths])

coerce_path_result(value)

convert(value, param, ctx)

Convert the value to the correct type.

fail(message[, param, ctx])

Helper method to fail with an invalid value message.

get_metavar(param)

Returns the metavar default for this param if it provides one.

get_missing_message(param, ctx)

Optionally might return extra information about a missing parameter.

shell_complete(ctx, param, incomplete)

Return a special completion marker that tells the completion system to use the shell to provide path completions for only directories or any paths.

split_envvar_value(rv)

Given a value from an environment variable this splits it up into small chunks depending on the defined envvar list splitter.

to_info_dict()

Gather information that could be useful for a tool generating user-facing documentation.

Attributes

arity

envvar_list_splitter

if a list of this type is expected and the value is pulled from a string environment variable, this is what splits it up.

is_composite

name

the descriptive name of this type

__init__(search_paths=None, *args, **kwargs)
convert(value, param, ctx)

Convert the value to the correct type. This is not called if the value is None (the missing value).

This must accept string values from the command line, as well as values that are already the correct type. It may also convert other compatible types.

The param and ctx arguments may be None in certain situations, such as when converting prompt input.

If the value cannot be converted, call fail() with a descriptive message.

Parameters:
  • value – The value to convert.

  • param – The parameter that is using this type to convert its value. May be None.

  • ctx – The current context that arrived at this value. May be None.

get_metavar(param)

Returns the metavar default for this param if it provides one.