-
-
Notifications
You must be signed in to change notification settings - Fork 27
16 simplify DeltaCode output #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,64 +3,45 @@ | |
| # | ||
| from __future__ import absolute_import | ||
|
|
||
| from collections import OrderedDict | ||
|
|
||
| import csv | ||
| import json | ||
|
|
||
| import click | ||
|
|
||
| from deltacode import DeltaCode | ||
| from deltacode import __version__ | ||
|
|
||
|
|
||
| def generate_csv(data, result_file): | ||
| """ | ||
| Using the OrderedDict generated by DeltaCode.to_dict(), create a .csv file | ||
| containing the primary information from the Delta objects. | ||
| """ | ||
| category, new, new_filename, new_sha1, new_size, new_type, new_orig, old,\ | ||
| old_filename, old_sha1, old_size, old_type, old_orig = '', '', '', '',\ | ||
| '', '', '', '', '', '', '', '', '' | ||
| tuple = () | ||
| tuple_list = [] | ||
| deltas = data | ||
|
|
||
| for delta in deltas: | ||
| category = delta | ||
| for f in deltas[delta]: | ||
| new = '' if delta == 'removed' else f['new']['path'] | ||
| new_filename = '' if delta == 'removed' else f['new']['name'] | ||
| new_sha1 = '' if delta == 'removed' else f['new']['sha1'] | ||
| new_size = '' if delta == 'removed' else f['new']['size'] | ||
| new_type = '' if delta == 'removed' else f['new']['type'] | ||
| new_orig = '' if delta == 'removed' else f['new']['original_path'] | ||
| old = '' if delta == 'added' else f['old']['path'] | ||
| old_filename = '' if delta == 'added' else f['old']['name'] | ||
| old_sha1 = '' if delta == 'added' else f['old']['sha1'] | ||
| old_size = '' if delta == 'added' else f['old']['size'] | ||
| old_type = '' if delta == 'added' else f['old']['type'] | ||
| old_orig = '' if delta == 'added' else f['old']['original_path'] | ||
|
|
||
| tuple = (category, new, old, new_filename, old_filename, new_sha1, | ||
| old_sha1, new_size, old_size, new_type, old_type, new_orig, old_orig) | ||
| tuple_list.append(tuple) | ||
| tuple_list = [(f['category'], f['path']) for delta in deltas for f in deltas[delta]] | ||
|
|
||
| with open(result_file, 'wb') as out: | ||
| csv_out = csv.writer(out) | ||
| csv_out.writerow(['Type of delta', 'New scan path', 'Old scan path', | ||
| 'new_filename', 'old_filename', 'new_sha1', 'old_sha1', 'new_size', | ||
| 'old_size', 'new_type', 'old_type', 'new_original_path', 'old_original_path']) | ||
|
|
||
| csv_out.writerow(['Type of delta', 'Path']) | ||
| for row in tuple_list: | ||
| csv_out.writerow(row) | ||
|
|
||
|
|
||
| def generate_json(data, result_file): | ||
| def generate_json(delta, result_file): | ||
| """ | ||
| Using the OrderedDict generated by DeltaCode.to_dict(), create a .json file | ||
| Using the DeltaCode object, create a .json file | ||
| containing the primary information from the Delta objects. | ||
| """ | ||
| # TODO: Add json file headers here | ||
| json_dict = OrderedDict([ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, json_dict is a poor variable choice. Something like |
||
| ('deltacode_version', __version__), | ||
| ('deltacode_stats', delta.get_stats()), | ||
| ('deltas', delta.to_dict()) | ||
| ]) | ||
|
|
||
| with open(result_file, 'w') as outfile: | ||
| json.dump(data, outfile, indent=4) | ||
| json.dump(json_dict, outfile, indent=4) | ||
|
|
||
|
|
||
| @click.command() | ||
|
|
@@ -77,18 +58,16 @@ def cli(new, old, csv_file, json_file): | |
| .json file (-j or -json-file) at a user-designated location. If no file | ||
| option is selected, the JSON results are printed to the console. | ||
| """ | ||
|
|
||
| # do the delta | ||
| delta = DeltaCode(new, old) | ||
| data = delta.to_dict() | ||
|
|
||
|
|
||
| # output to csv | ||
| if csv_file: | ||
| generate_csv(data, csv_file) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We pass the whole delta object to |
||
| # generate JSON output | ||
| elif json_file: | ||
| generate_json(data, json_file) | ||
| generate_json(delta, json_file) | ||
| # print to stdout | ||
| else: | ||
| print(json.dumps(data, indent=4)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to change this to |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| Type of delta,New scan path,Old scan path,new_filename,old_filename,new_sha1,old_sha1,new_size,old_size,new_type,old_type,new_original_path,old_original_path | ||
| added,a/a5.py,,a5.py,,a0c7dab0c0f07cc8ef8fac7729359b6997ed50cd,,200,,file,,codebase_01_1_file_added/a/a5.py, | ||
| unmodified,a/a3.py,a/a3.py,a3.py,a3.py,fd5d3589c825f448546d7dcec36da3e567d35fe9,fd5d3589c825f448546d7dcec36da3e567d35fe9,200,200,file,file,codebase_01_1_file_added/a/a3.py,codebase_01/a/a3.py | ||
| unmodified,b/b4.py,b/b4.py,b4.py,b4.py,98c9e6bed78b1513c28e666016cb35a50708c36e,98c9e6bed78b1513c28e666016cb35a50708c36e,200,200,file,file,codebase_01_1_file_added/b/b4.py,codebase_01/b/b4.py | ||
| unmodified,a/a2.py,a/a2.py,a2.py,a2.py,310797523e47db8481aeb06f1634317285115091,310797523e47db8481aeb06f1634317285115091,200,200,file,file,codebase_01_1_file_added/a/a2.py,codebase_01/a/a2.py | ||
| unmodified,b/b2.py,b/b2.py,b2.py,b2.py,3340d86b1da9323067db8022f86dc97cfccee1d0,3340d86b1da9323067db8022f86dc97cfccee1d0,200,200,file,file,codebase_01_1_file_added/b/b2.py,codebase_01/b/b2.py | ||
| unmodified,b/b1.py,b/b1.py,b1.py,b1.py,70f6ce80985578b5104db0abc578cf5a05e78f4b,70f6ce80985578b5104db0abc578cf5a05e78f4b,200,200,file,file,codebase_01_1_file_added/b/b1.py,codebase_01/b/b1.py | ||
| unmodified,b/b3.py,b/b3.py,b3.py,b3.py,e49d4463662414bee5ad2d2e5c1fbd704f33b84e,e49d4463662414bee5ad2d2e5c1fbd704f33b84e,200,200,file,file,codebase_01_1_file_added/b/b3.py,codebase_01/b/b3.py | ||
| unmodified,a/a4.py,a/a4.py,a4.py,a4.py,6f71666c46446c29d3f45feef5419ae76fb86a5b,6f71666c46446c29d3f45feef5419ae76fb86a5b,200,200,file,file,codebase_01_1_file_added/a/a4.py,codebase_01/a/a4.py | ||
| unmodified,a/a1.py,a/a1.py,a1.py,a1.py,84b647771481d39dd3a53f6dc210c26abac37748,84b647771481d39dd3a53f6dc210c26abac37748,200,200,file,file,codebase_01_1_file_added/a/a1.py,codebase_01/a/a1.py | ||
| Type of delta,Path | ||
| added,a/a5.py | ||
| unmodified,a/a3.py | ||
| unmodified,b/b4.py | ||
| unmodified,a/a2.py | ||
| unmodified,b/b2.py | ||
| unmodified,b/b1.py | ||
| unmodified,b/b3.py | ||
| unmodified,a/a4.py | ||
| unmodified,a/a1.py |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| Type of delta,New scan path,Old scan path,new_filename,old_filename,new_sha1,old_sha1,new_size,old_size,new_type,old_type,new_original_path,old_original_path | ||
| modified,a/a4.py,a/a4.py,a4.py,a4.py,56aacf4e8ebcd7d8365e68cf11df933a237d33eb,6f71666c46446c29d3f45feef5419ae76fb86a5b,246,200,file,file,codebase_01_1_file_modified_not_renamed/a/a4.py,codebase_01/a/a4.py | ||
| unmodified,a/a3.py,a/a3.py,a3.py,a3.py,fd5d3589c825f448546d7dcec36da3e567d35fe9,fd5d3589c825f448546d7dcec36da3e567d35fe9,200,200,file,file,codebase_01_1_file_modified_not_renamed/a/a3.py,codebase_01/a/a3.py | ||
| unmodified,b/b4.py,b/b4.py,b4.py,b4.py,98c9e6bed78b1513c28e666016cb35a50708c36e,98c9e6bed78b1513c28e666016cb35a50708c36e,200,200,file,file,codebase_01_1_file_modified_not_renamed/b/b4.py,codebase_01/b/b4.py | ||
| unmodified,b/b1.py,b/b1.py,b1.py,b1.py,70f6ce80985578b5104db0abc578cf5a05e78f4b,70f6ce80985578b5104db0abc578cf5a05e78f4b,200,200,file,file,codebase_01_1_file_modified_not_renamed/b/b1.py,codebase_01/b/b1.py | ||
| unmodified,a/a2.py,a/a2.py,a2.py,a2.py,310797523e47db8481aeb06f1634317285115091,310797523e47db8481aeb06f1634317285115091,200,200,file,file,codebase_01_1_file_modified_not_renamed/a/a2.py,codebase_01/a/a2.py | ||
| unmodified,b/b2.py,b/b2.py,b2.py,b2.py,3340d86b1da9323067db8022f86dc97cfccee1d0,3340d86b1da9323067db8022f86dc97cfccee1d0,200,200,file,file,codebase_01_1_file_modified_not_renamed/b/b2.py,codebase_01/b/b2.py | ||
| unmodified,b/b3.py,b/b3.py,b3.py,b3.py,e49d4463662414bee5ad2d2e5c1fbd704f33b84e,e49d4463662414bee5ad2d2e5c1fbd704f33b84e,200,200,file,file,codebase_01_1_file_modified_not_renamed/b/b3.py,codebase_01/b/b3.py | ||
| unmodified,a/a1.py,a/a1.py,a1.py,a1.py,84b647771481d39dd3a53f6dc210c26abac37748,84b647771481d39dd3a53f6dc210c26abac37748,200,200,file,file,codebase_01_1_file_modified_not_renamed/a/a1.py,codebase_01/a/a1.py | ||
| Type of delta,Path | ||
| modified,a/a4.py | ||
| unmodified,a/a3.py | ||
| unmodified,b/b4.py | ||
| unmodified,b/b1.py | ||
| unmodified,a/a2.py | ||
| unmodified,b/b2.py | ||
| unmodified,b/b3.py | ||
| unmodified,a/a1.py |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| Type of delta,New scan path,Old scan path,new_filename,old_filename,new_sha1,old_sha1,new_size,old_size,new_type,old_type,new_original_path,old_original_path | ||
| removed,,a/a4.py,,a4.py,,6f71666c46446c29d3f45feef5419ae76fb86a5b,,200,,file,,codebase_01/a/a4.py | ||
| unmodified,a/a3.py,a/a3.py,a3.py,a3.py,fd5d3589c825f448546d7dcec36da3e567d35fe9,fd5d3589c825f448546d7dcec36da3e567d35fe9,200,200,file,file,codebase_01_1_file_removed/a/a3.py,codebase_01/a/a3.py | ||
| unmodified,b/b4.py,b/b4.py,b4.py,b4.py,98c9e6bed78b1513c28e666016cb35a50708c36e,98c9e6bed78b1513c28e666016cb35a50708c36e,200,200,file,file,codebase_01_1_file_removed/b/b4.py,codebase_01/b/b4.py | ||
| unmodified,b/b1.py,b/b1.py,b1.py,b1.py,70f6ce80985578b5104db0abc578cf5a05e78f4b,70f6ce80985578b5104db0abc578cf5a05e78f4b,200,200,file,file,codebase_01_1_file_removed/b/b1.py,codebase_01/b/b1.py | ||
| unmodified,a/a2.py,a/a2.py,a2.py,a2.py,310797523e47db8481aeb06f1634317285115091,310797523e47db8481aeb06f1634317285115091,200,200,file,file,codebase_01_1_file_removed/a/a2.py,codebase_01/a/a2.py | ||
| unmodified,b/b2.py,b/b2.py,b2.py,b2.py,3340d86b1da9323067db8022f86dc97cfccee1d0,3340d86b1da9323067db8022f86dc97cfccee1d0,200,200,file,file,codebase_01_1_file_removed/b/b2.py,codebase_01/b/b2.py | ||
| unmodified,b/b3.py,b/b3.py,b3.py,b3.py,e49d4463662414bee5ad2d2e5c1fbd704f33b84e,e49d4463662414bee5ad2d2e5c1fbd704f33b84e,200,200,file,file,codebase_01_1_file_removed/b/b3.py,codebase_01/b/b3.py | ||
| unmodified,a/a1.py,a/a1.py,a1.py,a1.py,84b647771481d39dd3a53f6dc210c26abac37748,84b647771481d39dd3a53f6dc210c26abac37748,200,200,file,file,codebase_01_1_file_removed/a/a1.py,codebase_01/a/a1.py | ||
| Type of delta,Path | ||
| removed,a/a4.py | ||
| unmodified,a/a3.py | ||
| unmodified,b/b4.py | ||
| unmodified,b/b1.py | ||
| unmodified,a/a2.py | ||
| unmodified,b/b2.py | ||
| unmodified,b/b3.py | ||
| unmodified,a/a1.py |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| Type of delta,New scan path,Old scan path,new_filename,old_filename,new_sha1,old_sha1,new_size,old_size,new_type,old_type,new_original_path,old_original_path | ||
| added,a/a4_renamed_not_modified.py,,a4_renamed_not_modified.py,,6f71666c46446c29d3f45feef5419ae76fb86a5b,,200,,file,,codebase_01_1_file_renamed_not_modified/a/a4_renamed_not_modified.py, | ||
| removed,,a/a4.py,,a4.py,,6f71666c46446c29d3f45feef5419ae76fb86a5b,,200,,file,,codebase_01/a/a4.py | ||
| unmodified,a/a3.py,a/a3.py,a3.py,a3.py,fd5d3589c825f448546d7dcec36da3e567d35fe9,fd5d3589c825f448546d7dcec36da3e567d35fe9,200,200,file,file,codebase_01_1_file_renamed_not_modified/a/a3.py,codebase_01/a/a3.py | ||
| unmodified,b/b4.py,b/b4.py,b4.py,b4.py,98c9e6bed78b1513c28e666016cb35a50708c36e,98c9e6bed78b1513c28e666016cb35a50708c36e,200,200,file,file,codebase_01_1_file_renamed_not_modified/b/b4.py,codebase_01/b/b4.py | ||
| unmodified,b/b1.py,b/b1.py,b1.py,b1.py,70f6ce80985578b5104db0abc578cf5a05e78f4b,70f6ce80985578b5104db0abc578cf5a05e78f4b,200,200,file,file,codebase_01_1_file_renamed_not_modified/b/b1.py,codebase_01/b/b1.py | ||
| unmodified,b/b2.py,b/b2.py,b2.py,b2.py,3340d86b1da9323067db8022f86dc97cfccee1d0,3340d86b1da9323067db8022f86dc97cfccee1d0,200,200,file,file,codebase_01_1_file_renamed_not_modified/b/b2.py,codebase_01/b/b2.py | ||
| unmodified,b/b3.py,b/b3.py,b3.py,b3.py,e49d4463662414bee5ad2d2e5c1fbd704f33b84e,e49d4463662414bee5ad2d2e5c1fbd704f33b84e,200,200,file,file,codebase_01_1_file_renamed_not_modified/b/b3.py,codebase_01/b/b3.py | ||
| unmodified,a/a1.py,a/a1.py,a1.py,a1.py,84b647771481d39dd3a53f6dc210c26abac37748,84b647771481d39dd3a53f6dc210c26abac37748,200,200,file,file,codebase_01_1_file_renamed_not_modified/a/a1.py,codebase_01/a/a1.py | ||
| unmodified,a/a2.py,a/a2.py,a2.py,a2.py,310797523e47db8481aeb06f1634317285115091,310797523e47db8481aeb06f1634317285115091,200,200,file,file,codebase_01_1_file_renamed_not_modified/a/a2.py,codebase_01/a/a2.py | ||
| Type of delta,Path | ||
| added,a/a4_renamed_not_modified.py | ||
| removed,a/a4.py | ||
| unmodified,a/a3.py | ||
| unmodified,b/b4.py | ||
| unmodified,b/b1.py | ||
| unmodified,b/b2.py | ||
| unmodified,b/b3.py | ||
| unmodified,a/a1.py | ||
| unmodified,a/a2.py |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tuple_listis a poor variable choice. Reading the list comprehension, we know that it is a list of tuples; no need to be redundant here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead, we can skip the varibles all together and just do the list comprehension on line 28
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and you should really use
.get()syntax when accessing dictionaries.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good points. 👍