stepup.core.path
Specialized path operations.
apply_affixes(path, leading, trailing)
¶
Apply leading ./ and trailing / slashes to a path.
Parameters:
-
path(StrPath) –The path to which the affixes will be applied.
-
leading(str) –The leading
./to apply or"". -
trailing(str) –The trailing slash to apply or
"".
Raises:
-
PathError–If the path already has a leading or trailing slash and the corresponding affix is not empty.
-
PathError–If
leadingis neither""nor"./". -
PathError–If
trailingis neither""nor"/".
Source code in stepup/core/path.py
coerce_path(arg)
¶
coerce_paths(args)
¶
Convert a path-like argument or flat iterable to path.Path instances.
coerce_paths2(args)
¶
Convert an iterable of paths or path sub-iterables, flattening one level of nesting.
Source code in stepup/core/path.py
coerce_str(arg)
¶
dir_range_upper(parent)
¶
Compute the exclusive upper bound for a range of paths that are all under the given parent.
SQL queries can define the range of all paths under parent with the following WHERE clause:
where :parent is the parent path and :upper_bound is the result of this function.
Raises:
-
ValueError–If
parentdoes not end with a trailing slash.
Source code in stepup/core/path.py
format_local_executable(executable)
¶
Format a relative path to a local executable for execution in a shell.
A path without a leading ./ or ../ gets a ./ prefix,
so that a shell runs the file at that path instead of searching PATH for its name.
Raises:
-
PathError–If
executableis an absolute path.
Source code in stepup/core/path.py
get_affixes(path)
¶
Get the leading ./ and trailing / of a path.
Parameters:
-
path(StrPath) –The path from which the affixes will be extracted.
Returns:
-
leading–The leading
./of the path, or""if there is none. -
trailing–The trailing slash of the path, or
""if there is none.
Notes
For the special case of the path "./", the leading is "" and the trailing is "/".
Source code in stepup/core/path.py
get_stepup_root()
¶
Get the StepUp root directory.
Returns:
-
stepup_root–The StepUp root directory, which is either the value of
${STEPUP_ROOT}, or the current working directory if the environment variable is not set. The returned path is absolute and normalized.
Source code in stepup/core/path.py
make_path_out(path_in, dest, ext, other_exts=())
¶
Construct an output path given the input path, a destination and the expected extension.
Parameters:
-
path_in(StrPath) –The input path from which the output path can be derived.
-
dest(StrPath | None) –An output destination. Either
None(only change extension), a destination directory (requires trailing slash) or a file. In all three cases, the output must have extensionext, unlessextisNoneor the extension is one ofother_exts. -
ext(str | None) –The (new) extension of the output, e.g.
.pdf. WhenNone, the extension of the input is preserved. -
other_exts(Iterable[str], default:()) –Other extensions that are allowed for the output.
Returns:
-
path_out–A properly formatted output path.
Raises:
-
PathError–If the output path is equal to the input path, or if the output path does not have the expected extension.
Source code in stepup/core/path.py
parent_dir(path)
¶
The directory containing path, with the project root written as ..
A path ending in a separator names a directory, and its parent is then that same directory without the trailing separator. Strip the trailing separator before calling to get the directory one level up instead.
Parameters:
-
path(StrPath) –A path relative to the project root.
Returns:
-
parent–The parent directory, never empty and without a trailing separator.
Source code in stepup/core/path.py
short_path(path)
¶
Shorten a path for display in a message.
A path inside the working directory is made relative to it, with a ./ prefix.
Any other path is shown as it is:
an absolute path is easier to read than a relative one climbing out of the tree,
and a path like ~/.config/stepup.toml already says where it is.
Source code in stepup/core/path.py
translate(path, workdir='.')
¶
Normalize the path and, if relative, make it relative to STEPUP_ROOT.
Parameters:
-
path(StrPath) –The path to translate. If relative, it is assumed to be relative to
workdir. -
workdir(StrPath, default:'.') –The working directory. If relative, it is assumed to be relative to
HERE.
Returns:
-
translated_path–A path that can be interpreted in the working directory of the StepUp director.
Source code in stepup/core/path.py
translate_back(path, workdir='.')
¶
If relative, make the path relative to workdir, assuming it is relative to STEPUP_ROOT.
Parameters:
-
path(StrPath) –The path to translate. If relative, it is assumed to be relative to
STEPUP_ROOT. -
workdir(StrPath, default:'.') –The working directory. If relative, it is assumed to be relative to
HERE.
Returns:
-
back_translated_path–A path that can be interpreted in the working directory.