| 1 |
""" |
| 2 |
This module provides private utility functions for backends. |
| 3 |
|
| 4 |
WARNING: The *pathspec._backends* package is not part of the public API. Its |
| 5 |
contents and structure are likely to change. |
| 6 |
""" |
| 7 |
|
| 8 |
from collections.abc import ( |
| 9 |
Iterable) |
| 10 |
from typing import ( |
| 11 |
TypeVar) |
| 12 |
|
| 13 |
from pathspec.pattern import ( |
| 14 |
Pattern) |
| 15 |
|
| 16 |
TPattern = TypeVar("TPattern", bound=Pattern) |
| 17 |
|
| 18 |
|
| 19 |
def enumerate_patterns( |
| 20 |
patterns: Iterable[TPattern], |
| 21 |
filter: bool, |
| 22 |
reverse: bool, |
| 23 |
) -> list[tuple[int, TPattern]]: |
| 24 |
""" |
| 25 |
Enumerate the patterns. |
| 26 |
|
| 27 |
*patterns* (:class:`Iterable` of :class:`.Pattern`) contains the patterns. |
| 28 |
|
| 29 |
*filter* (:class:`bool`) is whether to remove no-op patterns (:data:`True`), |
| 30 |
or keep them (:data:`False`). |
| 31 |
|
| 32 |
*reverse* (:class:`bool`) is whether to reverse the pattern order |
| 33 |
(:data:`True`), or keep the order (:data:`True`). |
| 34 |
|
| 35 |
Returns the enumerated patterns (:class:`list` of :class:`tuple`). |
| 36 |
""" |
| 37 |
out_patterns = [ |
| 38 |
(__i, __pat) |
| 39 |
for __i, __pat in enumerate(patterns) |
| 40 |
if not filter or __pat.include is not None |
| 41 |
] |
| 42 |
if reverse: |
| 43 |
out_patterns.reverse() |
| 44 |
|
| 45 |
return out_patterns |