33#
44from __future__ import absolute_import
55
6+ from collections import OrderedDict
7+
68import csv
79import json
810
911import click
1012
1113from deltacode import DeltaCode
14+ from deltacode import __version__
1215
1316
14- def generate_csv (data , result_file ):
17+ def generate_csv (delta , result_file ):
1518 """
16- Using the OrderedDict generated by DeltaCode.to_dict() , create a .csv file
19+ Using the DeltaCode object , create a .csv file
1720 containing the primary information from the Delta objects.
1821 """
19- category , new , new_filename , new_sha1 , new_size , new_type , new_orig , old ,\
20- old_filename , old_sha1 , old_size , old_type , old_orig = '' , '' , '' , '' ,\
21- '' , '' , '' , '' , '' , '' , '' , '' , ''
22- tuple = ()
23- tuple_list = []
24- deltas = data
25-
26- for delta in deltas :
27- category = delta
28- for f in deltas [delta ]:
29- new = '' if delta == 'removed' else f ['new' ]['path' ]
30- new_filename = '' if delta == 'removed' else f ['new' ]['name' ]
31- new_sha1 = '' if delta == 'removed' else f ['new' ]['sha1' ]
32- new_size = '' if delta == 'removed' else f ['new' ]['size' ]
33- new_type = '' if delta == 'removed' else f ['new' ]['type' ]
34- new_orig = '' if delta == 'removed' else f ['new' ]['original_path' ]
35- old = '' if delta == 'added' else f ['old' ]['path' ]
36- old_filename = '' if delta == 'added' else f ['old' ]['name' ]
37- old_sha1 = '' if delta == 'added' else f ['old' ]['sha1' ]
38- old_size = '' if delta == 'added' else f ['old' ]['size' ]
39- old_type = '' if delta == 'added' else f ['old' ]['type' ]
40- old_orig = '' if delta == 'added' else f ['old' ]['original_path' ]
41-
42- tuple = (category , new , old , new_filename , old_filename , new_sha1 ,
43- old_sha1 , new_size , old_size , new_type , old_type , new_orig , old_orig )
44- tuple_list .append (tuple )
45-
4622 with open (result_file , 'wb' ) as out :
4723 csv_out = csv .writer (out )
48- csv_out .writerow (['Type of delta' , 'New scan path' , 'Old scan path' ,
49- 'new_filename' , 'old_filename' , 'new_sha1' , 'old_sha1' , 'new_size' ,
50- 'old_size' , 'new_type' , 'old_type' , 'new_original_path' , 'old_original_path' ])
51-
52- for row in tuple_list :
24+ csv_out .writerow (['Type of delta' , 'Path' ])
25+ for row in [(f .category , f .old_file .path if f .category == 'removed' else f .new_file .path ) for d in delta .deltas for f in delta .deltas .get (d )]:
5326 csv_out .writerow (row )
5427
5528
56- def generate_json (data , result_file ):
29+ def generate_json (delta , result_file ):
5730 """
58- Using the OrderedDict generated by DeltaCode.to_dict() , create a .json file
31+ Using the DeltaCode object , create a .json file
5932 containing the primary information from the Delta objects.
6033 """
61- # TODO: Add json file headers here
34+ output = OrderedDict ([
35+ ('deltacode_version' , __version__ ),
36+ ('deltacode_stats' , delta .get_stats ()),
37+ ('deltas' , delta .to_dict ())
38+ ])
39+
6240 with open (result_file , 'w' ) as outfile :
63- json .dump (data , outfile , indent = 4 )
41+ json .dump (output , outfile , indent = 4 )
6442
6543
6644@click .command ()
@@ -77,18 +55,15 @@ def cli(new, old, csv_file, json_file):
7755 .json file (-j or -json-file) at a user-designated location. If no file
7856 option is selected, the JSON results are printed to the console.
7957 """
80-
8158 # do the delta
8259 delta = DeltaCode (new , old )
83- data = delta .to_dict ()
84-
8560
8661 # output to csv
8762 if csv_file :
88- generate_csv (data , csv_file )
63+ generate_csv (delta , csv_file )
8964 # generate JSON output
9065 elif json_file :
91- generate_json (data , json_file )
66+ generate_json (delta , json_file )
9267 # print to stdout
9368 else :
94- print (json .dumps (data , indent = 4 ))
69+ print (json .dumps (delta . to_dict () ))
0 commit comments