|
1 | 1 | """The standard `search` function extension.""" |
2 | 2 |
|
3 | | -from typing import Optional |
| 3 | +from ._pattern import AbstractRegexFilterFunction |
4 | 4 |
|
5 | | -try: |
6 | | - import regex as re |
7 | 5 |
|
8 | | - REGEX_AVAILABLE = True |
9 | | -except ImportError: # pragma: no cover |
10 | | - import re # type: ignore |
| 6 | +class Search(AbstractRegexFilterFunction): |
| 7 | + """The standard `search` function.""" |
11 | 8 |
|
12 | | - REGEX_AVAILABLE = False |
13 | | - |
14 | | -try: |
15 | | - from iregexp_check import check |
16 | | - |
17 | | - IREGEXP_AVAILABLE = True |
18 | | -except ImportError: # pragma: no cover |
19 | | - IREGEXP_AVAILABLE = False |
20 | | - |
21 | | -from jsonpath.exceptions import JSONPathError |
22 | | -from jsonpath.function_extensions import ExpressionType |
23 | | -from jsonpath.function_extensions import FilterFunction |
24 | | -from jsonpath.lru_cache import LRUCache |
25 | | -from jsonpath.lru_cache import ThreadSafeLRUCache |
26 | | - |
27 | | -from ._pattern import map_re |
28 | | - |
29 | | - |
30 | | -class Search(FilterFunction): |
31 | | - """The standard `search` function. |
32 | | -
|
33 | | - Arguments: |
34 | | - cache_capacity: The size of the regular expression cache. |
35 | | - debug: When `True`, raise an exception when regex pattern compilation |
36 | | - fails. The default - as required by RFC 9535 - is `False`, which |
37 | | - silently ignores bad patterns. |
38 | | - thread_safe: When `True`, use a `ThreadSafeLRUCache` instead of an |
39 | | - instance of `LRUCache`. |
40 | | - """ |
41 | | - |
42 | | - arg_types = [ExpressionType.VALUE, ExpressionType.VALUE] |
43 | | - return_type = ExpressionType.LOGICAL |
44 | | - |
45 | | - def __init__( |
46 | | - self, |
47 | | - *, |
48 | | - cache_capacity: int = 300, |
49 | | - debug: bool = False, |
50 | | - thread_safe: bool = False, |
51 | | - ): |
52 | | - self._cache: LRUCache[str, Optional[re.Pattern[str]]] = ( |
53 | | - ThreadSafeLRUCache(capacity=cache_capacity) |
54 | | - if thread_safe |
55 | | - else LRUCache(capacity=cache_capacity) |
56 | | - ) |
57 | | - |
58 | | - self.debug = debug |
59 | | - |
60 | | - def __call__(self, value: str, pattern: object) -> bool: |
61 | | - """Return `True` if _value_ contains _pattern_, or `False` otherwise.""" |
| 9 | + def __call__(self, value: object, pattern: object) -> bool: |
| 10 | + """Return `True` if _value_ matches _pattern_, or `False` otherwise.""" |
62 | 11 | if not isinstance(value, str) or not isinstance(pattern, str): |
63 | 12 | return False |
64 | 13 |
|
65 | | - try: |
66 | | - _pattern = self._cache[pattern] |
67 | | - except KeyError: |
68 | | - if IREGEXP_AVAILABLE and not check(pattern): |
69 | | - if self.debug: |
70 | | - raise JSONPathError( |
71 | | - "search pattern is not a valid I-Regexp", token=None |
72 | | - ) from None |
73 | | - _pattern = None |
74 | | - else: |
75 | | - if REGEX_AVAILABLE: |
76 | | - pattern = map_re(pattern) |
77 | | - |
78 | | - try: |
79 | | - _pattern = re.compile(pattern) |
80 | | - except re.error: |
81 | | - if self.debug: |
82 | | - raise |
83 | | - _pattern = None |
84 | | - |
85 | | - self._cache[pattern] = _pattern |
| 14 | + _pattern = self.check_cache(pattern) |
86 | 15 |
|
87 | 16 | if _pattern is None: |
88 | 17 | return False |
|
0 commit comments