Skip to content

Commit

Permalink
Add cli support
Browse files Browse the repository at this point in the history
  • Loading branch information
Agent-Hellboy committed Nov 17, 2023
1 parent 8629da0 commit 87c0405
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ General Info
- The value returned by the `trace` function is an array of paths, where each path is an array of dictionary keys.
- Because of that, the library can be used in a practical way by taking advantage of this format.
- In the example below we use the returned path to iterate over the dictionary keys and print the key value:

- you can use the library as `python3 -m trace_dkey --file=test.json --key=name`, test.json is
a json file containing name as key
.. code:: py
from trace_dkey import trace
l={'a':{'b':{'c':{'d':{'e':{'f':1}}}}}}
paths = trace(l,'f')
for path in paths:
dic = l
for key in path:
Expand Down
10 changes: 7 additions & 3 deletions src/trace_dkey/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import copy
from typing import Tuple, List, Any, Dict, Union


def __process_tuple(tuple_elts: List) -> Tuple:
ans = [elt.value for elt in tuple_elts]
return tuple(ans)
Expand Down Expand Up @@ -45,7 +44,7 @@ def __trace_key(dict_key: Any, dict_value: Any, key: Any, path: List[Union[str,
return paths


def trace(dictionary: Dict, key: Any) -> None:
def _trace(dictionary: Dict, key: Any) -> List[List]:
dict_expr = str(dictionary)
ast_obj = ast.parse(dict_expr, mode="eval")
paths = []
Expand All @@ -55,7 +54,12 @@ def trace(dictionary: Dict, key: Any) -> None:
dict_value = ast_obj.body.values[indx]

trace_res = __trace_key(dict_key, dict_value, key, [])

paths = [*paths, *trace_res]

return paths

def trace(dictionary: Dict, key: Any):
paths = _trace(dictionary,key)

return paths

4 changes: 4 additions & 0 deletions src/trace_dkey/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .cmd import cli

if __name__ == "__main__":
cli()
13 changes: 13 additions & 0 deletions src/trace_dkey/cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import click
import json
from trace_dkey import trace


@click.command()
@click.option("--file", "-f", required=True, help="Path to the JSON file")
@click.option("--key", "-k", required=False, help="Key to trace in the dictionary")
def cli(file: str, key: str):
with open(file) as file_obj:
dictionary = json.load(file_obj)

print(trace(dictionary, key))
28 changes: 28 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import sys

import pytest
from click.testing import CliRunner

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../src')))
from trace_dkey.cmd import cli



@pytest.fixture
def runner():
return CliRunner()


def test_trace_key(runner):
input_json = '{"name": "John Doe", "age": 30}'
expected_output = "[['name']]"

with runner.isolated_filesystem():
# Create a temporary test JSON file
with open("test.json", "w") as file:
file.write(input_json)

result = runner.invoke(cli, ["--file", "test.json", "--key", "name"])
assert result.exit_code == 0
assert result.output.strip() == expected_output

0 comments on commit 87c0405

Please sign in to comment.