Skip to content

I/O

momapy.io

Base classes and functions for reading and writing maps

Classes:

Name Description
IOResult

Base class for I/O results

Reader

Base class for readers

ReaderResult

Base class for reader results

Writer

Base class for writers

WriterResult

Base class for writer results

Functions:

Name Description
read

Read a map file and return a reader result using the given registered reader. If no reader is given, will check for an appropriate reader among the registered readers, using the check_file method of each reader. If there is more than one appropriate reader, will use the first one.

register_reader

Register a reader

register_writer

Register a writer

write

Write an object to a file and return a writer result using the given registered writer

IOResult dataclass

IOResult(exceptions: list[Exception] = list())

Base class for I/O results

Reader

Bases: ABC

Base class for readers

Methods:

Name Description
check_file

Return true if the given file is supported by the reader, false otherwise

read

Read a file and return a reader result using the reader

check_file classmethod

check_file(file_path: str | PathLike) -> bool

Return true if the given file is supported by the reader, false otherwise

Source code in src/momapy/io.py
@classmethod
def check_file(cls, file_path: str | os.PathLike) -> bool:
    """Return `true` if the given file is supported by the reader, `false` otherwise"""
    pass

read abstractmethod classmethod

read(file_path: str | PathLike, **options) -> ReaderResult

Read a file and return a reader result using the reader

Source code in src/momapy/io.py
@classmethod
@abc.abstractmethod
def read(cls, file_path: str | os.PathLike, **options) -> ReaderResult:
    """Read a file and return a reader result using the reader"""
    pass

ReaderResult dataclass

ReaderResult(exceptions: list[Exception] = list(), obj: Any | None = None, annotations: dict | None = None, notes: dict | None = None, ids: dict | None = None, file_path: str | PathLike | None = None)

Bases: IOResult

Base class for reader results

Writer

Bases: ABC

Base class for writers

Methods:

Name Description
write

Write an object to a file and return a writer result using the writer

write abstractmethod classmethod

write(obj: Any, file_path: str | PathLike, **options) -> WriterResult

Write an object to a file and return a writer result using the writer

Source code in src/momapy/io.py
@classmethod
@abc.abstractmethod
def write(
    cls,
    obj: typing.Any,
    file_path: str | os.PathLike,
    **options,
) -> WriterResult:
    """Write an object to a file and return a writer result using the writer"""
    pass

WriterResult dataclass

WriterResult(exceptions: list[Exception] = list(), obj: Any | None = None, file_path: str | PathLike | None = None)

Bases: IOResult

Base class for writer results

read

read(file_path: str | PathLike, reader: str | None = None, **options) -> ReaderResult

Read a map file and return a reader result using the given registered reader. If no reader is given, will check for an appropriate reader among the registered readers, using the check_file method of each reader. If there is more than one appropriate reader, will use the first one.

Parameters:

Name Type Description Default
file_path str | PathLike

The path of the file to read

required
reader str | None

The registered reader

None
options

Options to be passed to the reader

{}

Returns:

Type Description
ReaderResult

A reader result

Source code in src/momapy/io.py
def read(
    file_path: str | os.PathLike,
    reader: str | None = None,
    **options,
) -> ReaderResult:
    """Read a map file and return a reader result using the given registered reader. If no reader is given, will check for an appropriate reader among the registered readers, using the `check_file` method of each reader. If there is more than one appropriate reader, will use the first one.

    Args:
        file_path: The path of the file to read
        reader: The registered reader
        options: Options to be passed to the reader

    Returns:
        A reader result

    """
    reader_cls = None
    if reader is not None:
        reader_cls = readers.get(reader)
        if reader_cls is None:
            raise ValueError(f"no registered reader named '{reader}'")
    else:
        for candidate_reader_cls in readers.values():
            if candidate_reader_cls.check_file(file_path):
                reader_cls = candidate_reader_cls
                break
    if reader_cls is not None:
        result = reader_cls.read(file_path, **options)
    else:
        raise ValueError(
            f"could not find a suitable registered reader for file '{file_path}'"
        )
    return result

register_reader

register_reader(name: str, reader_cls: Type)

Register a reader

Source code in src/momapy/io.py
def register_reader(name: str, reader_cls: typing.Type):
    """Register a reader"""
    readers[name] = reader_cls

register_writer

register_writer(name, writer_cls)

Register a writer

Source code in src/momapy/io.py
def register_writer(name, writer_cls):
    """Register a writer"""
    writers[name] = writer_cls

write

write(obj: Any, file_path: str | PathLike, writer: str, **options) -> WriterResult

Write an object to a file and return a writer result using the given registered writer

Parameters:

Name Type Description Default
obj Any

The object to write

required
file_path str | PathLike

The path of the file to write to

required
writer str

The registered writer

required
options

Options to be passed to the writer

{}

Returns:

Type Description
WriterResult

A writer result

Source code in src/momapy/io.py
def write(
    obj: typing.Any,
    file_path: str | os.PathLike,
    writer: str,
    **options,
) -> WriterResult:
    """Write an object to a file and return a writer result using the given registered writer

    Args:
        obj: The object to write
        file_path: The path of the file to write to
        writer: The registered writer
        options: Options to be passed to the writer

    Returns:
        A writer result
    """
    writer_cls = None
    writer_cls = writers.get(writer)
    if writer_cls is None:
        raise ValueError(f"no registered writer named '{writer}'")
    result = writer_cls.write(obj, file_path, **options)
    return result