From 6142e853fa2cf1c1be695ca7cce121e48b177d7b Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Fri, 29 Jan 2021 18:50:12 -0300 Subject: Feat: change default and test params; use return status --- csv-hasher.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'csv-hasher.py') diff --git a/csv-hasher.py b/csv-hasher.py index c07adb0..e76c7b0 100755 --- a/csv-hasher.py +++ b/csv-hasher.py @@ -90,7 +90,7 @@ class CsvHasher: # Check the input file if nlines < 2: print('CSV file is too small.') - exit (1) + return False # Holds columns definition columns = None @@ -104,7 +104,7 @@ class CsvHasher: # Check for the column if self.args.colname[0] not in columns: print('Column not found: ' + self.args.colname[0]) - exit (1) + return False # Start with an empty file try: @@ -112,7 +112,7 @@ class CsvHasher: f.truncate(0) except IOError: print('Error writing to ' + outfile) - exit(1) + return False # Initialize progress bar progress_bar = tqdm(total=nlines) if self.args.progress else False @@ -136,7 +136,7 @@ class CsvHasher: df[self.args.colname[0]] = self.apply_hash(df) except KeyError as e: print('Column not found: ' + self.args.colname[0]) - exit (1) + return False # Writing the new CSV output df.to_csv(outfile, index=False, mode='a', header=write_header) @@ -173,7 +173,11 @@ def cmdline(): :return: Command line arguments. """ - basename = os.path.basename(__file__) + # Defaults + basename = os.path.basename(__file__) + chunksize = '1M' + hashfunc = 'sha256' + progress = True # Parse CLI #examples = "Examples:\n\t" + basename + " --no-progress \n" @@ -190,23 +194,23 @@ def cmdline(): parser.add_argument('--sep', dest='sep', help='Separator, defaults to ","') parser.add_argument('--chunksize', dest='chunksize', - help='Read chunks at a time, defaults to 1M, supports human-readable notation') + help='Read chunks at a time, supports human-readable notation, defaults to ' + chunksize) - parser.add_argument('--hashfunc', dest='hashfunc', help='Hash function, defaults do sha256') + parser.add_argument('--hashfunc', dest='hashfunc', help='Hash function, defaults do ' + hashfunc) parser.add_argument('--progress', dest='progress', action='store_true', - help='Enable progress bar.') + help='Enable progress bar, defaults to ' + str(progress)) parser.add_argument('--no-progress', dest='progress', action='store_false', help='Disable progress bar.') parser.add_argument('--check', dest='check', action='store_true', - help='Check both files for differences (test suite), defaults to false.') + help='Check both files for differences (test suite), defaults to ' + str(not progress)) # Add default values and get args parser.set_defaults(sep=',') - parser.set_defaults(chunksize='1M') - parser.set_defaults(hashfunc='sha256') + parser.set_defaults(chunksize=chunksize) + parser.set_defaults(hashfunc=hashfunc) parser.set_defaults(progress=True) parser.set_defaults(check=False) args = parser.parse_args() @@ -216,8 +220,10 @@ def cmdline(): if __name__ == "__main__": args = cmdline() instance = CsvHasher(args) + status = instance.run() - instance.run() + if status is False: + exit(1) if args.check == True: instance.check() -- cgit v1.2.3