Skip to content

Commit

Permalink
Export API and remove test code
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Aug 13, 2024
1 parent b1c2e46 commit 0699fca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
20 changes: 20 additions & 0 deletions src/jsonyx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"format_syntax_error",
"load",
"loads",
"patch",
"read",
"write",
]
Expand All @@ -30,6 +31,7 @@

from jsonyx._decoder import DuplicateKey, JSONSyntaxError, make_scanner
from jsonyx._encoder import make_encoder
from jsonyx._patcher import patcher
from jsonyx.allow import NOTHING

if TYPE_CHECKING:
Expand Down Expand Up @@ -368,6 +370,24 @@ def loads(
)


def patch(obj: Any, operations: dict[str, Any] | list[dict[str, Any]]) -> Any:
"""Patch a Python object with an operation or a list of operations.
:param obj: a Python object
:type obj: Any
:param operations: an operation or a list of operations
:type operations: dict[str, Any] | list[dict[str, Any]]
:return: the patched Python object
:rtype: Any
"""
root: list[Any] = [obj]
if isinstance(operations, dict):
operations = [operations]

patcher(root, operations)
return root[0]


# pylint: disable-next=R0913
def write( # noqa: PLR0913
obj: object,
Expand Down
37 changes: 5 additions & 32 deletions src/jsonyx/patch.py → src/jsonyx/_patcher.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# Copyright (C) 2024 Nice Zombies
"""JSON patcher."""
# TODO(Nice Zombies): add error messages
# TODO(Nice Zombies): export API
# TODO(Nice Zombies): raise JSONSyntaxError
# TODO(Nice Zombies): remove executable code
# TODO(Nice Zombies): update command line options
# TODO(Nice Zombies): write documentation
# TODO(Nice Zombies): write tests
from __future__ import annotations

__all__: list[str] = ["patch"]
__all__: list[str] = ["patcher"]

import re
from math import isinf
Expand All @@ -18,17 +16,9 @@
from sys import maxsize
from typing import TYPE_CHECKING, Any

from jsonyx import dump

if TYPE_CHECKING:
from collections.abc import Callable

input_json: Any = []
patch_json: dict[str, Any] | list[dict[str, Any]] = {
"op": "assert",
"expr": "!@",
"path": "$[0]"
}

_FLAGS: RegexFlag = VERBOSE | MULTILINE | DOTALL

Expand Down Expand Up @@ -349,25 +339,13 @@ def _traverser(


# pylint: disable-next=R0912
def patch( # noqa: C901, PLR0912
obj: Any, operations: dict[str, Any] | list[dict[str, Any]],
) -> Any:
"""Patch a Python object with an operation or a list of operations.
:param obj: a Python object
:type obj: Any
:param operations: an operation or a list of operations
:type operations: dict[str, Any] | list[dict[str, Any]]
:return: the patched Python object
:rtype: Any
"""
root: list[Any] = [obj]
def patcher( # noqa: C901, PLR0912
root: list[Any], operations: list[dict[str, Any]],
) -> None:
"""Patch a Python object with a list of operations."""
nodes: list[tuple[dict[Any, Any] | list[Any], int | slice | str]] = [
(root, 0),
]
if isinstance(operations, dict):
operations = [operations]

for operation in operations:
op: str = operation["op"]
path: str = operation["path"]
Expand Down Expand Up @@ -425,8 +403,3 @@ def patch( # noqa: C901, PLR0912
dict.update(target[key], value) # type: ignore
else:
raise ValueError

return root[0]


dump(patch(input_json, patch_json), indent=4)

0 comments on commit 0699fca

Please sign in to comment.