1"""
2This module provides the simple backend for :class:`~pathspec.pathspec.PathSpec`.
3
4WARNING: The *pathspec._backends.simple* package is not part of the public API.
5Its contents and structure are likely to change.
6"""
7
8from collections.abc import (
9 Sequence)
10from typing import (
11 Optional) # Replaced by `X | None` in 3.10.
12
13from ...pattern import (
14 Pattern)
15from ..._typing import (
16 override) # Added in 3.12.
17from ...util import (
18 check_match_file)
19
20from ..base import (
21 Backend)
22from .._utils import (
23 enumerate_patterns)
24
25
26class SimplePsBackend(Backend):
27 """
28 The :class:`SimplePsBackend` class is the default (or simple) implementation
29 used by :class:`~pathspec.pathspec.PathSpec` for matching files.
30 """
31
32 def __init__(
33 self,
34 patterns: Sequence[Pattern],
35 *,
36 no_filter: Optional[bool] = None,
37 no_reverse: Optional[bool] = None,
38 ) -> None:
39 """
40 Initialize the :class:`SimplePsBackend` instance.
41
42 *patterns* (:class:`Sequence` of :class:`.Pattern`) contains the compiled
43 patterns.
44
45 *no_filter* (:class:`bool`) is whether to keep no-op patterns (:data:`True`),
46 or remove them (:data:`False`).
47
48 *no_reverse* (:class:`bool`) is whether to keep the pattern order
49 (:data:`True`), or reverse the order (:data:`True`).
50 """
51
52 self._is_reversed: bool = not no_reverse
53 """
54 *_is_reversed* (:class:`bool`) is whether to the pattern order was reversed.
55 """
56
57 self._patterns: list[tuple[int, Pattern]] = enumerate_patterns(
58 patterns, filter=not no_filter, reverse=not no_reverse,
59 )
60 """
61 *_patterns* (:class:`list` of :class:`tuple`) contains the enumerated
62 patterns.
63 """
64
65 @override
66 def match_file(self, file: str) -> tuple[Optional[bool], Optional[int]]:
67 """
68 Check the file against the patterns.
69
70 *file* (:class:`str`) is the normalized file path to check.
71
72 Returns a :class:`tuple` containing whether to include *file* (:class:`bool`
73 or :data:`None`), and the index of the last matched pattern (:class:`int` or
74 :data:`None`).
75 """
76 return check_match_file(self._patterns, file, self._is_reversed)