FileSystem

class rmote.tools.fs.LineInFileMatch(*values)[source]

Bases: IntEnum

Controls 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: Tool

Remote filesystem operations - read, glob, and idempotent line-in-file.

static glob(path, pattern)[source]

Return all paths under path that match pattern.

Parameters:
  • path (Text | Path) – Directory to search in.

  • pattern (Text) – Glob pattern relative to path (e.g. "*.conf").

Return type:

list[Text]

Returns:

Sorted list of matching paths as strings.

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). Default True.

  • create (bool) – Create the file if it does not exist. Default False.

  • match (LineInFileMatch) – Whether to replace the FIRST matching line or ALL matching lines.

Return type:

Text

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.

static read_bytes(path)[source]

Read path and return its raw contents.

Parameters:

path (Text) – Absolute or relative path on the remote filesystem.

Return type:

bytes

Returns:

File contents as bytes.

static read_str(path)[source]

Read path and return its contents decoded as UTF-8.

Parameters:

path (Text) – Absolute or relative path on the remote filesystem.

Return type:

Text

Returns:

File contents as str.