FileSystem¶
- class rmote.tools.fs.LineInFileMatch(*values)[source]¶
Bases:
IntEnumControls how regexp matches are replaced by
FileSystem.line_in_file().- FIRST¶
Replace only the first line that matches the regexp.
- ALL¶
Replace every line that matches the regexp.
- class rmote.tools.fs.FileSystem[source]¶
Bases:
ToolRemote filesystem operations - read, glob, and idempotent line-in-file.
- static line_in_file(path, *, line, regexp=None, strip=True, create=False, match=LineInFileMatch.FIRST)[source]¶
Ensure line is present in the file at path, idempotently.
If regexp is given, replace the first (or all, with
match=ALL) lines that match the pattern with line. If no line matches, or if regexp is not given and line is not found, line is appended.- Parameters:
path (
Text) – Path to the file to modify.line (
Text) – The desired line content to insert or substitute.regexp (
Text|None) – A regex pattern to match against existing lines. When a match is found, the matching line(s) are replaced with line.strip (
bool) – Compare lines after stripping whitespace when looking for an exact match (no regexp). DefaultTrue.create (
bool) – Create the file if it does not exist. DefaultFalse.match (
LineInFileMatch) – Whether to replace theFIRSTmatching line orALLmatching lines.
- Return type:
- Returns:
A unified diff string describing the change, or an empty string if the file was already in the desired state.
- Raises:
FileNotFoundError – If path does not exist and create is
False.