Simplify exception throwing in sed_i

This commit is contained in:
Anders Ingemann 2015-03-05 17:15:13 +01:00 committed by Anders Ingemann
parent f63d3c73aa
commit d26ba8bea4
2 changed files with 8 additions and 15 deletions

View file

@ -28,11 +28,7 @@ class TaskError(Exception):
pass
class NoMatchesError(Exception):
pass
class TooManyMatchesError(Exception):
class UnexpectedNumMatchesError(Exception):
pass

View file

@ -63,16 +63,13 @@ def log_call(command, stdin=None, env=None, shell=False, cwd=None):
def sed_i(file_path, pattern, subst, expected_replacements=1):
replacement_count = inline_replace(file_path, pattern, subst)
if replacement_count < expected_replacements:
from exceptions import NoMatchesError
msg = ('There were no matches for the expression `{exp}\' in the file `{path}\''
.format(exp=pattern, path=file_path))
raise NoMatchesError(msg)
if replacement_count > expected_replacements:
from exceptions import TooManyMatchesError
msg = ('There were too many matches for the expression `{exp}\' in the file `{path}\''
.format(exp=pattern, path=file_path))
raise TooManyMatchesError(msg)
if replacement_count != expected_replacements:
from exceptions import UnexpectedNumMatchesError
msg = ('There were {real} instead of {expected} matches for '
'the expression `{exp}\' in the file `{path}\''
.format(real=replacement_count, expected=expected_replacements,
exp=pattern, path=file_path))
raise UnexpectedNumMatchesError(msg)
def inline_replace(file_path, pattern, subst):