Path patterns
Path pattern utilities
This module provides some handy utilities for parsing and working with the specialized "path
patterns" utilized by yaml-extras
in the construction of !import
tags.
The core abstraction provided by this module is the PathPattern
, which is a custom implementation
of UNIX-like glob search on pathlib.Path
objects, except that it also supports naming wildcard
globs so that their values can be extracted and referenced elsewhere. Some example valid path
patterns include:
# Use of anonymous * wildcard
- data/*.yml
# Use of anonymous ** wildcard
- data/**/*.yml
# Use of named wildcards
- data/{name:*}.yml
- data/{sub_path:**}/info.yml
- data/{name:*}/{sub_path:**}/{base_name:*}.yml
The results retrieved by a PathPattern
are PathWithMetadata
objects, which are a wrapper class
around pathlib.Path
objects that also store optional metadata. This metadata is extracted from the
named wildcards in the pattern.
PathWithMetadata
dataclass
Custom dataclass to store a Path object with optional metadata.
Attributes:
Name | Type | Description |
---|---|---|
path |
Path
|
Path object. |
metadata |
Any
|
Optional metadata. Defaults to None. |
Methods:
Name | Description |
---|---|
__hash__ |
Return the hash of the PathWithMetadata object, which is the hash of the path and string representation of the metadata. |
Source code in yaml_extras/file_utils.py
PathPattern
dataclass
Custom implementation of unix-like glob search on pathlib.Path objects. Returned paths may include metadata as PathWithMetadata dataclasses.
Limitations
- Only supports
*
and**
wildcards. - Only officially supports selecting files, not directories.
Enhancements
- Supports named wildcards with syntax
{name:*}
and{name:**}
.
Attributes:
Name | Type | Description |
---|---|---|
pattern |
str
|
Pattern to match, using the supported |
relative_to |
Path
|
Path to search for files. Defaults to None, which assumes the current working directory. |
Methods:
Name | Description |
---|---|
__hash__ |
Return the hash of the PathPattern object, which is the hash of the string glob pattern. |
names |
Return all named wildcards in the pattern. |
as_regex |
Convert a pattern to a regular expression which should match all paths that match the UNIX glob pattern. |
glob_results |
Return all paths that match the pattern using standard pathlib.Path.glob() method, returning simple Paths without metadata. |
results |
Return all paths that match the pattern, including the metadata parsed from the named wildcards in the pattern. |
Source code in yaml_extras/file_utils.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
|
names
property
Return all named wildcards in the pattern. E.g., for the pattern data/{name:*}.yml
, the
only named wildcard is "name", so this method would return ["name"]
.
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: List of named wildcards. |
as_regex(pattern)
classmethod
Convert a pattern to a regular expression. The regular expression should match all paths that match the UNIX glob pattern, meaning that "" wildcards match any character except slashes, and "*" wildcards match any characters including slashes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern
|
str
|
Glob pattern to convert to a regex expression. |
required |
Returns:
Type | Description |
---|---|
Pattern
|
re.Pattern: Compiled regular expression object. |
Source code in yaml_extras/file_utils.py
glob_results()
cached
Return all paths that match the pattern using standard pathlib.Path.glob() method, returning simple Paths without metadata.
Returns:
Type | Description |
---|---|
list[Path]
|
list[Path]: List of pathlib.Path objects matching the pattern. |
Source code in yaml_extras/file_utils.py
results()
cached
Return all paths that match the pattern, including metadata.
Returns:
Type | Description |
---|---|
list[PathWithMetadata]
|
list[PathWithMetadata]: List of PathWithMetadata objects matching the pattern. |