Skip to content

Commit

Permalink
Merge pull request #12 from jyejare/finder_submodule
Browse files Browse the repository at this point in the history
Finder/Reader to read the specific path from json file
  • Loading branch information
jyejare authored Mar 11, 2024
2 parents 142e3fb + 8b15a16 commit ba13675
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
8 changes: 8 additions & 0 deletions candore/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import asyncio # noqa: F401
import json
from pathlib import Path
from pprint import pprint

import click

from candore.errors import ModeError
from candore.modules.api_lister import APILister
from candore.modules.comparator import Comparator
from candore.modules.extractor import Extractor
from candore.modules.finder import Finder
from candore.modules.report import Reporting


Expand Down Expand Up @@ -58,3 +60,9 @@ def compare_entities(
results = comp.compare_json(pre_file=pre_file, post_file=post_file)
reporter = Reporting(results=results)
reporter.generate_report(output_file=output, output_type=report_type)

def find_path(self, path, json_file, delimiter):
finder = Finder()
data = finder.find(path=path, json_file=json_file, delimiter=delimiter)
if data:
pprint(data)
18 changes: 18 additions & 0 deletions candore/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,23 @@ def compare(ctx, pre, post, output, report_type, record_evs):
)


@candore.command(help="JSON Reader for reading the specific path data from entities data file")
@click.option(
"--path",
type=str,
help="The path to search the data from.\n"
"Path contents could divided by some delimiter.\n"
"e.g entity/5/description",
)
@click.option(
"--data-file", type=str, help="The data file from which to search the data on a given path"
)
@click.option("--delimiter", type=str, default='/', help="Settings file path. Default is '/'")
@click.pass_context
def reader(ctx, path, data_file, delimiter):
candore_obj = ctx.parent.candore
candore_obj.find_path(path=path, json_file=data_file, delimiter=delimiter)


if __name__ == "__main__":
candore()
41 changes: 41 additions & 0 deletions candore/modules/finder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import json


class Finder:
def read_json(self, jsonfile=None):
jdata = None
with open(jsonfile) as file:
jdata = json.load(file)
return jdata

def json_iterator(self, data=None, path=None, delimiter=None):
if not data:
return data
if path:
path_contents = path.strip().split(delimiter)
first_path = path_contents[0]
remaining_path = f'{delimiter}'.join(path_contents[1:])

if isinstance(data, dict):
if first_path in data:
return self.json_iterator(data.get(first_path, None), remaining_path, delimiter)
else:
print(
f'Oh!O! {first_path} is not found in given path, please correct the path!'
)
elif isinstance(data, list):
for index, element_data in enumerate(data):
if isinstance(element_data, dict):
if 'id' in element_data and str(element_data['id']) == first_path:
return self.json_iterator(element_data, remaining_path, delimiter)
elif isinstance(element_data, list):
if first_path in element_data:
return self.json_iterator(
element_data[index], remaining_path, delimiter
)
else:
return data

def find(self, path=None, json_file=None, delimiter='/'):
jdata = self.read_json(json_file)
return self.json_iterator(jdata, path=path, delimiter=delimiter)

0 comments on commit ba13675

Please sign in to comment.