I/O - momapy

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

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`exceptions` | `list[Exception]` | Built-in mutable sequence. If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified. | `<dynamic>`

### `` 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`

    117
    118
    119
    120
    @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`

    111
    112
    113
    114
    115
    @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

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`exceptions` | `list[Exception]` | Built-in mutable sequence. If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified. | `<dynamic>`
`obj` | `Any | None` |  | `None`
`annotations` | `dict | None` |  | `None`
`notes` | `dict | None` |  | `None`
`ids` | `dict | None` |  | `None`
`file_path` | `str | PathLike | None` |  | `None`

### `` 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`

    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    @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

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`exceptions` | `list[Exception]` | Built-in mutable sequence. If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified. | `<dynamic>`
`obj` | `Any | None` |  | `None`
`file_path` | `str | PathLike | None` |  | `None`

### `` 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`

    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    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`

    12
    13
    14
    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`

    17
    18
    19
    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`

     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    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