Add util.uncomment() function

copied from fabric v1
This commit is contained in:
Lance Edgar 2020-02-07 11:29:53 -06:00
parent 88a08fcf8f
commit f584cf7e16

View file

@ -268,3 +268,38 @@ def sed(c, filename, before, after, limit='', use_sudo=False, backup='.bak',
return func(command,
# shell=shell
)
def uncomment(c, filename, regex, use_sudo=False, char='#', backup='.bak',
# shell=False,
):
"""
NOTE: This was copied from the upstream ``fabric.contrib.files`` (v1) module.
Attempt to uncomment all lines in ``filename`` matching ``regex``.
The default comment delimiter is `#` and may be overridden by the ``char``
argument.
This function uses the `sed` function, and will accept the same
``use_sudo``, ``shell`` and ``backup`` keyword arguments that `sed` does.
`uncomment` will remove a single whitespace character following the comment
character, if it exists, but will preserve all preceding whitespace. For
example, ``# foo`` would become ``foo`` (the single space is stripped) but
`` # foo`` would become `` foo`` (the single space is still stripped,
but the preceding 4 spaces are not.)
.. versionchanged:: 1.6
Added the ``shell`` keyword argument.
"""
return sed(
c,
filename,
before=r'^([[:space:]]*)%s[[:space:]]?' % char,
after=r'\1',
limit=regex,
use_sudo=use_sudo,
backup=backup,
# shell=shell
)