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