Class BreakContext

BreakContext is a context manager the is designed to wrap other context managers. If any error other than BreakContext.Break is raised then BreakContext will re-raise the error.

If BreakContext.Break is raised then then the inner context manager exits and no error is raised. This gives a clean way of exiting a context manager on purpose when certain conditions are not met.

In this example BreakContext wraps Lo.Loader context manager. If there is a error opening document then BreakContext.Break is raised. Lo.Loader terminates the office connection and BreakContext context manager exits quietly.

with BreakContext(Lo.Loader(Lo.ConnectSocket(headless=True))) as loader:

    fnm = cast(str, args.file_path)

        doc = Lo.open_doc(fnm=fnm, loader=loader)
    except Exception:
        print(f"Could not open '{fnm}'")
        raise BreakContext.Break
class ooodev.wrapper.break_context.BreakContext(value)[source]

Generic Context manager that allow for BreakContext. Break statement to exit with block.


with BreakContext(open(path)) as f:
    print 'before condition'
    if condition:
        raise BreakContext.Break
    print 'after condition'
exception Break[source]

Break out of the with statement