From d9973f82236a26c248ab5065078ab35e9e45943c Mon Sep 17 00:00:00 2001 From: Pratikrocks Date: Mon, 13 Apr 2020 20:07:32 +0530 Subject: [PATCH] added a terminal CLI when -j option is json file Signed-off-by: Pratikrocks --- src/deltacode/__init__.py | 14 +++++------ src/deltacode/cli.py | 52 +++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/deltacode/__init__.py b/src/deltacode/__init__.py index a42c9df9..de7243ab 100644 --- a/src/deltacode/__init__.py +++ b/src/deltacode/__init__.py @@ -348,21 +348,21 @@ def to_dict(self): def __str__(self): if self.old_file: - old_file_name = self.old_file.name + old_file_path = self.old_file.original_path else: - old_file_name = "null" + old_file_path = "null" if self.new_file: - new_file_name = self.new_file.name + new_file_path = self.new_file.original_path else: - new_file_name = "null" + new_file_path = "null" return (" {\n"+ - " new file: " + new_file_name + "\n"+ - " old file: " + old_file_name + "\n" + - " factors: [" + " ".join(self.factors) + "]\n" + " status: " + str(self.status) + "\n" + " factors: [" + " ".join(self.factors) + "]\n" + " score: " + str(self.score) + "\n"+ + " new_file: " + new_file_path + "\n"+ + " old_file: " + old_file_path + "\n" + " }\n" ) diff --git a/src/deltacode/cli.py b/src/deltacode/cli.py index 74cd90dd..ae05d3b6 100644 --- a/src/deltacode/cli.py +++ b/src/deltacode/cli.py @@ -50,27 +50,37 @@ def write_json(deltacode, outfile, all_delta_types=False): ('deltas_count', len([d for d in deltas(deltacode, all_delta_types)])), ('delta_stats', deltacode.stats.to_dict()), ('deltas', deltas(deltacode, all_delta_types)) - ]) - - if( outfile.name != ''): - - click.secho(get_notice_for_cli(),fg="green") - click.echo('deltas_count: {}'.format(len([d for d in deltas(deltacode, all_delta_types)]))) - click.echo('delta_stats: ') - for stat in deltacode.stats.to_dict(): - click.echo(""" "{}": {}""".format(str(stat),deltacode.stats.to_dict()[stat])) - # TODO: add toggle for pretty printing - - delta_count = 1 - for delta in deltacode.deltas : - if delta.status != "unmodified": - click.secho("Delta {}".format(delta_count),fg="blue") - click.echo(delta) - delta_count += 1 - click.secho("--------------------ALL DELTAS GENERATED--------------------",fg="yellow") - - simplejson.dump(results, outfile, iterable_as_array=True, indent=2) - outfile.write('\n') + ]) + + try: + + simplejson.dump(results, outfile, iterable_as_array=True, indent=2) + outfile.write('\n') + if outfile.name != '' : + "This condition arises when we are required to print in the json file and the value of -j in not the console terminal" + click.secho(get_notice_for_cli(),fg="green") + click.echo('deltas_count: {}'.format(len([d for d in deltas(deltacode, all_delta_types)]))) + click.echo('delta_stats: ') + for stat in deltacode.stats.to_dict(): + click.echo(""" "{}": {}""".format(str(stat),deltacode.stats.to_dict()[stat])) + # TODO: add toggle for pretty printing + + delta_count = 1 + for delta in deltacode.deltas : + if delta.status != "unmodified": + click.echo("\n") + click.secho("delta {}".format(delta_count)) + click.echo(delta) + delta_count += 1 + click.secho("#" * 211 + "\n",fg="yellow") + click.secho(" "*70 + "All deltas are generated, view the json file for details" + "\n" , fg="green") + click.secho("#" * 211,fg="yellow") + + except Exception as e: + "This exception arises when outfile has no name attribute" + simplejson.dump(results, outfile, iterable_as_array=True, indent=2) + outfile.write('\n') + def print_version(ctx, param, value): if not value or ctx.resilient_parsing: