Skip to content

Python JSONPath Version 2 #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 33 additions & 27 deletions jsonpath/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
from .filter import UNDEFINED
from .filter import VALUE_TYPE_EXPRESSIONS
from .filter import FilterExpression
from .filter import FilterQuery
from .filter import FunctionExtension
from .filter import InfixExpression
from .filter import Path
from .fluent_api import Query
from .function_extensions import ExpressionType
from .function_extensions import FilterFunction
Expand All @@ -40,8 +40,8 @@
from .path import JSONPath
from .stream import TokenStream
from .token import TOKEN_EOF
from .token import TOKEN_FAKE_ROOT
from .token import TOKEN_INTERSECTION
from .token import TOKEN_PSEUDO_ROOT
from .token import TOKEN_UNION
from .token import Token

Expand Down Expand Up @@ -92,8 +92,8 @@ class attributes `root_token`, `self_token` and `filter_context_token`.
## Class attributes

Attributes:
fake_root_token (str): The pattern used to select a "fake" root node, one level
above the real root node.
pseudo_root_token (str): The pattern used to select a "fake" root node, one
level above the real root node.
filter_context_token (str): The pattern used to select extra filter context
data. Defaults to `"_"`.
intersection_token (str): The pattern used as the intersection operator.
Expand All @@ -117,7 +117,7 @@ class attributes `root_token`, `self_token` and `filter_context_token`.

# These should be unescaped strings. `re.escape` will be called
# on them automatically when compiling lexer rules.
fake_root_token = "^"
pseudo_root_token = "^"
filter_context_token = "_"
intersection_token = "&"
key_token = "#"
Expand Down Expand Up @@ -180,46 +180,52 @@ def compile(self, path: str) -> Union[JSONPath, CompoundJSONPath]: # noqa: A003
"""
tokens = self.lexer.tokenize(path)
stream = TokenStream(tokens)
fake_root = stream.current.kind == TOKEN_FAKE_ROOT
pseudo_root = stream.current().kind == TOKEN_PSEUDO_ROOT
_path: Union[JSONPath, CompoundJSONPath] = JSONPath(
env=self, selectors=self.parser.parse(stream), fake_root=fake_root
env=self, segments=self.parser.parse(stream), pseudo_root=pseudo_root
)

if stream.current.kind != TOKEN_EOF:
# TODO: Optionally raise for trailing whitespace
stream.skip_whitespace()

# TODO: better!
if stream.current().kind != TOKEN_EOF:
_path = CompoundJSONPath(env=self, path=_path)
while stream.current.kind != TOKEN_EOF:
if stream.peek.kind == TOKEN_EOF:
while stream.current().kind != TOKEN_EOF:
if stream.peek().kind == TOKEN_EOF:
# trailing union or intersection
raise JSONPathSyntaxError(
f"expected a path after {stream.current.value!r}",
token=stream.current,
f"expected a path after {stream.current().value!r}",
token=stream.current(),
)

if stream.current.kind == TOKEN_UNION:
stream.next_token()
fake_root = stream.current.kind == TOKEN_FAKE_ROOT
if stream.current().kind == TOKEN_UNION:
stream.next()
stream.skip_whitespace()
pseudo_root = stream.current().kind == TOKEN_PSEUDO_ROOT
_path = _path.union(
JSONPath(
env=self,
selectors=self.parser.parse(stream),
fake_root=fake_root,
segments=self.parser.parse(stream),
pseudo_root=pseudo_root,
)
)
elif stream.current.kind == TOKEN_INTERSECTION:
stream.next_token()
fake_root = stream.current.kind == TOKEN_FAKE_ROOT
elif stream.current().kind == TOKEN_INTERSECTION:
stream.next()
stream.skip_whitespace()
pseudo_root = stream.current().kind == TOKEN_PSEUDO_ROOT
_path = _path.intersection(
JSONPath(
env=self,
selectors=self.parser.parse(stream),
fake_root=fake_root,
segments=self.parser.parse(stream),
pseudo_root=pseudo_root,
)
)
else: # pragma: no cover
# Parser.parse catches this too
raise JSONPathSyntaxError( # noqa: TRY003
f"unexpected token {stream.current.value!r}",
token=stream.current,
f"unexpected token {stream.current().value!r}",
token=stream.current(),
)

return _path
Expand Down Expand Up @@ -456,21 +462,21 @@ def check_well_typedness(
if typ == ExpressionType.VALUE:
if not (
isinstance(arg, VALUE_TYPE_EXPRESSIONS)
or (isinstance(arg, Path) and arg.path.singular_query())
or (isinstance(arg, FilterQuery) and arg.path.singular_query())
or (self._function_return_type(arg) == ExpressionType.VALUE)
):
raise JSONPathTypeError(
f"{token.value}() argument {idx} must be of ValueType",
token=token,
)
elif typ == ExpressionType.LOGICAL:
if not isinstance(arg, (Path, InfixExpression)):
if not isinstance(arg, (FilterQuery, InfixExpression)):
raise JSONPathTypeError(
f"{token.value}() argument {idx} must be of LogicalType",
token=token,
)
elif typ == ExpressionType.NODES and not (
isinstance(arg, Path)
isinstance(arg, FilterQuery)
or self._function_return_type(arg) == ExpressionType.NODES
):
raise JSONPathTypeError(
Expand Down
22 changes: 9 additions & 13 deletions jsonpath/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from .function_extensions import FilterFunction
from .match import NodeList
from .selectors import Filter as FilterSelector
from .selectors import ListSelector
from .serialize import canonical_string

if TYPE_CHECKING:
Expand Down Expand Up @@ -494,7 +493,7 @@ def set_children(self, children: List[FilterExpression]) -> None:
self._expr.set_children(children)


class Path(FilterExpression, ABC):
class FilterQuery(FilterExpression, ABC):
"""Base expression for all _sub paths_ found in filter expressions."""

__slots__ = ("path",)
Expand All @@ -504,25 +503,22 @@ def __init__(self, path: JSONPath) -> None:
super().__init__()

def __eq__(self, other: object) -> bool:
return isinstance(other, Path) and str(self) == str(other)
return isinstance(other, FilterQuery) and str(self) == str(other)

def children(self) -> List[FilterExpression]:
_children: List[FilterExpression] = []
for segment in self.path.selectors:
if isinstance(segment, ListSelector):
_children.extend(
selector.expression
for selector in segment.items
if isinstance(selector, FilterSelector)
)
for segment in self.path.segments:
for selector in segment.selectors:
if isinstance(selector, FilterSelector):
_children.append(selector.expression)
return _children

def set_children(self, children: List[FilterExpression]) -> None: # noqa: ARG002
# self.path has its own cache
return


class SelfPath(Path):
class RelativeFilterQuery(FilterQuery):
"""A JSONPath starting at the current node."""

__slots__ = ()
Expand Down Expand Up @@ -561,7 +557,7 @@ async def evaluate_async(self, context: FilterContext) -> object:
)


class RootPath(Path):
class RootFilterQuery(FilterQuery):
"""A JSONPath starting at the root node."""

__slots__ = ()
Expand All @@ -584,7 +580,7 @@ async def evaluate_async(self, context: FilterContext) -> object:
)


class FilterContextPath(Path):
class FilterContextPath(FilterQuery):
"""A JSONPath starting at the root of any extra context data."""

__slots__ = ()
Expand Down
Loading
Loading