Skip to content

AtomicOutput

This module creates the output file only if no errors occurred.

AtomicOutput()

Bases: MessageHandlerFile

Create output file only if no errors occurred.

abort(message)

Abort the operation with an error message and discard the output.

PARAMETER DESCRIPTION
message

The error message.

TYPE: str

capture_exceptions(code)

Run code and capture any exceptions as error messages.

Exceptions with empty messages are not reported as error messages but still mark the handler as failed.

PARAMETER DESCRIPTION
code

The function to run.

TYPE: Callable[[], None]

RETURNS DESCRIPTION
bool

True if code ran without exceptions, False otherwise.

close()

Close the output file.

discard()

Discard the output file.

error(message, file=None, position=None, position_end=None)

Report an error and mark current script as failed.

PARAMETER DESCRIPTION
message

The error message.

TYPE: str

file

The file where the error occurred.

TYPE: str | None DEFAULT: None

position

The position in the file where the error occurred.

TYPE: Any DEFAULT: None

position_end

The end position in the file where the error occurred.

TYPE: Any DEFAULT: None

error_from_exception(exception, omit_empty=False)

Print message from exception.

If the exception is a FilePositionException, the file and position information will be used.

PARAMETER DESCRIPTION
exception

The exception to print the message from.

TYPE: Exception

omit_empty

Whether to omit messages with empty text. Handler will still be marked as failed.

TYPE: bool DEFAULT: False

fail()

Mark the message handler as failed.

get_file()

Get the output file handle, opening the file if necessary.

RETURNS DESCRIPTION
IO[Any]

The output file handle.

get_filename()

Get the output filename.

RETURNS DESCRIPTION
str

The output filename.

RAISES DESCRIPTION
RuntimeError

If the output filename is not set.

has_failed()

Check if the message handler has failed (errors reported).

RETURNS DESCRIPTION
bool

True if the message handler has failed, False otherwise.

info(message, file=None, position=None, position_end=None)

Print informational message using current script.

PARAMETER DESCRIPTION
message

Informational message.

TYPE: str

file

The file where the message occurred.

TYPE: str | None DEFAULT: None

position

The position in the file where the message occurred.

TYPE: Any DEFAULT: None

position_end

The end position in the file where the message occurred.

TYPE: Any DEFAULT: None

is_ok()

Check if the message handler is ok (no errors reported).

RETURNS DESCRIPTION
bool

True if the message handler is ok, False otherwise.

reset()

Reset the message handler to ok.

run(code)

Run code that produces the output file. If an uncaught exception occurs, the output file is discarded.

PARAMETER DESCRIPTION
code

The function to call to produce the output file.

TYPE: Callable[[], None]

set_filename(filename, binary=False)

Set output filename.

PARAMETER DESCRIPTION
filename

The output filename.

TYPE: str

binary

Whether to open the file in binary mode.

TYPE: bool DEFAULT: False

RAISES DESCRIPTION
RuntimeError

If the output file is already open.

set_only_if_changed(value)

Set whether not to overwrite an existing file if it remains unchanged. This avoids unnecessary rebuilds.

RAISES DESCRIPTION
RuntimeError

If the output file is already open.

warning(message, file=None, position=None, position_end=None)

Print warning using current script.

PARAMETER DESCRIPTION
message

Warning message.

TYPE: str

file

The file where the warning occurred.

TYPE: str | None DEFAULT: None

position

The position in the file where the warning occurred.

TYPE: Any DEFAULT: None

position_end

The end position in the file where the warning occurred.

TYPE: Any DEFAULT: None