1"""
2This module provides private data for the base implementation for the
3:module:`re2` library.
4
5WARNING: The *pathspec._backends.re2* package is not part of the public API. Its
6contents and structure are likely to change.
7"""
8from __future__ import annotations
9
10from dataclasses import (
11 dataclass)
12from typing import (
13 Optional, # Replaced by `X | None` in 3.10.
14 Union) # Replaced by `X | Y` in 3.10.
15
16try:
17 import re2
18 re2_error = None
19except ModuleNotFoundError as e:
20 re2 = None
21 re2_error = e
22 RE2_OPTIONS = None
23else:
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
32 RE2_OPTIONS = None
33
34RE2_OPTIONS: re2.Options
35"""
36The 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
44re2_error: Optional[Exception]
45"""
46*re2_error* (:class:`Exception` or :data:`None`) is the re2 import error.
47"""
48
49
50@dataclass(frozen=True)
51class 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)
83class 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 """