Files Module

Custom file / path input handlers for Parameters

author:

Doug Skrypa

class cli_command_parser.inputs.files.FileInput(*, exists: bool | ~typing.Any = None, expand: bool | ~typing.Any = True, resolve: bool | ~typing.Any = False, type: ~cli_command_parser.inputs.utils.StatMode | str = <StatMode:ANY>, readable: bool | ~typing.Any = False, writable: bool | ~typing.Any = False, allow_dash: bool | ~typing.Any = False, use_windows_fix: bool | ~typing.Any = True, fix_default: bool | ~typing.Any = True)[source]

Bases: InputType[T], ABC

exists: bool
expand: bool
resolve: bool
type: StatMode
readable: bool
writable: bool
allow_dash: bool
use_windows_fix: bool
fix_default(value: T | None) T | None[source]

Fixes the default value to conform to the expected return type for this input. Allows the default value for a path to be provided as a string, for example.

validated_path(path: str | Path) Path[source]
class cli_command_parser.inputs.files.Path(*, exists: bool | ~typing.Any = None, expand: bool | ~typing.Any = True, resolve: bool | ~typing.Any = False, type: ~cli_command_parser.inputs.utils.StatMode | str = <StatMode:ANY>, readable: bool | ~typing.Any = False, writable: bool | ~typing.Any = False, allow_dash: bool | ~typing.Any = False, use_windows_fix: bool | ~typing.Any = True, fix_default: bool | ~typing.Any = True)[source]

Bases: FileInput[Path]

Parameters:
  • exists – If set, then the provided path must already exist if True, or must not already exist if False. Default: existence is not checked.

  • expand – Whether tilde (~) should be expanded.

  • resolve – Whether the path should be fully resolved to its absolute path, with symlinks resolved, or not.

  • type – To restrict the acceptable types of files/directories that are accepted, specify the StatMode that matches the desired type. By default, any type is accepted. To accept specifically only regular files or directories, for example, use type=StatMode.DIR | StatMode.FILE.

  • readable – If True, the path must be readable.

  • writable – If True, the path must be writable.

  • allow_dash – Allow a dash (-) to be provided to indicate stdin/stdout (default: False).

  • use_windows_fix – If True (the default) and the program is running on Windows, then fix_windows_path() will be called to fix issues caused by auto-completion via Git Bash.

  • fix_default – Whether default values should be normalized using fix_default().

__call__(value: str | Path) Path[source]
class cli_command_parser.inputs.files.File(mode: str = 'r', *, encoding: str = None, errors: str = None, lazy: bool | Any = True, parents: bool | Any = False, **kwargs)[source]

Bases: FileInput[FileWrapper | str | bytes]

Parameters:
  • mode – The mode in which the file should be opened. For more info, see open().

  • encoding – The encoding to use when reading the file in text mode. Ignored if the parsed path is -.

  • errors – Error handling when reading the file in text mode. Ignored if the parsed path is -.

  • lazy – If True, a FileWrapper will be stored in the Parameter using this File, otherwise the file will be read immediately upon parsing of the path argument.

  • parents – If True and mode implies writing, then create parent directories as needed. Ignored otherwise.

  • kwargs – Additional keyword arguments to pass to Path.

type: StatMode
mode: str
encoding: str
errors: str
lazy: bool
parents: bool
__call__(value: str | Path) FileWrapper | str | bytes[source]
class cli_command_parser.inputs.files.Serialized(converter: Callable[[str | bytes | TextIO | BinaryIO], Any] | Callable[[...], str | bytes | None], *, pass_file: bool | Any = False, **kwargs)[source]

Bases: File

Parameters:
  • converter – Function to use to (de)serialize the given file, such as json.loads(), json.dumps(), pickle.load(), etc.

  • pass_file – For reading, if True, call the converter with the file object, otherwise read the file first and call the converter with the result. For writing, if True, call the converter with both the data to be written and the file object, otherwise call the converter with only the data and then write the result to the file.

  • kwargs – Additional keyword arguments to pass to File

converter: Callable[[str | bytes | TextIO | BinaryIO], Any] | Callable[[...], str | bytes | None]
pass_file: bool
class cli_command_parser.inputs.files.Json(*, mode: str = 'rb', **kwargs)[source]

Bases: Serialized

Parameters:

kwargs – Additional keyword arguments to pass to File

class cli_command_parser.inputs.files.Pickle(*, mode: str = 'rb', **kwargs)[source]

Bases: Serialized

Parameters:

kwargs – Additional keyword arguments to pass to File