mirror of
https://github.com/kevingruesser/bootstrap-vz.git
synced 2025-08-22 09:50:37 +00:00
Simplify exception throwing in sed_i
This commit is contained in:
parent
f63d3c73aa
commit
d26ba8bea4
2 changed files with 8 additions and 15 deletions
|
@ -28,11 +28,7 @@ class TaskError(Exception):
|
|||
pass
|
||||
|
||||
|
||||
class NoMatchesError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class TooManyMatchesError(Exception):
|
||||
class UnexpectedNumMatchesError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Add table
Reference in a new issue