API reference¶
Complete reference for all public classes and functions in the searchpath library. This reference is auto-generated from source docstrings using mkdocstrings.
Contents¶
- Module-level functions - One-liner convenience functions
- SearchPath class - Core class for ordered directory searching
- Match class - Result object with provenance information
- Entry type - Type alias for directory entries
- Pattern matchers - Pluggable pattern matching implementations
- Exceptions - Error hierarchy
Module-level functions¶
Convenience functions for one-shot searches without creating a SearchPath instance.
Functions:
| Name | Description |
|---|---|
first |
Find the first matching path across directories. |
match |
Find the first matching path with provenance information. |
all |
Find all matching paths across directories. |
matches |
Find all matching paths with provenance information. |
searchpath.first ¶
first(
pattern: str = "**",
*entries: Entry,
kind: Literal["files", "dirs", "both"] = "files",
include: str | Sequence[str] | None = None,
include_from: Path | str | Sequence[Path | str] | None = None,
include_from_ancestors: str | None = None,
exclude: str | Sequence[str] | None = None,
exclude_from: Path | str | Sequence[Path | str] | None = None,
exclude_from_ancestors: str | None = None,
matcher: PathMatcher | None = None,
follow_symlinks: bool = True
) -> Path | None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Glob pattern for matching paths. Defaults to "**" (all). |
'**'
|
*entries
|
Entry
|
Directory entries to search, in priority order. |
()
|
kind
|
Literal['files', 'dirs', 'both']
|
What to match: "files", "dirs", or "both". |
'files'
|
include
|
str | Sequence[str] | None
|
Additional patterns paths must match. |
None
|
include_from
|
Path | str | Sequence[Path | str] | None
|
Path(s) to files containing include patterns. |
None
|
include_from_ancestors
|
str | None
|
Filename to load include patterns from ancestors. |
None
|
exclude
|
str | Sequence[str] | None
|
Patterns that reject paths. |
None
|
exclude_from
|
Path | str | Sequence[Path | str] | None
|
Path(s) to files containing exclude patterns. |
None
|
exclude_from_ancestors
|
str | None
|
Filename to load exclude patterns from ancestors. |
None
|
matcher
|
PathMatcher | None
|
PathMatcher implementation. Defaults to GlobMatcher. |
None
|
follow_symlinks
|
bool
|
Whether to follow symbolic links. |
True
|
Returns:
| Type | Description |
|---|---|
Path | None
|
The first matching Path, or None if not found. |
Source code in src/searchpath/_functions.py
searchpath.match ¶
match(
pattern: str = "**",
*entries: Entry,
kind: Literal["files", "dirs", "both"] = "files",
include: str | Sequence[str] | None = None,
include_from: Path | str | Sequence[Path | str] | None = None,
include_from_ancestors: str | None = None,
exclude: str | Sequence[str] | None = None,
exclude_from: Path | str | Sequence[Path | str] | None = None,
exclude_from_ancestors: str | None = None,
matcher: PathMatcher | None = None,
follow_symlinks: bool = True
) -> Match | None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Glob pattern for matching paths. Defaults to "**" (all). |
'**'
|
*entries
|
Entry
|
Directory entries to search, in priority order. |
()
|
kind
|
Literal['files', 'dirs', 'both']
|
What to match: "files", "dirs", or "both". |
'files'
|
include
|
str | Sequence[str] | None
|
Additional patterns paths must match. |
None
|
include_from
|
Path | str | Sequence[Path | str] | None
|
Path(s) to files containing include patterns. |
None
|
include_from_ancestors
|
str | None
|
Filename to load include patterns from ancestors. |
None
|
exclude
|
str | Sequence[str] | None
|
Patterns that reject paths. |
None
|
exclude_from
|
Path | str | Sequence[Path | str] | None
|
Path(s) to files containing exclude patterns. |
None
|
exclude_from_ancestors
|
str | None
|
Filename to load exclude patterns from ancestors. |
None
|
matcher
|
PathMatcher | None
|
PathMatcher implementation. Defaults to GlobMatcher. |
None
|
follow_symlinks
|
bool
|
Whether to follow symbolic links. |
True
|
Returns:
| Type | Description |
|---|---|
Match | None
|
The first Match object, or None if not found. |
Source code in src/searchpath/_functions.py
searchpath.all ¶
all(
pattern: str = "**",
*entries: Entry,
kind: Literal["files", "dirs", "both"] = "files",
dedupe: bool = True,
include: str | Sequence[str] | None = None,
include_from: Path | str | Sequence[Path | str] | None = None,
include_from_ancestors: str | None = None,
exclude: str | Sequence[str] | None = None,
exclude_from: Path | str | Sequence[Path | str] | None = None,
exclude_from_ancestors: str | None = None,
matcher: PathMatcher | None = None,
follow_symlinks: bool = True
) -> list[Path]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Glob pattern for matching paths. Defaults to "**" (all). |
'**'
|
*entries
|
Entry
|
Directory entries to search, in priority order. |
()
|
kind
|
Literal['files', 'dirs', 'both']
|
What to match: "files", "dirs", or "both". |
'files'
|
dedupe
|
bool
|
If True, keep only first occurrence per relative path. |
True
|
include
|
str | Sequence[str] | None
|
Additional patterns paths must match. |
None
|
include_from
|
Path | str | Sequence[Path | str] | None
|
Path(s) to files containing include patterns. |
None
|
include_from_ancestors
|
str | None
|
Filename to load include patterns from ancestors. |
None
|
exclude
|
str | Sequence[str] | None
|
Patterns that reject paths. |
None
|
exclude_from
|
Path | str | Sequence[Path | str] | None
|
Path(s) to files containing exclude patterns. |
None
|
exclude_from_ancestors
|
str | None
|
Filename to load exclude patterns from ancestors. |
None
|
matcher
|
PathMatcher | None
|
PathMatcher implementation. Defaults to GlobMatcher. |
None
|
follow_symlinks
|
bool
|
Whether to follow symbolic links. |
True
|
Returns:
| Type | Description |
|---|---|
list[Path]
|
List of matching Path objects. |
Source code in src/searchpath/_functions.py
searchpath.matches ¶
matches(
pattern: str = "**",
*entries: Entry,
kind: Literal["files", "dirs", "both"] = "files",
dedupe: bool = True,
include: str | Sequence[str] | None = None,
include_from: Path | str | Sequence[Path | str] | None = None,
include_from_ancestors: str | None = None,
exclude: str | Sequence[str] | None = None,
exclude_from: Path | str | Sequence[Path | str] | None = None,
exclude_from_ancestors: str | None = None,
matcher: PathMatcher | None = None,
follow_symlinks: bool = True
) -> list[Match]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Glob pattern for matching paths. Defaults to "**" (all). |
'**'
|
*entries
|
Entry
|
Directory entries to search, in priority order. |
()
|
kind
|
Literal['files', 'dirs', 'both']
|
What to match: "files", "dirs", or "both". |
'files'
|
dedupe
|
bool
|
If True, keep only first occurrence per relative path. |
True
|
include
|
str | Sequence[str] | None
|
Additional patterns paths must match. |
None
|
include_from
|
Path | str | Sequence[Path | str] | None
|
Path(s) to files containing include patterns. |
None
|
include_from_ancestors
|
str | None
|
Filename to load include patterns from ancestors. |
None
|
exclude
|
str | Sequence[str] | None
|
Patterns that reject paths. |
None
|
exclude_from
|
Path | str | Sequence[Path | str] | None
|
Path(s) to files containing exclude patterns. |
None
|
exclude_from_ancestors
|
str | None
|
Filename to load exclude patterns from ancestors. |
None
|
matcher
|
PathMatcher | None
|
PathMatcher implementation. Defaults to GlobMatcher. |
None
|
follow_symlinks
|
bool
|
Whether to follow symbolic links. |
True
|
Returns:
| Type | Description |
|---|---|
list[Match]
|
List of Match objects. |
Source code in src/searchpath/_functions.py
SearchPath class¶
The core class representing an ordered list of directories to search.
searchpath.SearchPath ¶
An ordered list of directories to search.
SearchPath represents an ordered sequence of directories that can be searched for files. Each directory is associated with a scope name that identifies where matches came from (e.g., "user", "project").
Entries can be specified as: - A tuple of (scope_name, path): Explicit scope naming - A bare Path or str: Auto-named as "dir0", "dir1", etc. - None: Silently ignored (useful for optional directories)
Attributes:
| Name | Type | Description |
|---|---|---|
dirs |
list[Path]
|
List of directories in the search path. |
scopes |
list[str]
|
List of scope names corresponding to each directory. |
Example
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize a SearchPath with the given entries. |
__add__ |
Concatenate two search paths. |
__bool__ |
Return True if the search path has any directories. |
__iter__ |
Iterate over directories in the search path. |
__len__ |
Return the number of directories in the search path. |
__repr__ |
Return a detailed string representation. |
__str__ |
Return a human-readable string representation. |
with_suffix |
Create a new SearchPath with path components appended. |
filter |
Create a new SearchPath with entries filtered by a predicate. |
existing |
Create a new SearchPath with only existing directories. |
items |
Iterate over (scope, path) pairs in the search path. |
first |
Find the first matching path in the search path. |
match |
Find the first matching path with provenance information. |
all |
Find all matching paths in the search path. |
matches |
Find all matching paths with provenance information. |
Source code in src/searchpath/_searchpath.py
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 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 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 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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 | |
searchpath.SearchPath.dirs
property
¶
searchpath.SearchPath.scopes
property
¶
searchpath.SearchPath.__init__ ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*entries
|
tuple[str, Path | str | None] | Path | str | None
|
Directory entries to include in the search path. Each entry can be: - A tuple of (scope_name, path): Explicit scope naming - A bare Path or str: Auto-named as "dir0", "dir1", etc. - None: Silently ignored |
()
|
Source code in src/searchpath/_searchpath.py
searchpath.SearchPath.__add__ ¶
Concatenate two search paths.
Creates a new SearchPath containing all entries from this search path followed by all entries from the other search path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
Another SearchPath to concatenate. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
A new SearchPath with entries from both. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If other is not a SearchPath. |
Example
Source code in src/searchpath/_searchpath.py
searchpath.SearchPath.__iter__ ¶
Iterate over directories in the search path.
Yields:
| Type | Description |
|---|---|
Path
|
Each directory Path in order. |
searchpath.SearchPath.__repr__ ¶
__repr__() -> str
Return a detailed string representation.
Returns:
| Type | Description |
|---|---|
str
|
A string showing the SearchPath constructor call. |
Source code in src/searchpath/_searchpath.py
searchpath.SearchPath.__str__ ¶
__str__() -> str
Return a human-readable string representation.
Format suitable for error messages showing scope: path pairs.
Returns:
| Type | Description |
|---|---|
str
|
A string like "project: /project/.config, user: ~/.config" |
Source code in src/searchpath/_searchpath.py
searchpath.SearchPath.with_suffix ¶
Create a new SearchPath with path components appended.
Appends the given path components to each directory in this search path, creating a new SearchPath.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*parts
|
str
|
Path components to append to each directory. |
()
|
Returns:
| Type | Description |
|---|---|
Self
|
A new SearchPath with the path components appended. |
Example
Source code in src/searchpath/_searchpath.py
searchpath.SearchPath.filter ¶
Create a new SearchPath with entries filtered by a predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[Path], bool]
|
A function that takes a Path and returns True to keep the entry or False to exclude it. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
A new SearchPath containing only entries for which the |
Self
|
predicate returned True. |
Example
Source code in src/searchpath/_searchpath.py
searchpath.SearchPath.existing ¶
existing() -> Self
Create a new SearchPath with only existing directories.
This is a shorthand for filter(lambda p: p.exists()).
Returns:
| Type | Description |
|---|---|
Self
|
A new SearchPath containing only directories that exist. |
Example
Source code in src/searchpath/_searchpath.py
searchpath.SearchPath.items ¶
Iterate over (scope, path) pairs in the search path.
Yields:
| Type | Description |
|---|---|
tuple[str, Path]
|
Tuples of (scope_name, directory_path) in order. |
Example
Source code in src/searchpath/_searchpath.py
searchpath.SearchPath.first ¶
first(
pattern: str = "**",
*,
kind: Literal["files", "dirs", "both"] = "files",
include: str | Sequence[str] | None = None,
include_from: Path | str | Sequence[Path | str] | None = None,
include_from_ancestors: str | None = None,
exclude: str | Sequence[str] | None = None,
exclude_from: Path | str | Sequence[Path | str] | None = None,
exclude_from_ancestors: str | None = None,
matcher: PathMatcher | None = None,
follow_symlinks: bool = True
) -> Path | None
Find the first matching path in the search path.
Searches directories in order and returns the first path that matches the pattern and filter criteria. Returns None if no match is found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Glob pattern for matching paths. Defaults to "**" (all). |
'**'
|
kind
|
Literal['files', 'dirs', 'both']
|
What to match: "files", "dirs", or "both". |
'files'
|
include
|
str | Sequence[str] | None
|
Additional patterns paths must match. |
None
|
include_from
|
Path | str | Sequence[Path | str] | None
|
Path(s) to files containing include patterns. |
None
|
include_from_ancestors
|
str | None
|
Filename to load include patterns from ancestor directories. Patterns are collected from the search entry root toward each file's parent directory. |
None
|
exclude
|
str | Sequence[str] | None
|
Patterns that reject paths. |
None
|
exclude_from
|
Path | str | Sequence[Path | str] | None
|
Path(s) to files containing exclude patterns. |
None
|
exclude_from_ancestors
|
str | None
|
Filename to load exclude patterns from ancestor directories. Patterns are collected from the search entry root toward each file's parent directory. |
None
|
matcher
|
PathMatcher | None
|
PathMatcher implementation. Defaults to GlobMatcher. |
None
|
follow_symlinks
|
bool
|
Whether to follow symbolic links. |
True
|
Returns:
| Type | Description |
|---|---|
Path | None
|
The first matching Path, or None if not found. |
Example
Source code in src/searchpath/_searchpath.py
searchpath.SearchPath.match ¶
match(
pattern: str = "**",
*,
kind: Literal["files", "dirs", "both"] = "files",
include: str | Sequence[str] | None = None,
include_from: Path | str | Sequence[Path | str] | None = None,
include_from_ancestors: str | None = None,
exclude: str | Sequence[str] | None = None,
exclude_from: Path | str | Sequence[Path | str] | None = None,
exclude_from_ancestors: str | None = None,
matcher: PathMatcher | None = None,
follow_symlinks: bool = True
) -> Match | None
Find the first matching path with provenance information.
Like first(), but returns a Match object containing the path along with the scope name and source directory. Returns None if no match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Glob pattern for matching paths. Defaults to "**" (all). |
'**'
|
kind
|
Literal['files', 'dirs', 'both']
|
What to match: "files", "dirs", or "both". |
'files'
|
include
|
str | Sequence[str] | None
|
Additional patterns paths must match. |
None
|
include_from
|
Path | str | Sequence[Path | str] | None
|
Path(s) to files containing include patterns. |
None
|
include_from_ancestors
|
str | None
|
Filename to load include patterns from ancestor directories. Patterns are collected from the search entry root toward each file's parent directory. |
None
|
exclude
|
str | Sequence[str] | None
|
Patterns that reject paths. |
None
|
exclude_from
|
Path | str | Sequence[Path | str] | None
|
Path(s) to files containing exclude patterns. |
None
|
exclude_from_ancestors
|
str | None
|
Filename to load exclude patterns from ancestor directories. Patterns are collected from the search entry root toward each file's parent directory. |
None
|
matcher
|
PathMatcher | None
|
PathMatcher implementation. Defaults to GlobMatcher. |
None
|
follow_symlinks
|
bool
|
Whether to follow symbolic links. |
True
|
Returns:
| Type | Description |
|---|---|
Match | None
|
The first Match object, or None if not found. |
Example
Source code in src/searchpath/_searchpath.py
searchpath.SearchPath.all ¶
all(
pattern: str = "**",
*,
kind: Literal["files", "dirs", "both"] = "files",
dedupe: bool = True,
include: str | Sequence[str] | None = None,
include_from: Path | str | Sequence[Path | str] | None = None,
include_from_ancestors: str | None = None,
exclude: str | Sequence[str] | None = None,
exclude_from: Path | str | Sequence[Path | str] | None = None,
exclude_from_ancestors: str | None = None,
matcher: PathMatcher | None = None,
follow_symlinks: bool = True
) -> list[Path]
Find all matching paths in the search path.
Searches all directories and returns paths matching the pattern and filter criteria. By default, deduplicates by relative path, keeping the first occurrence (from higher priority directories).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Glob pattern for matching paths. Defaults to "**" (all). |
'**'
|
kind
|
Literal['files', 'dirs', 'both']
|
What to match: "files", "dirs", or "both". |
'files'
|
dedupe
|
bool
|
If True, keep only first occurrence per relative path. |
True
|
include
|
str | Sequence[str] | None
|
Additional patterns paths must match. |
None
|
include_from
|
Path | str | Sequence[Path | str] | None
|
Path(s) to files containing include patterns. |
None
|
include_from_ancestors
|
str | None
|
Filename to load include patterns from ancestor directories. Patterns are collected from the search entry root toward each file's parent directory. |
None
|
exclude
|
str | Sequence[str] | None
|
Patterns that reject paths. |
None
|
exclude_from
|
Path | str | Sequence[Path | str] | None
|
Path(s) to files containing exclude patterns. |
None
|
exclude_from_ancestors
|
str | None
|
Filename to load exclude patterns from ancestor directories. Patterns are collected from the search entry root toward each file's parent directory. |
None
|
matcher
|
PathMatcher | None
|
PathMatcher implementation. Defaults to GlobMatcher. |
None
|
follow_symlinks
|
bool
|
Whether to follow symbolic links. |
True
|
Returns:
| Type | Description |
|---|---|
list[Path]
|
List of matching Path objects. |
Example
Source code in src/searchpath/_searchpath.py
searchpath.SearchPath.matches ¶
matches(
pattern: str = "**",
*,
kind: Literal["files", "dirs", "both"] = "files",
dedupe: bool = True,
include: str | Sequence[str] | None = None,
include_from: Path | str | Sequence[Path | str] | None = None,
include_from_ancestors: str | None = None,
exclude: str | Sequence[str] | None = None,
exclude_from: Path | str | Sequence[Path | str] | None = None,
exclude_from_ancestors: str | None = None,
matcher: PathMatcher | None = None,
follow_symlinks: bool = True
) -> list[Match]
Find all matching paths with provenance information.
Like all(), but returns Match objects containing paths along with scope names and source directories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Glob pattern for matching paths. Defaults to "**" (all). |
'**'
|
kind
|
Literal['files', 'dirs', 'both']
|
What to match: "files", "dirs", or "both". |
'files'
|
dedupe
|
bool
|
If True, keep only first occurrence per relative path. |
True
|
include
|
str | Sequence[str] | None
|
Additional patterns paths must match. |
None
|
include_from
|
Path | str | Sequence[Path | str] | None
|
Path(s) to files containing include patterns. |
None
|
include_from_ancestors
|
str | None
|
Filename to load include patterns from ancestor directories. Patterns are collected from the search entry root toward each file's parent directory. |
None
|
exclude
|
str | Sequence[str] | None
|
Patterns that reject paths. |
None
|
exclude_from
|
Path | str | Sequence[Path | str] | None
|
Path(s) to files containing exclude patterns. |
None
|
exclude_from_ancestors
|
str | None
|
Filename to load exclude patterns from ancestor directories. Patterns are collected from the search entry root toward each file's parent directory. |
None
|
matcher
|
PathMatcher | None
|
PathMatcher implementation. Defaults to GlobMatcher. |
None
|
follow_symlinks
|
bool
|
Whether to follow symbolic links. |
True
|
Returns:
| Type | Description |
|---|---|
list[Match]
|
List of Match objects. |
Example
Source code in src/searchpath/_searchpath.py
Match class¶
Result object containing a matched path with provenance information.
searchpath.Match
dataclass
¶
Result of a search path lookup.
A Match represents a file or directory found during a search path lookup. It includes the absolute path to the match, the scope name identifying which search path entry it came from, and the source directory.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
Path
|
Absolute path to the matched file or directory. |
scope |
str
|
Scope name of the search path entry (e.g., "user", "project"). |
source |
Path
|
The search path directory this match came from. |
Example
Source code in src/searchpath/_match.py
searchpath.Match.path
instance-attribute
¶
path: Path
Absolute path to the matched file or directory.
searchpath.Match.scope
instance-attribute
¶
scope: str
Scope name of the search path entry (e.g., "user", "project").
Entry type¶
Type alias for SearchPath entry arguments.
searchpath.Entry
module-attribute
¶
Entry: TypeAlias = 'tuple[str, Path | str | None] | Path | str | None'
Type alias for SearchPath entry arguments.
Pattern matchers¶
PathMatcher protocol¶
Protocol defining the interface for pattern matching implementations.
searchpath.PathMatcher ¶
Bases: Protocol
Protocol for pattern matching implementations.
Path matchers check if paths match include/exclude pattern lists. Each matcher handles pattern compilation internally.
Example
Methods:
| Name | Description |
|---|---|
matches |
Check if path matches the include/exclude patterns. |
Attributes:
| Name | Type | Description |
|---|---|---|
supports_negation |
bool
|
Whether this matcher supports negation patterns (e.g., !pattern). |
supports_dir_only |
bool
|
Whether this matcher supports directory-only patterns (e.g., pattern/). |
Source code in src/searchpath/_matchers.py
searchpath.PathMatcher.supports_negation
property
¶
supports_negation: bool
Whether this matcher supports negation patterns (e.g., !pattern).
searchpath.PathMatcher.supports_dir_only
property
¶
supports_dir_only: bool
Whether this matcher supports directory-only patterns (e.g., pattern/).
searchpath.PathMatcher.matches ¶
matches(
path: str,
*,
is_dir: bool = False,
include: Sequence[str] = (),
exclude: Sequence[str] = ()
) -> bool
Check if path matches the include/exclude patterns.
A path matches if: - It matches at least one include pattern (or include is empty), AND - It does not match any exclude pattern
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Relative path from search root (forward slashes). |
required |
is_dir
|
bool
|
Whether the path represents a directory. |
False
|
include
|
Sequence[str]
|
Patterns the path must match (empty = match all). |
()
|
exclude
|
Sequence[str]
|
Patterns that reject the path. |
()
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the path should be included in results. |
Source code in src/searchpath/_matchers.py
GlobMatcher¶
Default pattern matcher using glob-style patterns.
searchpath.GlobMatcher ¶
Path matcher using glob-style patterns.
Patterns are translated to regular expressions internally. The ** pattern is only treated as recursive when it appears as a complete path component (e.g., /, foo//bar), matching gitignore semantics.
Supports
*: Matches any characters except/**: Matches any characters including/(when complete component)?: Matches any single character except/[abc]: Matches any character in the set[!abc]or[^abc]: Matches any character not in the set[a-z]: Matches any character in the range
Does not support
- Negation patterns (
!pattern) - Directory-only patterns (
pattern/) - Anchored patterns (
/pattern)
Example
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the matcher with an empty pattern cache. |
matches |
Check if path matches the include/exclude patterns. |
Attributes:
| Name | Type | Description |
|---|---|---|
supports_negation |
bool
|
Whether this matcher supports negation patterns. |
supports_dir_only |
bool
|
Whether this matcher supports directory-only patterns. |
Source code in src/searchpath/_matchers.py
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 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 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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |
searchpath.GlobMatcher.supports_negation
property
¶
supports_negation: bool
Whether this matcher supports negation patterns.
Returns:
| Type | Description |
|---|---|
bool
|
Always False for GlobMatcher. |
searchpath.GlobMatcher.supports_dir_only
property
¶
supports_dir_only: bool
Whether this matcher supports directory-only patterns.
Returns:
| Type | Description |
|---|---|
bool
|
Always False for GlobMatcher. |
searchpath.GlobMatcher.__init__ ¶
searchpath.GlobMatcher.matches ¶
matches(
path: str,
*,
is_dir: bool = False,
include: Sequence[str] = (),
exclude: Sequence[str] = ()
) -> bool
Check if path matches the include/exclude patterns.
A path matches if: - It matches at least one include pattern (or include is empty), AND - It does not match any exclude pattern
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Relative path from search root (forward slashes). |
required |
is_dir
|
bool
|
Whether the path represents a directory (ignored by GlobMatcher). |
False
|
include
|
Sequence[str]
|
Patterns the path must match (empty = match all). |
()
|
exclude
|
Sequence[str]
|
Patterns that reject the path. |
()
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the path should be included in results. |
Raises:
| Type | Description |
|---|---|
PatternSyntaxError
|
If any pattern has invalid syntax. |
Example
Source code in src/searchpath/_matchers.py
RegexMatcher¶
Pattern matcher using Python regular expressions.
searchpath.RegexMatcher ¶
Path matcher using Python regular expressions.
Uses the re module for full regex syntax. Patterns are matched against the entire path using fullmatch() for consistency with GlobMatcher.
Supports
- Full Python regex syntax (re module)
- Character classes, quantifiers, alternation, groups
Does not support
- Negation patterns (
!pattern) - Directory-only patterns (
pattern/)
Example
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the matcher with an empty pattern cache. |
matches |
Check if path matches the include/exclude patterns. |
Attributes:
| Name | Type | Description |
|---|---|---|
supports_negation |
bool
|
Whether this matcher supports negation patterns. |
supports_dir_only |
bool
|
Whether this matcher supports directory-only patterns. |
Source code in src/searchpath/_matchers.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | |
searchpath.RegexMatcher.supports_negation
property
¶
supports_negation: bool
Whether this matcher supports negation patterns.
Returns:
| Type | Description |
|---|---|
bool
|
Always False for RegexMatcher. |
searchpath.RegexMatcher.supports_dir_only
property
¶
supports_dir_only: bool
Whether this matcher supports directory-only patterns.
Returns:
| Type | Description |
|---|---|
bool
|
Always False for RegexMatcher. |
searchpath.RegexMatcher.__init__ ¶
searchpath.RegexMatcher.matches ¶
matches(
path: str,
*,
is_dir: bool = False,
include: Sequence[str] = (),
exclude: Sequence[str] = ()
) -> bool
Check if path matches the include/exclude patterns.
A path matches if: - It matches at least one include pattern (or include is empty), AND - It does not match any exclude pattern
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Relative path from search root (forward slashes). |
required |
is_dir
|
bool
|
Whether the path represents a directory (ignored by RegexMatcher). |
False
|
include
|
Sequence[str]
|
Patterns the path must match (empty = match all). |
()
|
exclude
|
Sequence[str]
|
Patterns that reject the path. |
()
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the path should be included in results. |
Raises:
| Type | Description |
|---|---|
PatternSyntaxError
|
If any pattern has invalid regex syntax. |
Source code in src/searchpath/_matchers.py
GitignoreMatcher¶
Pattern matcher using gitignore-style patterns via the pathspec library.
searchpath.GitignoreMatcher ¶
Path matcher using gitignore-style patterns via pathspec library.
Provides full gitignore compatibility including
*: Matches any characters except/**: Recursive directory matching?: Matches any single character except/[abc]: Character classes!pattern: Negation (un-ignores previously matched paths)pattern/: Directory-only patterns/pattern: Anchored patterns (match from root only)
Requires the optional pathspec package. Install with::
pip install searchpath[gitignore]
Example
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the matcher, checking for pathspec availability. |
matches |
Check if path matches the include/exclude patterns. |
Attributes:
| Name | Type | Description |
|---|---|---|
supports_negation |
bool
|
Whether this matcher supports negation patterns. |
supports_dir_only |
bool
|
Whether this matcher supports directory-only patterns. |
Source code in src/searchpath/_matchers.py
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 | |
searchpath.GitignoreMatcher.supports_negation
property
¶
supports_negation: bool
Whether this matcher supports negation patterns.
Returns:
| Type | Description |
|---|---|
bool
|
Always True for GitignoreMatcher. |
searchpath.GitignoreMatcher.supports_dir_only
property
¶
supports_dir_only: bool
Whether this matcher supports directory-only patterns.
Returns:
| Type | Description |
|---|---|
bool
|
Always True for GitignoreMatcher. |
searchpath.GitignoreMatcher.__init__ ¶
Source code in src/searchpath/_matchers.py
searchpath.GitignoreMatcher.matches ¶
matches(
path: str,
*,
is_dir: bool = False,
include: Sequence[str] = (),
exclude: Sequence[str] = ()
) -> bool
Check if path matches the include/exclude patterns.
Uses gitignore semantics where patterns are evaluated in order and negation patterns (!pattern) can re-include previously excluded paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Relative path from search root (forward slashes). |
required |
is_dir
|
bool
|
Whether the path represents a directory. |
False
|
include
|
Sequence[str]
|
Patterns the path must match (empty = match all). |
()
|
exclude
|
Sequence[str]
|
Patterns that reject the path. |
()
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the path should be included in results. |
Raises:
| Type | Description |
|---|---|
PatternSyntaxError
|
If any pattern has invalid syntax. |
Source code in src/searchpath/_matchers.py
Exceptions¶
Base exception¶
searchpath.SearchPathError ¶
Pattern exceptions¶
searchpath.PatternError ¶
Bases: SearchPathError
Base exception for pattern-related errors.
searchpath.PatternSyntaxError ¶
Bases: PatternError
Raised when a pattern has invalid syntax.
Attributes:
| Name | Type | Description |
|---|---|---|
pattern |
str
|
The pattern that failed to parse. |
message |
str
|
Description of the syntax error. |
position |
int | None
|
Character position where the error occurred, or None. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize a PatternSyntaxError. |
Source code in src/searchpath/_exceptions.py
searchpath.PatternSyntaxError.__init__ ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
The pattern that failed to parse. |
required |
message
|
str
|
Description of the syntax error. |
required |
position
|
int | None
|
Character position where the error occurred, or None. |
None
|
Source code in src/searchpath/_exceptions.py
searchpath.PatternFileError ¶
Bases: PatternError
Raised when a pattern file cannot be read or parsed.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
Path
|
Path to the pattern file. |
message |
str
|
Description of the error. |
line_number |
int | None
|
Line number where the error occurred, or None. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize a PatternFileError. |
Source code in src/searchpath/_exceptions.py
searchpath.PatternFileError.__init__ ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the pattern file. |
required |
message
|
str
|
Description of the error. |
required |
line_number
|
int | None
|
Line number where the error occurred, or None. |
None
|
Source code in src/searchpath/_exceptions.py
Configuration exceptions¶
searchpath.ConfigurationError ¶
Bases: SearchPathError
Raised when SearchPath configuration is invalid.