Debian Codegraph @

pathspec/_backends/agg.py

raw
1 """
2 This module provides aggregated private data and utilities functions about the
3 available backends.
4
5 WARNING: The *pathspec._backends* package is not part of the public API. Its
6 contents and structure are likely to change.
7 """
8
9 from collections.abc import (
10 Sequence)
11 from typing import (
12 cast)
13
14 from pathspec.backend import (
15 BackendNamesHint,
16 _Backend)
17 from pathspec.pattern import (
18 Pattern,
19 RegexPattern)
20
21 from .hyperscan.base import (
22 hyperscan_error)
23 from .hyperscan.gitignore import (
24 HyperscanGiBackend)
25 from .hyperscan.pathspec import (
26 HyperscanPsBackend)
27 from .re2.base import (
28 re2_error)
29 from .re2.gitignore import (
30 Re2GiBackend)
31 from .re2.pathspec import (
32 Re2PsBackend)
33 from .simple.gitignore import (
34 SimpleGiBackend)
35 from .simple.pathspec import (
36 SimplePsBackend)
37
38 _BEST_BACKEND: BackendNamesHint
39 """
40 The best available backend.
41 """
42
43 if re2_error is None:
44 _BEST_BACKEND = 're2'
45 elif hyperscan_error is None:
46 _BEST_BACKEND = 'hyperscan'
47 else:
48 _BEST_BACKEND = 'simple'
49
50
51 def make_gitignore_backend(
52 name: BackendNamesHint,
53 patterns: Sequence[Pattern],
54 ) -> _Backend:
55 """
56 Create the specified backend with the supplied patterns for
57 :class:`~pathspec.gitignore.GitIgnoreSpec`.
58
59 *name* (:class:`str`) is the name of the backend.
60
61 *patterns* (:class:`.Iterable` of :class:`.Pattern`) contains the compiled
62 patterns.
63
64 Returns the backend (:class:`._Backend`).
65 """
66 if name == 'best':
67 name = _BEST_BACKEND
68
69 if name == 'hyperscan':
70 return HyperscanGiBackend(cast(Sequence[RegexPattern], patterns))
71 elif name == 're2':
72 return Re2GiBackend(cast(Sequence[RegexPattern], patterns))
73 elif name == 'simple':
74 return SimpleGiBackend(cast(Sequence[RegexPattern], patterns))
75 else:
76 raise ValueError(f"Backend {name=!r} is invalid.")
77
78
79 def make_pathspec_backend(
80 name: BackendNamesHint,
81 patterns: Sequence[Pattern],
82 ) -> _Backend:
83 """
84 Create the specified backend with the supplied patterns for
85 :class:`~pathspec.pathspec.PathSpec`.
86
87 *name* (:class:`str`) is the name of the backend.
88
89 *patterns* (:class:`Iterable` of :class:`Pattern`) contains the compiled
90 patterns.
91
92 Returns the backend (:class:`._Backend`).
93 """
94 if name == 'best':
95 name = _BEST_BACKEND
96
97 if name == 'hyperscan':
98 return HyperscanPsBackend(cast(Sequence[RegexPattern], patterns))
99 elif name == 're2':
100 return Re2PsBackend(cast(Sequence[RegexPattern], patterns))
101 elif name == 'simple':
102 return SimplePsBackend(patterns)
103 else:
104 raise ValueError(f"Backend {name=!r} is invalid.")