Skip to content

Commit

Permalink
added a terminal CLI when -j option is json file
Browse files Browse the repository at this point in the history
Signed-off-by: Pratikrocks <[email protected]>
  • Loading branch information
Pratikrocks committed Apr 13, 2020
1 parent fd46939 commit d9973f8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
14 changes: 7 additions & 7 deletions src/deltacode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
52 changes: 31 additions & 21 deletions src/deltacode/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 != '<stdout>'):

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 != '<stdout>' :
"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:
Expand Down

0 comments on commit d9973f8

Please sign in to comment.