Skip to content

Commit a443d2d

Browse files
committed
added a terminal CLI when -j option is json file
1 parent fd46939 commit a443d2d

2 files changed

Lines changed: 38 additions & 28 deletions

File tree

src/deltacode/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -348,21 +348,21 @@ def to_dict(self):
348348
def __str__(self):
349349

350350
if self.old_file:
351-
old_file_name = self.old_file.name
351+
old_file_path = self.old_file.original_path
352352
else:
353-
old_file_name = "null"
353+
old_file_path = "null"
354354

355355
if self.new_file:
356-
new_file_name = self.new_file.name
356+
new_file_path = self.new_file.original_path
357357
else:
358-
new_file_name = "null"
358+
new_file_path = "null"
359359

360360
return (" {\n"+
361-
" new file: " + new_file_name + "\n"+
362-
" old file: " + old_file_name + "\n" +
363-
" factors: [" + " ".join(self.factors) + "]\n" +
364361
" status: " + str(self.status) + "\n"
362+
" factors: [" + " ".join(self.factors) + "]\n" +
365363
" score: " + str(self.score) + "\n"+
364+
" new_file: " + new_file_path + "\n"+
365+
" old_file: " + old_file_path + "\n" +
366366
" }\n"
367367
)
368368

src/deltacode/cli.py

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,37 @@ def write_json(deltacode, outfile, all_delta_types=False):
5050
('deltas_count', len([d for d in deltas(deltacode, all_delta_types)])),
5151
('delta_stats', deltacode.stats.to_dict()),
5252
('deltas', deltas(deltacode, all_delta_types))
53-
])
54-
55-
if( outfile.name != '<stdout>'):
56-
57-
click.secho(get_notice_for_cli(),fg="green")
58-
click.echo('deltas_count: {}'.format(len([d for d in deltas(deltacode, all_delta_types)])))
59-
click.echo('delta_stats: ')
60-
for stat in deltacode.stats.to_dict():
61-
click.echo(""" "{}": {}""".format(str(stat),deltacode.stats.to_dict()[stat]))
62-
# TODO: add toggle for pretty printing
63-
64-
delta_count = 1
65-
for delta in deltacode.deltas :
66-
if delta.status != "unmodified":
67-
click.secho("Delta {}".format(delta_count),fg="blue")
68-
click.echo(delta)
69-
delta_count += 1
70-
click.secho("--------------------ALL DELTAS GENERATED--------------------",fg="yellow")
71-
72-
simplejson.dump(results, outfile, iterable_as_array=True, indent=2)
73-
outfile.write('\n')
53+
])
54+
55+
try:
56+
57+
simplejson.dump(results, outfile, iterable_as_array=True, indent=2)
58+
outfile.write('\n')
59+
if outfile.name != '<stdout>' :
60+
"This condition arises when we are required to print in the json file and the value of -j in not the console terminal"
61+
click.secho(get_notice_for_cli(),fg="green")
62+
click.echo('deltas_count: {}'.format(len([d for d in deltas(deltacode, all_delta_types)])))
63+
click.echo('delta_stats: ')
64+
for stat in deltacode.stats.to_dict():
65+
click.echo(""" "{}": {}""".format(str(stat),deltacode.stats.to_dict()[stat]))
66+
# TODO: add toggle for pretty printing
67+
68+
delta_count = 1
69+
for delta in deltacode.deltas :
70+
if delta.status != "unmodified":
71+
click.echo("\n")
72+
click.secho("delta {}".format(delta_count))
73+
click.echo(delta)
74+
delta_count += 1
75+
click.secho("#" * 211 + "\n",fg="yellow")
76+
click.secho(" "*70 + "All deltas are generated, view the json file for details" + "\n" , fg="green")
77+
click.secho("#" * 211,fg="yellow")
78+
79+
except Exception as e:
80+
"This exception arises when outfile has no name attribute"
81+
simplejson.dump(results, outfile, iterable_as_array=True, indent=2)
82+
outfile.write('\n')
83+
7484

7585
def print_version(ctx, param, value):
7686
if not value or ctx.resilient_parsing:

0 commit comments

Comments
 (0)