Repository navigation
Expand file tree
/
Copy pathselectors.py
More file actions
734 lines (589 loc) · 24.1 KB
/
Copy pathselectors.py
File metadata and controls
734 lines (589 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
"""JSONPath segments and selectors, as returned from `Parser.parse`."""
from __future__ import annotations
from abc import ABC
from abc import abstractmethod
from collections.abc import Mapping
from collections.abc import Sequence
from contextlib import suppress
from typing import TYPE_CHECKING
from typing import Any
from typing import AsyncIterable
from typing import Iterable
from typing import Optional
from typing import Union
from .exceptions import JSONPathIndexError
from .exceptions import JSONPathSyntaxError
from .exceptions import JSONPathTypeError
from .match import NodeList
from .serialize import canonical_string
if TYPE_CHECKING:
from .env import JSONPathEnvironment
from .filter import FilterExpression
from .match import JSONPathMatch
from .path import JSONPath
from .token import Token
class JSONPathSelector(ABC):
"""Base class for all JSONPath segments and selectors."""
__slots__ = ("env", "token")
def __init__(self, *, env: JSONPathEnvironment, token: Token) -> None:
self.env = env
self.token = token
@abstractmethod
def resolve(self, node: JSONPathMatch) -> Iterable[JSONPathMatch]:
"""Apply the segment/selector to each node in _matches_.
Arguments:
node: A node matched by preceding segments/selectors.
Returns:
The `JSONPathMatch` instances created by applying this selector to each
preceding node.
"""
@abstractmethod
def resolve_async(self, node: JSONPathMatch) -> AsyncIterable[JSONPathMatch]:
"""An async version of `resolve`."""
class NameSelector(JSONPathSelector):
"""Select at most one object member value given an object member name."""
__slots__ = ("name",)
def __init__(self, *, env: JSONPathEnvironment, token: Token, name: str) -> None:
super().__init__(env=env, token=token)
self.name = name
def __str__(self) -> str:
return canonical_string(self.name)
def __eq__(self, __value: object) -> bool:
return (
isinstance(__value, NameSelector)
and self.name == __value.name
and self.token == __value.token
)
def __hash__(self) -> int:
return hash((self.name, self.token))
def resolve(self, node: JSONPathMatch) -> Iterable[JSONPathMatch]:
if isinstance(node.obj, Mapping):
with suppress(KeyError):
match = node.new_child(self.env.getitem(node.obj, self.name), self.name)
node.add_child(match)
yield match
async def resolve_async(self, node: JSONPathMatch) -> AsyncIterable[JSONPathMatch]:
if isinstance(node.obj, Mapping):
with suppress(KeyError):
match = node.new_child(
await self.env.getitem_async(node.obj, self.name), self.name
)
node.add_child(match)
yield match
class IndexSelector(JSONPathSelector):
"""Select at most one array element value given an index."""
__slots__ = ("index", "_as_key")
def __init__(
self,
*,
env: JSONPathEnvironment,
token: Token,
index: int,
) -> None:
if index < env.min_int_index or index > env.max_int_index:
raise JSONPathIndexError("index out of range", token=token)
super().__init__(env=env, token=token)
self.index = index
self._as_key = str(self.index)
def __str__(self) -> str:
return str(self.index)
def __eq__(self, __value: object) -> bool:
return (
isinstance(__value, IndexSelector)
and self.index == __value.index
and self.token == __value.token
)
def __hash__(self) -> int:
return hash((self.index, self.token))
def _normalized_index(self, obj: Sequence[object]) -> int:
if self.index < 0 and len(obj) >= abs(self.index):
return len(obj) + self.index
return self.index
def resolve(self, node: JSONPathMatch) -> Iterable[JSONPathMatch]:
# Optionally try string representation of int
if not self.env.strict and isinstance(node.obj, Mapping):
# Try the string representation of the index as a key.
with suppress(KeyError):
match = node.new_child(
self.env.getitem(node.obj, self._as_key), self.index
)
node.add_child(match)
yield match
if isinstance(node.obj, Sequence) and not isinstance(node.obj, str):
norm_index = self._normalized_index(node.obj)
with suppress(IndexError):
match = node.new_child(
self.env.getitem(node.obj, self.index), norm_index
)
node.add_child(match)
yield match
async def resolve_async(self, node: JSONPathMatch) -> AsyncIterable[JSONPathMatch]:
if not self.env.strict and isinstance(node.obj, Mapping):
# Try the string representation of the index as a key.
with suppress(KeyError):
match = node.new_child(
await self.env.getitem_async(node.obj, self._as_key), self.index
)
node.add_child(match)
yield match
if isinstance(node.obj, Sequence) and not isinstance(node.obj, str):
norm_index = self._normalized_index(node.obj)
with suppress(IndexError):
match = node.new_child(
await self.env.getitem_async(node.obj, self.index), norm_index
)
node.add_child(match)
yield match
class KeySelector(JSONPathSelector):
"""Select at most one name from an object member, given the name.
The key selector is introduced to facilitate valid normalized paths for nodes
produced by the "keys selector" and the "keys filter selector". It is not expected
to be of much use elsewhere.
NOTE: This is a non-standard selector.
See https://jg-rp.github.io/json-p3/guides/jsonpath-extra#key-selector.
"""
__slots__ = ("key",)
def __init__(self, *, env: JSONPathEnvironment, token: Token, key: str) -> None:
super().__init__(env=env, token=token)
self.key = key
def __str__(self) -> str:
return f"{self.env.keys_selector_token}{canonical_string(self.key)}"
def __eq__(self, __value: object) -> bool:
return (
isinstance(__value, KeySelector)
and self.token == __value.token
and self.key == __value.key
)
def __hash__(self) -> int:
return hash((self.token, self.key))
def resolve(self, node: JSONPathMatch) -> Iterable[JSONPathMatch]:
if isinstance(node.obj, Mapping) and self.key in node.obj:
match = node.__class__(
filter_context=node.filter_context(),
obj=self.key,
parent=node,
parts=node.parts + (f"{self.env.keys_selector_token}{self.key}",),
path=f"{node.path}[{self}]",
root=node.root,
)
node.add_child(match)
yield match
async def resolve_async(self, node: JSONPathMatch) -> AsyncIterable[JSONPathMatch]:
for _node in self.resolve(node):
yield _node
class KeysSelector(JSONPathSelector):
"""Select all names from an object's name/value members.
NOTE: This is a non-standard selector.
See https://jg-rp.github.io/json-p3/guides/jsonpath-extra#keys-selector
"""
__slots__ = ()
def __init__(self, *, env: JSONPathEnvironment, token: Token) -> None:
super().__init__(env=env, token=token)
def __str__(self) -> str:
return self.env.keys_selector_token
def __eq__(self, __value: object) -> bool:
return isinstance(__value, KeysSelector) and self.token == __value.token
def __hash__(self) -> int:
return hash(self.token)
def _keys(self, node: JSONPathMatch) -> Iterable[JSONPathMatch]:
if isinstance(node.obj, Mapping):
for key in node.obj:
match = node.__class__(
filter_context=node.filter_context(),
obj=key,
parent=node,
parts=node.parts + (f"{self.env.keys_selector_token}{key}",),
path=f"{node.path}[{self.env.keys_selector_token}{canonical_string(key)}]",
root=node.root,
)
node.add_child(match)
yield match
def resolve(self, node: JSONPathMatch) -> Iterable[JSONPathMatch]:
yield from self._keys(node)
async def resolve_async(self, node: JSONPathMatch) -> AsyncIterable[JSONPathMatch]:
for match in self._keys(node):
yield match
class SliceSelector(JSONPathSelector):
"""Select array elements given a start index, a stop index and a step."""
__slots__ = ("slice",)
def __init__(
self,
*,
env: JSONPathEnvironment,
token: Token,
start: Optional[int] = None,
stop: Optional[int] = None,
step: Optional[int] = None,
) -> None:
super().__init__(env=env, token=token)
self._check_range(start, stop, step)
self.slice = slice(start, stop, step)
def __str__(self) -> str:
stop = self.slice.stop if self.slice.stop is not None else ""
start = self.slice.start if self.slice.start is not None else ""
step = self.slice.step if self.slice.step is not None else "1"
return f"{start}:{stop}:{step}"
def __eq__(self, __value: object) -> bool:
return (
isinstance(__value, SliceSelector)
and self.slice == __value.slice
and self.token == __value.token
)
def __hash__(self) -> int:
return hash((str(self), self.token))
def _check_range(self, *indices: Optional[int]) -> None:
for i in indices:
if i is not None and (
i < self.env.min_int_index or i > self.env.max_int_index
):
raise JSONPathIndexError("index out of range", token=self.token)
def resolve(self, node: JSONPathMatch) -> Iterable[JSONPathMatch]:
if not isinstance(node.obj, Sequence) or self.slice.step == 0:
return
for norm_index, obj in zip( # noqa: B905
range(*self.slice.indices(len(node.obj))),
self.env.getitem(node.obj, self.slice),
):
match = node.new_child(obj, norm_index)
node.add_child(match)
yield match
async def resolve_async(self, node: JSONPathMatch) -> AsyncIterable[JSONPathMatch]:
if not isinstance(node.obj, Sequence) or self.slice.step == 0:
return
for norm_index, obj in zip( # noqa: B905
range(*self.slice.indices(len(node.obj))),
await self.env.getitem_async(node.obj, self.slice),
):
match = node.new_child(obj, norm_index)
node.add_child(match)
yield match
class WildcardSelector(JSONPathSelector):
"""Select nodes of all children of an object or array."""
__slots__ = ()
def __str__(self) -> str:
return "*"
def __eq__(self, __value: object) -> bool:
return isinstance(__value, WildcardSelector) and self.token == __value.token
def __hash__(self) -> int:
return hash(self.token)
def resolve(self, node: JSONPathMatch) -> Iterable[JSONPathMatch]:
if isinstance(node.obj, Mapping):
for key, val in node.obj.items():
match = node.new_child(val, key)
node.add_child(match)
yield match
elif isinstance(node.obj, Sequence) and not isinstance(node.obj, str):
for i, val in enumerate(node.obj):
match = node.new_child(val, i)
node.add_child(match)
yield match
async def resolve_async(self, node: JSONPathMatch) -> AsyncIterable[JSONPathMatch]:
if isinstance(node.obj, Mapping):
for key, val in node.obj.items():
match = node.new_child(val, key)
node.add_child(match)
yield match
elif isinstance(node.obj, Sequence) and not isinstance(node.obj, str):
for i, val in enumerate(node.obj):
match = node.new_child(val, i)
node.add_child(match)
yield match
class SingularQuerySelector(JSONPathSelector):
"""An embedded absolute query.
The result of the embedded query is used as an object member name or array element
index.
NOTE: This is a non-standard selector.
"""
__slots__ = ("query",)
def __init__(
self, *, env: JSONPathEnvironment, token: Token, query: JSONPath
) -> None:
super().__init__(env=env, token=token)
self.query = query
if env.strict:
raise JSONPathSyntaxError("unexpected query selector", token=token)
def __str__(self) -> str:
return str(self.query)
def __eq__(self, __value: object) -> bool:
return (
isinstance(__value, SingularQuerySelector)
and self.query == __value.query
and self.token == __value.token
)
def __hash__(self) -> int:
return hash((self.query, self.token))
def resolve(self, node: JSONPathMatch) -> Iterable[JSONPathMatch]:
if isinstance(node.obj, Mapping):
nodes = NodeList(self.query.finditer(node.root))
if nodes.empty():
return
value = nodes[0].value
if not isinstance(value, str):
return
with suppress(KeyError):
match = node.new_child(self.env.getitem(node.obj, value), value)
node.add_child(match)
yield match
if isinstance(node.obj, Sequence) and not isinstance(node.obj, str):
nodes = NodeList(self.query.finditer(node.root))
if nodes.empty():
return
value = nodes[0].value
if not isinstance(value, int):
return
index = self._normalized_index(node.obj, value)
with suppress(IndexError):
match = node.new_child(self.env.getitem(node.obj, index), index)
node.add_child(match)
yield match
async def resolve_async(self, node: JSONPathMatch) -> AsyncIterable[JSONPathMatch]:
if isinstance(node.obj, Mapping):
nodes = NodeList(
[match async for match in await self.query.finditer_async(node.root)]
)
if nodes.empty():
return
value = nodes[0].value
if not isinstance(value, str):
return
with suppress(KeyError):
match = node.new_child(
await self.env.getitem_async(node.obj, value), value
)
node.add_child(match)
yield match
if isinstance(node.obj, Sequence) and not isinstance(node.obj, str):
nodes = NodeList(
[match async for match in await self.query.finditer_async(node.root)]
)
if nodes.empty():
return
value = nodes[0].value
if not isinstance(value, int):
return
index = self._normalized_index(node.obj, value)
with suppress(IndexError):
match = node.new_child(
await self.env.getitem_async(node.obj, index), index
)
node.add_child(match)
yield match
def _normalized_index(self, obj: Sequence[object], index: int) -> int:
if index < 0 and len(obj) >= abs(index):
return len(obj) + index
return index
class Filter(JSONPathSelector):
"""Select array elements or object values according to a filter expression."""
__slots__ = ("expression", "cacheable_nodes")
def __init__(
self,
*,
env: JSONPathEnvironment,
token: Token,
expression: FilterExpression,
) -> None:
super().__init__(env=env, token=token)
self.expression = expression
# Compile-time check for cacheable nodes.
self.cacheable_nodes = self.expression.cacheable_nodes()
def __str__(self) -> str:
return f"?{self.expression}"
def __eq__(self, __value: object) -> bool:
return (
isinstance(__value, Filter)
and self.expression == __value.expression
and self.token == __value.token
)
def __hash__(self) -> int:
return hash((str(self.expression), self.token))
def resolve(self, node: JSONPathMatch) -> Iterable[JSONPathMatch]:
if self.cacheable_nodes and self.env.filter_caching:
expr = self.expression.cache_tree()
else:
expr = self.expression
if isinstance(node.obj, Mapping):
for key, val in node.obj.items():
context = FilterContext(
env=self.env,
current=val,
root=node.root,
extra_context=node.filter_context(),
current_key=key,
)
try:
if expr.evaluate(context):
match = node.new_child(val, key)
node.add_child(match)
yield match
except JSONPathTypeError as err:
if not err.token:
err.token = self.token
raise
elif isinstance(node.obj, Sequence) and not isinstance(node.obj, str):
for i, obj in enumerate(node.obj):
context = FilterContext(
env=self.env,
current=obj,
root=node.root,
extra_context=node.filter_context(),
current_key=i,
)
try:
if expr.evaluate(context):
match = node.new_child(obj, i)
node.add_child(match)
yield match
except JSONPathTypeError as err:
if not err.token:
err.token = self.token
raise
async def resolve_async(self, node: JSONPathMatch) -> AsyncIterable[JSONPathMatch]:
if self.cacheable_nodes and self.env.filter_caching:
expr = self.expression.cache_tree()
else:
expr = self.expression
if isinstance(node.obj, Mapping):
for key, val in node.obj.items():
context = FilterContext(
env=self.env,
current=val,
root=node.root,
extra_context=node.filter_context(),
current_key=key,
)
try:
result = await expr.evaluate_async(context)
except JSONPathTypeError as err:
if not err.token:
err.token = self.token
raise
if result:
match = node.new_child(val, key)
node.add_child(match)
yield match
elif isinstance(node.obj, Sequence) and not isinstance(node.obj, str):
for i, obj in enumerate(node.obj):
context = FilterContext(
env=self.env,
current=obj,
root=node.root,
extra_context=node.filter_context(),
current_key=i,
)
try:
result = await expr.evaluate_async(context)
except JSONPathTypeError as err:
if not err.token:
err.token = self.token
raise
if result:
match = node.new_child(obj, i)
node.add_child(match)
yield match
class KeysFilter(JSONPathSelector):
"""Selects names from an object's name/value members.
NOTE: This is a non-standard selector.
See https://jg-rp.github.io/json-p3/guides/jsonpath-extra#keys-filter-selector
"""
__slots__ = ("expression",)
def __init__(
self,
*,
env: JSONPathEnvironment,
token: Token,
expression: FilterExpression,
) -> None:
super().__init__(env=env, token=token)
self.expression = expression
def __str__(self) -> str:
return f"~?{self.expression}"
def __eq__(self, __value: object) -> bool:
return (
isinstance(__value, Filter)
and self.expression == __value.expression
and self.token == __value.token
)
def __hash__(self) -> int:
return hash(("~", str(self.expression), self.token))
def resolve(self, node: JSONPathMatch) -> Iterable[JSONPathMatch]:
if isinstance(node.value, Mapping):
for key, val in node.value.items():
context = FilterContext(
env=self.env,
current=val,
root=node.root,
extra_context=node.filter_context(),
current_key=key,
)
try:
if self.expression.evaluate(context):
match = node.__class__(
filter_context=node.filter_context(),
obj=key,
parent=node,
parts=node.parts
+ (f"{self.env.keys_selector_token}{key}",),
path=f"{node.path}[{self.env.keys_selector_token}{canonical_string(key)}]",
root=node.root,
)
node.add_child(match)
yield match
except JSONPathTypeError as err:
if not err.token:
err.token = self.token
raise
async def resolve_async(self, node: JSONPathMatch) -> AsyncIterable[JSONPathMatch]:
if isinstance(node.value, Mapping):
for key, val in node.value.items():
context = FilterContext(
env=self.env,
current=val,
root=node.root,
extra_context=node.filter_context(),
current_key=key,
)
try:
if await self.expression.evaluate_async(context):
match = node.__class__(
filter_context=node.filter_context(),
obj=key,
parent=node,
parts=node.parts
+ (f"{self.env.keys_selector_token}{key}",),
path=f"{node.path}[{self.env.keys_selector_token}{canonical_string(key)}]",
root=node.root,
)
node.add_child(match)
yield match
except JSONPathTypeError as err:
if not err.token:
err.token = self.token
raise
class FilterContext:
"""Contextual information and data for evaluating a filter expression."""
__slots__ = (
"current_key",
"current",
"env",
"extra_context",
"root",
)
def __init__(
self,
*,
env: JSONPathEnvironment,
current: object,
root: Union[Sequence[Any], Mapping[str, Any]],
extra_context: Optional[Mapping[str, Any]] = None,
current_key: Union[str, int, None] = None,
) -> None:
self.env = env
self.current = current
self.root = root
self.extra_context = extra_context or {}
self.current_key = current_key
def __str__(self) -> str:
return (
f"FilterContext(current={self.current}, "
f"extra_context={self.extra_context!r})"
)