1212from typing import Tuple
1313from typing import TypeVar
1414from typing import Union
15- from typing import TYPE_CHECKING
1615
1716from .match import JSONPathMatch
18-
19- if TYPE_CHECKING :
20- from .selectors import JSONPathSelector
17+ from .selectors import JSONPathSelector
2118
2219
2320class JSONPath :
2421 """A compiled JSONPath ready to be applied to a JSON string or Python object."""
2522
2623 __slots__ = ("_selectors" ,)
2724
25+ # TODO: pass env?
26+
27+ # TODO: Store these on env
28+ root_token = "$"
29+ union_token = "|"
30+ intersection_token = "&"
31+
2832 def __init__ (self , * , selectors : Iterable [JSONPathSelector ]) -> None :
2933 self ._selectors = list (selectors )
3034
35+ def __str__ (self ) -> str :
36+ return self .root_token + "" .join (str (selector ) for selector in self ._selectors )
37+
3138 def findall (
3239 self , data : Union [str , Sequence [Any ], Mapping [str , Any ]]
3340 ) -> List [object ]:
@@ -47,7 +54,7 @@ def finditer(
4754 data = json .loads (data )
4855
4956 matches : Iterable [JSONPathMatch ] = [
50- JSONPathMatch (path = "$" , obj = data , root = data )
57+ JSONPathMatch (path = self . root_token , obj = data , root = data )
5158 ]
5259
5360 for selector in self ._selectors :
@@ -73,7 +80,7 @@ async def finditer_async(
7380 data = json .loads (data )
7481
7582 async def root_iter () -> AsyncIterable [JSONPathMatch ]:
76- yield JSONPathMatch (path = "$" , obj = data , root = data )
83+ yield JSONPathMatch (path = self . root_token , obj = data , root = data )
7784
7885 matches : AsyncIterable [JSONPathMatch ] = root_iter ()
7986
@@ -88,22 +95,32 @@ class CompoundJSONPath:
8895
8996 __slots__ = ("path" , "paths" )
9097
98+ root_token = "$"
99+ union_token = "|"
100+ intersection_token = "&"
101+
91102 def __init__ (self , path : Union [JSONPath , CompoundJSONPath ]) -> None :
92103 self .path = path
93104 self .paths : List [Tuple [(str , JSONPath )]] = []
94105
106+ def __str__ (self ) -> str :
107+ buf : List [str ] = [str (self .path )]
108+ for op , path in self .paths :
109+ buf .append (f" { op } " )
110+ buf .append (str (path ))
111+ return "" .join (buf )
112+
95113 def findall (
96114 self , data : Union [str , Sequence [Any ], Mapping [str , Any ]]
97115 ) -> List [object ]:
98116 """Return a list of objects matching this path in the given data."""
99117 objs = self .path .findall (data )
100118
101119 for op , path in self .paths :
102- assert op in ("|" , "&" )
103120 _objs = path .findall (data )
104- if op == "|" :
121+ if op == self . union_token :
105122 objs .extend (_objs )
106- elif op == "&" :
123+ elif op == self . intersection_token :
107124 objs = [obj for obj in objs if obj in _objs ]
108125
109126 return objs
@@ -116,11 +133,10 @@ def finditer(
116133 matches = self .path .finditer (data )
117134
118135 for op , path in self .paths :
119- assert op in ("|" , "&" )
120136 _matches = path .finditer (data )
121- if op == "|" :
137+ if op == self . union_token :
122138 matches = itertools .chain (matches , _matches )
123- elif op == "&" :
139+ elif op == self . intersection_token :
124140 _objs = [match .obj for match in _matches ]
125141 _matches = (match for match in matches if match .obj in _objs )
126142
@@ -133,11 +149,10 @@ async def findall_async(
133149 objs = await self .path .findall_async (data )
134150
135151 for op , path in self .paths :
136- assert op in ("|" , "&" )
137152 _objs = await path .findall_async (data )
138- if op == "|" :
153+ if op == self . union_token :
139154 objs .extend (_objs )
140- elif op == "&" :
155+ elif op == self . intersection_token :
141156 objs = [obj for obj in objs if obj in _objs ]
142157
143158 return objs
@@ -150,24 +165,23 @@ async def finditer_async(
150165 matches = await self .path .finditer_async (data )
151166
152167 for op , path in self .paths :
153- assert op in ("|" , "&" )
154168 _matches = await path .finditer_async (data )
155- if op == "|" :
169+ if op == self . union_token :
156170 matches = _achain (matches , _matches )
157- elif op == "&" :
171+ elif op == self . intersection_token :
158172 _objs = [match .obj async for match in _matches ]
159173 _matches = (match async for match in matches if match .obj in _objs )
160174
161175 return matches
162176
163177 def union (self , path : JSONPath ) -> CompoundJSONPath :
164178 """In-place union of this path and another path."""
165- self .paths .append (("|" , path ))
179+ self .paths .append ((self . union_token , path ))
166180 return self
167181
168182 def intersection (self , path : JSONPath ) -> CompoundJSONPath :
169183 """In-place intersection of this path and another path."""
170- self .paths .append (("&" , path ))
184+ self .paths .append ((self . intersection_token , path ))
171185 return self
172186
173187
0 commit comments