Debian Codegraph @

pathspec/_backends/re2/_base.py

raw
1 """
2 This module provides private data for the base implementation for the
3 :module:`re2` library.
4
5 WARNING: The *pathspec._backends.re2* package is not part of the public API. Its
6 contents and structure are likely to change.
7 """
8 from __future__ import annotations
9
10 from dataclasses import (
11 dataclass)
12 from typing import (
13 Optional, # Replaced by `X | None` in 3.10.
14 Union) # Replaced by `X | Y` in 3.10.
15
16 try:
17 import re2
18 re2_error = None
19 except ModuleNotFoundError as e:
20 re2 = None # type: ignore[assignment]
21 re2_error = e.with_traceback(None)
22 RE2_OPTIONS = None
23 else:
24 # Both the `google-re2` and `pyre2` libraries use the `re2` namespace.
25 # `google-re2` is the only one currently supported.
26 try:
27 RE2_OPTIONS = re2.Options()
28 RE2_OPTIONS.log_errors = False
29 RE2_OPTIONS.never_capture = True
30 except Exception as e:
31 re2_error = e.with_traceback(None) # type: ignore[assignment]
32 RE2_OPTIONS = None
33
34 RE2_OPTIONS: re2.Options # type: ignore[no-redef]
35 """
36 The re2 options to use:
37
38 - `log_errors=False` disables logging to stderr.
39
40 - `never_capture=True` disables capture groups because they effectively cannot
41 be utilized with :class:`re2.Set`.
42 """
43
44 re2_error: Optional[Exception] # type: ignore[no-redef]
45 """
46 *re2_error* (:class:`Exception` or :data:`None`) is the re2 import error.
47 """
48
49
50 @dataclass(frozen=True)
51 class Re2RegexDat(object):
52 """
53 The :class:`Re2RegexDat` class is used to store data related to a regular
54 expression.
55 """
56
57 # The slots argument is not supported until Python 3.10.
58 __slots__ = [
59 'include',
60 'index',
61 'is_dir_pattern',
62 ]
63
64 include: bool
65 """
66 *include* (:class:`bool`) is whether is whether the matched files should be
67 included (:data:`True`), or excluded (:data:`False`).
68 """
69
70 index: int
71 """
72 *index* (:class:`int`) is the pattern index.
73 """
74
75 is_dir_pattern: bool
76 """
77 *is_dir_pattern* (:class:`bool`) is whether the pattern is a directory
78 pattern for gitignore.
79 """
80
81
82 @dataclass(frozen=True)
83 class Re2RegexDebug(Re2RegexDat):
84 """
85 The :class:`Re2RegexDebug` class stores additional debug information related
86 to a regular expression.
87 """
88
89 # The slots argument is not supported until Python 3.10.
90 __slots__ = ['regex']
91
92 regex: Union[str, bytes]
93 """
94 *regex* (:class:`str` or :class:`bytes`) is the regular expression.
95 """