Skip to content

Commit

Permalink
Exceptions instead of return codes, exit
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianGoeltz committed Jan 31, 2019
1 parent 3ddcf99 commit 2c93f6e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
9 changes: 3 additions & 6 deletions yccp/sweeps/parametersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .. import prelude as pl

import copy
import errno
import os
import os.path as osp

Expand Down Expand Up @@ -93,7 +94,7 @@ def setup_metadata(self, orig_file):
"transforms": [],
}

def write(self, filename, overwrite=False, failOnOverwrite=True):
def write(self, filename, overwrite=False):
"""
Dump data into filename.
"""
Expand All @@ -109,11 +110,7 @@ def write(self, filename, overwrite=False, failOnOverwrite=True):
log.error(
"File {} exists and overwrite was not set to True".format(
filename))
if failOnOverwrite:
exit()
else:
return 1
raise OSError(errno.EEXIST)
else:
with open(filename, "w") as f:
pl.dump(self.data, stream=f)
return 0
11 changes: 8 additions & 3 deletions yccp/sweeps/sweeps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python2
# encoding: utf-8

import errno
import inspect
import itertools as it
import logging
Expand Down Expand Up @@ -87,10 +88,14 @@ def dump(self,

if write_files:
log.info("Writing: {}".format(fn))
if 0 == ps.write(fn, overwrite=overwrite_files, failOnOverwrite=failOnOverwrite):
try:
ps.write(fn, overwrite=overwrite_files)
written_filenames.add(fn)
else:
overwritten_files.add(fn)
except OSError as e:
if e.errno == errno.EEXIST and not failOnOverwrite:
overwritten_files.add(fn)
else:
raise
else:
log.info("Would write: {}".format(fn))
written_filenames.add(fn)
Expand Down
5 changes: 2 additions & 3 deletions yccp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ def get_recursive(dct, path, default=None, sep="/"):
retval = retrieve_path(dct, path, sep, create=False)
if retval is None:
if default is None:
import sys
sys.exit("YCCP: Did not find {} in the given document. "
"Skip this by giving sensible (not None) default".format(path))
raise KeyError("YCCP: Did not find {} in the given document. "
"Skip this by giving sensible (not None) default".format(path))
retval = default
return retval

Expand Down

0 comments on commit 2c93f6e

Please sign in to comment.