Skip to content

rw

assert_file_exists(path)

Assert that a file exists.

Parameters:

Name Type Description Default
path str

The path to the file.

required

Returns:

Type Description
OneOf[Issue, Path]

A Good containing the Path if the file exists.

Source code in m/core/rw.py
def assert_file_exists(path: str) -> OneOf[Issue, Path]:
    """Assert that a file exists.

    Args:
        path: The path to the file.

    Returns:
        A `Good` containing the `Path` if the file exists.
    """
    path_inst = Path(path)
    if path_inst.exists():
        return Good(path_inst)
    return issue('file does not exist', context={'path': path})

insert_to_file(filename, start, text, end)

Insert content to a file.

Parameters:

Name Type Description Default
filename str

The file to insert to.

required
start str

The start delimiter where the insertion will take place.

required
text str

The main content to insert.

required
end str

The end delimiter.

required

Returns:

Type Description
OneOf[Issue, int]

None if successful, else an issue.

Source code in m/core/rw.py
def insert_to_file(
    filename: str,
    start: str,
    text: str,
    end: str,
) -> OneOf[Issue, int]:
    """Insert content to a file.

    Args:
        filename: The file to insert to.
        start: The start delimiter where the insertion will take place.
        text: The main content to insert.
        end: The end delimiter.

    Returns:
        None if successful, else an issue.
    """
    return one_of(lambda: [
        0
        for file_content in read_file(filename)
        for new_content in _insert_to_file(file_content, start, text, end)
        for _ in write_file(filename, new_content)
    ])

read_file(filename)

FP version of open to read the contents of a file.

If None is provided it will attempt to read from sys.stdin.

Parameters:

Name Type Description Default
filename str | None

The file path to read.

required

Returns:

Type Description
OneOf[Issue, str]

A Good containing the contents of the file.

Source code in m/core/rw.py
def read_file(filename: str | None) -> OneOf[Issue, str]:
    """FP version of open to read the contents of a file.

    If `None` is provided it will attempt to read from `sys.stdin`.

    Args:
        filename: The file path to read.

    Returns:
        A `Good` containing the contents of the file.
    """
    if filename is None:
        return Good(sys.stdin.read())
    try:
        with Path.open(Path(filename), encoding='UTF-8') as fp:
            return Good(fp.read())
    except Exception as ex:
        return issue(
            'failed to read file',
            context={'filename': filename},
            cause=ex,
        )

write_file(filename, text, mode='w')

Write contents to a file in a functional programming style.

Parameters:

Name Type Description Default
filename str

The file path where contents will be written.

required
text str

The contents of the file.

required
mode str

The mode to open the file in.

'w'

Returns:

Type Description
OneOf[Issue, int]

A Good containing 0 if the file was written.

Source code in m/core/rw.py
def write_file(filename: str, text: str, mode: str = 'w') -> OneOf[Issue, int]:
    """Write contents to a file in a functional programming style.

    Args:
        filename: The file path where contents will be written.
        text: The contents of the file.
        mode: The mode to open the file in.

    Returns:
        A `Good` containing 0 if the file was written.
    """
    try:
        with Path.open(Path(filename), mode, encoding='UTF-8') as fp:
            fp.write(text)
    except Exception as ex:
        return issue(
            'failed to write file',
            context={'filename': filename},
            cause=ex,
        )
    return Good(0)