Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
Catch CSV error gracefully and provide user message. Close #243.
Browse files Browse the repository at this point in the history
  • Loading branch information
chambridge committed Aug 31, 2017
1 parent 6211ff8 commit 1b43d45
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions rho/facthashcommand.py
Expand Up @@ -101,23 +101,29 @@ def read_and_hash(self, path):
keys = None

with open(path, 'r') as read_file:
reader = csv.DictReader(read_file, delimiter=',')
for row in reader:
for fact in self.facts_to_hash:
if fact in row:
row[fact] = compute_sha256_hash(row[fact])
facts_hashed.add(fact)
else:
facts_not_found.add(fact)
if keys is None:
keys = set(row.keys())
data.append(row)

for fact in facts_hashed:
print(_("Fact %s hashed" % fact))
for fact in facts_not_found:
print(_("Fact %s was not present in %s" %
(fact, self.options.report_path)))
try:
reader = csv.DictReader(read_file, delimiter=',')
for row in reader:
for fact in self.facts_to_hash:
if fact in row:
row[fact] = compute_sha256_hash(row[fact])
facts_hashed.add(fact)
else:
facts_not_found.add(fact)
if keys is None:
keys = set(row.keys())
data.append(row)

for fact in facts_hashed:
print(_("Fact %s hashed" % fact))
for fact in facts_not_found:
print(_("Fact %s was not present in %s" %
(fact, self.options.report_path)))
except csv.Error as csv_error:
print(_("An error occurred while attempting"
" to read CSV file %s." % (self.options.report_path)))
print(csv_error.message)
sys.exit(1)

return keys, data

Expand Down

0 comments on commit 1b43d45

Please sign in to comment.