mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
salt.modules.file: Improve docstrings
This also changes a few exceptions to match common usage (passive voice instead of imperative).
This commit is contained in:
parent
54b9641330
commit
650cc0af5c
1 changed files with 68 additions and 49 deletions
|
@ -1656,21 +1656,28 @@ def replace(path,
|
|||
'''
|
||||
.. versionadded:: 0.17.0
|
||||
|
||||
Replace occurrences of a pattern in a file
|
||||
Replace occurrences of a pattern in a file. If ``show_changes`` is
|
||||
``True``, then a diff of what changed will be returned, otherwise a
|
||||
``True`` will be returnd when changes are made, and ``False`` when
|
||||
no changes are made.
|
||||
|
||||
This is a pure Python implementation that wraps Python's :py:func:`~re.sub`.
|
||||
|
||||
path
|
||||
Filesystem path to the file to be edited
|
||||
|
||||
pattern
|
||||
Python's regular expression search
|
||||
https://docs.python.org/2/library/re.html
|
||||
A regular expression, to be matched using Python's
|
||||
:py:func:`~re.search`.
|
||||
|
||||
repl
|
||||
The replacement text
|
||||
count
|
||||
Maximum number of pattern occurrences to be replaced. Defaults to 0.
|
||||
If count is a positive integer n, only n occurrences will be replaced,
|
||||
|
||||
count : 0
|
||||
Maximum number of pattern occurrences to be replaced. If count is a
|
||||
positive integer ``n``, only ``n`` occurrences will be replaced,
|
||||
otherwise all occurrences will be replaced.
|
||||
|
||||
flags (list or int)
|
||||
A list of flags defined in the :ref:`re module documentation
|
||||
<contents-of-module-re>`. Each list item should be a string that will
|
||||
|
@ -1678,65 +1685,71 @@ def replace(path,
|
|||
'MULTILINE']``. Optionally, ``flags`` may be an int, with a value
|
||||
corresponding to the XOR (``|``) of all the desired flags. Defaults to
|
||||
8 (which supports 'MULTILINE').
|
||||
|
||||
bufsize (int or str)
|
||||
How much of the file to buffer into memory at once. The
|
||||
default value ``1`` processes one line at a time. The special value
|
||||
``file`` may be specified which will read the entire file into memory
|
||||
before processing.
|
||||
append_if_not_found
|
||||
|
||||
append_if_not_found : False
|
||||
.. versionadded:: 2014.7.0
|
||||
|
||||
If pattern is not found and set to ``True``
|
||||
then, the content will be appended to the file.
|
||||
Default is ``False``
|
||||
prepend_if_not_found
|
||||
If set to ``True``, and pattern is not found, then the content will be
|
||||
appended to the file.
|
||||
|
||||
prepend_if_not_found : False
|
||||
.. versionadded:: 2014.7.0
|
||||
|
||||
If pattern is not found and set to ``True``
|
||||
then, the content will be appended to the file.
|
||||
Default is ``False``
|
||||
If set to ``True`` and pattern is not found, then the content will be
|
||||
prepended to the file.
|
||||
|
||||
not_found_content
|
||||
.. versionadded:: 2014.7.0
|
||||
|
||||
Content to use for append/prepend if not found. If
|
||||
None (default), uses ``repl``. Useful when ``repl`` uses references to group in
|
||||
pattern.
|
||||
backup
|
||||
The file extension to use for a backup of the file before
|
||||
editing. Set to ``False`` to skip making a backup. Default
|
||||
is ``.bak``
|
||||
dry_run
|
||||
Don't make any edits to the file, Default is ``False``
|
||||
search_only
|
||||
Just search for the pattern; ignore the replacement;
|
||||
stop on the first match. Default is ``False``
|
||||
show_changes
|
||||
Output a unified diff of the old file and the new
|
||||
file. If ``False`` return a boolean if any changes were made.
|
||||
Default is ``True``
|
||||
Content to use for append/prepend if not found. If None (default), uses
|
||||
``repl``. Useful when ``repl`` uses references to group in pattern.
|
||||
|
||||
backup : .bak
|
||||
The file extension to use for a backup of the file before editing. Set
|
||||
to ``False`` to skip making a backup.
|
||||
|
||||
dry_run : False
|
||||
If set to ``True``, no changes will be made to the file, the function
|
||||
will just return the changes that would have been made (or a
|
||||
``True``/``False`` value if ``show_changes`` is set to ``False``).
|
||||
|
||||
search_only : False
|
||||
If set to true, this no changes will be perfomed on the file, and this
|
||||
function will simply return ``True`` if the pattern was matched, and
|
||||
``False`` if not.
|
||||
|
||||
show_changes : True
|
||||
If ``True``, return a diff of changes made. Otherwise, return ``True``
|
||||
if changes were made, and ``False`` if not.
|
||||
|
||||
.. note::
|
||||
Using this option will store two copies of the file in memory (the
|
||||
original version and the edited version) in order to generate the
|
||||
diff. This may not normally be a concern, but could impact
|
||||
performance if used with large files.
|
||||
|
||||
Using this option will store two copies of the file in-memory
|
||||
(the original version and the edited version) in order to generate the
|
||||
diff.
|
||||
ignore_if_missing
|
||||
ignore_if_missing : False
|
||||
.. versionadded:: 2015.8.0
|
||||
|
||||
When this parameter is ``True``, ``file.replace`` will return ``False`` if the
|
||||
file doesn't exist. When this parameter is ``False``, ``file.replace`` will
|
||||
throw an error if the file doesn't exist.
|
||||
Default is ``False`` (to maintain compatibility with prior behaviour).
|
||||
preserve_inode
|
||||
If set to ``True``, this function will simply return ``False``
|
||||
if the file doesn't exist. Otherwise, an error will be thrown.
|
||||
|
||||
preserve_inode : True
|
||||
.. versionadded:: 2015.8.0
|
||||
|
||||
Preserve the inode of the file, so that any hard links continue to share the
|
||||
inode with the original filename. This works by *copying* the file, reading
|
||||
from the copy, and writing to the file at the original inode. If ``False``, the
|
||||
file will be *moved* rather than copied, and a new file will be written to a
|
||||
new inode, but using the original filename. Hard links will then share an inode
|
||||
with the backup, instead (if using ``backup`` to create a backup copy).
|
||||
Default is ``True``.
|
||||
Preserve the inode of the file, so that any hard links continue to
|
||||
share the inode with the original filename. This works by *copying* the
|
||||
file, reading from the copy, and writing to the file at the original
|
||||
inode. If ``False``, the file will be *moved* rather than copied, and a
|
||||
new file will be written to a new inode, but using the original
|
||||
filename. Hard links will then share an inode with the backup, instead
|
||||
(if using ``backup`` to create a backup copy).
|
||||
|
||||
If an equal sign (``=``) appears in an argument to a Salt command it is
|
||||
interpreted as a keyword argument in the format ``key=val``. That
|
||||
|
@ -1776,10 +1789,14 @@ def replace(path,
|
|||
)
|
||||
|
||||
if search_only and (append_if_not_found or prepend_if_not_found):
|
||||
raise SaltInvocationError('Choose between search_only and append/prepend_if_not_found')
|
||||
raise SaltInvocationError(
|
||||
'search_only cannot be used with append/prepend_if_not_found'
|
||||
)
|
||||
|
||||
if append_if_not_found and prepend_if_not_found:
|
||||
raise SaltInvocationError('Choose between append or prepend_if_not_found')
|
||||
raise SaltInvocationError(
|
||||
'Only one of append and prepend_if_not_found is permitted'
|
||||
)
|
||||
|
||||
flags_num = _get_flags(flags)
|
||||
cpattern = re.compile(str(pattern), flags_num)
|
||||
|
@ -2056,7 +2073,9 @@ def blockreplace(path,
|
|||
raise SaltInvocationError('File not found: {0}'.format(path))
|
||||
|
||||
if append_if_not_found and prepend_if_not_found:
|
||||
raise SaltInvocationError('Choose between append or prepend_if_not_found')
|
||||
raise SaltInvocationError(
|
||||
'Only one of append and prepend_if_not_found is permitted'
|
||||
)
|
||||
|
||||
if not salt.utils.istextfile(path):
|
||||
raise SaltInvocationError(
|
||||
|
|
Loading…
Add table
Reference in a new issue