scaffold_kit.utils.ignore_parser
Parses and applies .gitignore-style ignore rules to filesystem paths.
This module provides a robust, two-class system for handling file exclusion
patterns. The IgnoreRule class interprets a single pattern line, converting it
to a regular expression. The IgnoreParser class then reads and manages a
collection of these rules, using them to filter lists of files or to check
individual paths.
Demo
To run the module’s demonstration code, use the following command:
$ uv run python -m scaffold_kit.utils.ignore_parser
IgnoreParser
#
Parses and applies ignore rules to filesystem paths.
This class provides methods to load rules from files or strings, and to apply those rules to filter lists of paths or check individual paths for ignored status.
Source code in src/scaffold_kit/utils/ignore_parser.py
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | |
__init__(base_path=None)
#
Initializes the IgnoreParser.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_path
|
str | Path | None
|
The root path to which relative patterns are anchored. If not provided, the parser will handle paths as they are. |
None
|
Source code in src/scaffold_kit/utils/ignore_parser.py
add_lines(lines)
#
Parses and adds ignore rules from an iterable of lines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines
|
Iterator[str]
|
An iterable of strings, such as from an open file or
|
required |
Source code in src/scaffold_kit/utils/ignore_parser.py
add_rule(pattern)
#
Adds a single ignore rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
The pattern string to add. |
required |
explain(path, is_dir=False)
#
Returns a list of all rules that apply to a path.
This method is useful for debugging as it shows every rule that matches the given path and its resulting decision.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The path string or |
required |
is_dir
|
bool
|
Optional flag to indicate if the path is a directory. |
False
|
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of strings, each explaining a matching rule and its |
list[str]
|
outcome. |
Source code in src/scaffold_kit/utils/ignore_parser.py
filter(paths)
#
Returns only the paths that are not ignored by the rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
list[str | Path]
|
A list of path strings or |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
A new list containing only the paths that are not ignored. |
Source code in src/scaffold_kit/utils/ignore_parser.py
from_file(file_path, base_path=None)
classmethod
#
Loads ignore rules from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
The path to the ignore file (e.g., ‘.gitignore’). |
required |
base_path
|
str | Path | None
|
The base path for relative rules. Defaults to the
directory of the |
None
|
Returns:
| Type | Description |
|---|---|
IgnoreParser
|
A new |
Source code in src/scaffold_kit/utils/ignore_parser.py
from_string(rules, base_path=None)
classmethod
#
Loads ignore rules from a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rules
|
str
|
A string containing newline-separated ignore patterns. |
required |
base_path
|
str | Path | None
|
The base path for relative rules. |
None
|
Returns:
| Type | Description |
|---|---|
IgnoreParser
|
A new |
Source code in src/scaffold_kit/utils/ignore_parser.py
matches(path, is_dir=False)
#
Checks if a path is ignored by the rules.
The method returns the result of the last matching rule, where a negated rule overrides a regular one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The path string or |
required |
is_dir
|
bool
|
Optional flag to indicate if the path is a directory. |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the path is ignored, False otherwise. |
Source code in src/scaffold_kit/utils/ignore_parser.py
IgnoreRule
#
Represents a single ignore pattern and its regex equivalent.
This class handles the conversion of a .gitignore-style pattern string into a compiled regular expression, managing pattern nuances like negation and directory-only rules.
Source code in src/scaffold_kit/utils/ignore_parser.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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 | |
__init__(pattern, regex, negated=False, dir_only=False)
#
Initializes an IgnoreRule instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
The original, raw pattern string. |
required |
regex
|
Pattern[str]
|
The compiled regex pattern. |
required |
negated
|
bool
|
True if the rule is a negation (starts with ‘!’). |
False
|
dir_only
|
bool
|
True if the rule is for directories only (ends with ‘/’). |
False
|
Source code in src/scaffold_kit/utils/ignore_parser.py
from_pattern(pattern)
classmethod
#
Creates an IgnoreRule from a raw ignore pattern string.
This factory method parses the input string to determine its properties (negation, directory-only) before converting it to a regex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
The raw ignore pattern (e.g., ‘logs/’, ‘!.gitkeep’). |
required |
Returns:
| Type | Description |
|---|---|
'IgnoreRule'
|
A new |
Source code in src/scaffold_kit/utils/ignore_parser.py
matches(path, is_dir=False)
#
Checks if the given path matches this ignore rule.
This method uses the rule’s compiled regex to check for a match and applies additional logic for directory-only rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The path string to check. |
required |
is_dir
|
bool
|
True if the path represents a directory. |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the path matches the rule, False otherwise. |