scaffold_kit.utils.pattern_processor
Converts glob-like patterns to regular expressions.
This module provides classes for processing .gitignore-style glob patterns and converting them into equivalent regular expressions. It uses a handler-based, “strategy” pattern to process different types of characters (e.g., wildcards, character classes, literals) and handles complex rules like recursive wildcards and root-anchored patterns.
Demo
To run the module’s demonstration code, use the following command:
$ uv run python -m scaffold_kit.utils.pattern_processor
CharacterClassHandler
#
Bases: CharacterHandler
Handles ‘[…]’ character classes.
Captures the entire character class including its content and closing bracket.
Source code in src/scaffold_kit/utils/pattern_processor.py
can_handle(char)
#
Checks if the character is a ‘[‘.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
char
|
str
|
The single character to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the character is a character class, False otherwise. |
Source code in src/scaffold_kit/utils/pattern_processor.py
handle(text, position)
#
Extracts the entire character class from the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The full text being processed. |
required |
position
|
int
|
Current position in the text. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[str, int]
|
A tuple containing: - The regex string for the character class. - The new position in the text after processing. |
Source code in src/scaffold_kit/utils/pattern_processor.py
CharacterHandler
#
Bases: ABC
Abstract base class for character handlers.
Character handlers define the logic for converting a specific type of pattern character into its regex equivalent.
Source code in src/scaffold_kit/utils/pattern_processor.py
can_handle(char)
abstractmethod
#
Checks if this handler can process the given character.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
char
|
str
|
The single character to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the handler can process the character, False otherwise. |
Source code in src/scaffold_kit/utils/pattern_processor.py
handle(text, position)
abstractmethod
#
Handles the character at the given position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The full text being processed. |
required |
position
|
int
|
Current position in the text. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[str, int]
|
A tuple containing: - The replacement string for the character(s). - The new position in the text after processing. |
Source code in src/scaffold_kit/utils/pattern_processor.py
GlobProcessor
#
Processes glob patterns using the strategy pattern.
This class iterates through a glob string, applying the appropriate CharacterHandler to each character to build a regex string part.
Source code in src/scaffold_kit/utils/pattern_processor.py
__init__()
#
Initializes the GlobProcessor with a list of handlers.
Note that the order of the handlers is crucial. More specific handlers (e.g., wildcards, character classes) must come before the generic fallback handler (LiteralCharHandler).
Source code in src/scaffold_kit/utils/pattern_processor.py
convert_glob_part(part)
#
Converts a single glob part to regex using character handlers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
part
|
str
|
A single string part of a glob pattern (e.g., ‘path’, ‘’, ‘*’). |
required |
Returns:
| Type | Description |
|---|---|
str
|
The regex equivalent of the glob part. |
Source code in src/scaffold_kit/utils/pattern_processor.py
LiteralCharHandler
#
Bases: CharacterHandler
Handles literal characters (default handler).
Converts a literal character to a regex-escaped string.
Source code in src/scaffold_kit/utils/pattern_processor.py
can_handle(char)
#
Checks if this is the fallback handler.
This is the fallback handler, so it can handle any character.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
char
|
str
|
The single character to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True. |
Source code in src/scaffold_kit/utils/pattern_processor.py
handle(text, position)
#
Escapes a single literal character for regex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The full text being processed. |
required |
position
|
int
|
Current position in the text. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[str, int]
|
A tuple of the escaped character and the new position. |
Source code in src/scaffold_kit/utils/pattern_processor.py
PatternProcessor
#
Main class for converting glob patterns to regex.
This class orchestrates the entire conversion process, handling normalization, splitting, and joining of the regex parts.
Source code in src/scaffold_kit/utils/pattern_processor.py
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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
__init__()
#
pattern_to_regex(pattern)
#
Converts a .gitignore-style glob pattern to a regex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
The glob pattern string to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The complete, anchored regular expression string. |
Source code in src/scaffold_kit/utils/pattern_processor.py
SingleCharHandler
#
Bases: CharacterHandler
Handles ‘?’ single character wildcards.
Converts a single ‘?’ glob character into its regex equivalent.
Source code in src/scaffold_kit/utils/pattern_processor.py
can_handle(char)
#
Checks if the character is a ‘?’.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
char
|
str
|
The single character to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the character is a single-char wildcard, False otherwise. |
Source code in src/scaffold_kit/utils/pattern_processor.py
handle(text, position)
#
Converts ‘?’ to ‘[^/]’.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The full text being processed. |
required |
position
|
int
|
Current position in the text. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[str, int]
|
A tuple of the replacement regex and the new position. |
Source code in src/scaffold_kit/utils/pattern_processor.py
WildcardHandler
#
Bases: CharacterHandler
Handles ‘*’ wildcard characters.
Converts a single ‘*’ glob character into its regex equivalent.
Source code in src/scaffold_kit/utils/pattern_processor.py
can_handle(char)
#
Checks if the character is a ‘*’.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
char
|
str
|
The single character to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the character is a wildcard, False otherwise. |
handle(text, position)
#
Converts ‘’ to ‘[^/]’.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The full text being processed. |
required |
position
|
int
|
Current position in the text. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[str, int]
|
A tuple of the replacement regex and the new position. |