Module importer_file

Function: importer_file

importer_file(module_path)

A context manager that manages adding ImporterFile to the sys.meta_path.

This context manager ensures that the resource is properly acquired and released.

Args:

module_path: Path to the folder where the module exist for import.

Returns:

None:

Example:

In this example the context manager is used to import a module from a known path.

from __future__ import annotations
from pathlib import Path
from ooodev.calc import CalcDoc
from ooodev.loader import Lo
from ooodev.uno_helper.importer import importer_file


def main():
    _ = Lo.load_office(connector=Lo.ConnectPipe())
    doc = None
    try:
        fnm = Path.cwd() / "mods"
        doc = CalcDoc.create_doc()

        with importer_file(fnm):
            import big_worker

        big_worker.work()

        print("Done")

    except Exception as e:
        print(f"Error: {e}")

    finally:
        if doc:
            doc.close()
        Lo.close_office()


if __name__ == "__main__":
    main()

In this example, a ImporterFile instance is automatically managed, ensuring proper cleanup.

class ImporterFile

class ooodev.uno_helper.importer.ImporterFile(module_path)[source]

Bases: MetaPathFinder, Loader

Parameters:

module_path (str) –

__init__(module_path)[source]
Parameters:

module_path (str) –

create_module(spec)[source]

Return a module to initialize and into which to load.

This method should raise ImportError if anything prevents it from creating a new module. It may return None to indicate that the spec should create the new module.

Parameters:

spec (ModuleSpec) –

exec_module(module)[source]
Parameters:

module (module) –

find_spec(fullname, path, target=None)[source]
Return type:

ModuleSpec | None

Parameters:
  • fullname (str) –

  • path (Sequence[str] | None) –

  • target (types.ModuleType | None) –

uri_to_path(file_uri)[source]

Converts a file URI to a regular file path.

Parameters:

file_uri (str) – The file URI to convert.

Returns:

The regular file path.

Return type:

str