datumaro.util.os_util#
Functions
|
|
Check if any "/" or ""-separated component of path is (or, once Win32's trailing dot/space trimming is accounted for, resolves to) the parent directory reference "..". |
|
|
|
|
|
|
Validate that an already-constructed path (e.g. one supplied directly by a caller instead of being composed from parts via join_within_base) stays within base_dir. |
|
Extract subset name from the given url. |
|
|
|
Generates the "next" name by appending a next index to the occurrence of the basename with the highest index in the input collection. |
|
|
|
|
|
Tests if a path is subpath of another path or the paths are equal. |
|
Join parts onto base_dir, and reject the result if it escapes base_dir. |
Normalizes string, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens. |
|
|
|
|
|
|
- datumaro.util.os_util.contains_unsafe_path_component(path: str) bool[source]#
Check if any “/” or “"-separated component of path is (or, once Win32’s trailing dot/space trimming is accounted for, resolves to) the parent directory reference “..”.
- datumaro.util.os_util.join_within_base(base_dir: str, *parts: str) str[source]#
Join parts onto base_dir, and reject the result if it escapes base_dir.
Untrusted values (e.g. dataset item ids) can contain ‘..’ or be absolute, so a plain osp.join() can traverse outside of base_dir. Raises ValueError in that case (including when the paths don’t share a common root, e.g. different drives on Windows, or when a part would resolve to ‘..’ only after Win32’s trailing dot/space trimming is applied).
- datumaro.util.os_util.ensure_within_base(path: str, base_dir: str) str[source]#
Validate that an already-constructed path (e.g. one supplied directly by a caller instead of being composed from parts via join_within_base) stays within base_dir. Raises ValueError otherwise.
- datumaro.util.os_util.walk(path, max_depth: int | None = None, min_depth: int | None = None)[source]#
- datumaro.util.os_util.find_files(dirpath: str, exts: str | Iterable[str], recursive: bool = False, max_depth: int | None = None, min_depth: int | None = None) Iterator[str][source]#
- datumaro.util.os_util.is_subpath(path: str, base: str) bool[source]#
Tests if a path is subpath of another path or the paths are equal.
- datumaro.util.os_util.make_file_name(s: str) str[source]#
Normalizes string, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens.
- datumaro.util.os_util.generate_next_name(names: Iterable[str], basename: str, sep: str = '.', suffix: str = '', default: str | None = None) str[source]#
Generates the “next” name by appending a next index to the occurrence of the basename with the highest index in the input collection.
Returns: next string name
Example:
- Inputs:
name_abc
name_base
name_base1
name_base5
Basename: name_base
Output: name_base6
- datumaro.util.os_util.extract_subset_name_from_parent(url: str, start: str) str[source]#
Extract subset name from the given url.
For example, if url = “/a/b/images/train/img.jpg” and start = “/a/b”, it will return “train”. On the other hand, if url = “/a/b/images/img.jpg” and start = “/a/b”, it will return DEFAULT_SUBSET_NAME.