-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2f53db
commit 995a203
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,44 @@ | ||
import importlib.util | ||
import sys | ||
from importlib.abc import MetaPathFinder | ||
from importlib.machinery import ModuleSpec | ||
from pathlib import Path | ||
from types import ModuleType | ||
from typing import Sequence | ||
|
||
from ome_types.model.ome_2016_06 import * # noqa | ||
|
||
# these are here mostly to make mypy happy in pre-commit even when the model isn't built | ||
from ome_types.model.ome_2016_06 import OME as OME | ||
from ome_types.model.ome_2016_06 import Reference as Reference | ||
|
||
_OME_2016 = Path(__file__).parent / "ome_2016_06" | ||
|
||
|
||
# add a sys.meta_path hook to allow import of any modules in | ||
# ome_types.model.ome_2016_06 | ||
class OMEMetaPathFinder(MetaPathFinder): | ||
def find_spec( | ||
self, | ||
fullname: str, | ||
path: Sequence[str] | None, | ||
target: ModuleType | None = None, | ||
) -> ModuleSpec | None: | ||
"""Return a module spec for any module in ome_types.model.ome_2016_06.""" | ||
if fullname.startswith("ome_types.model."): | ||
submodule = fullname.split(".", 2)[-1] | ||
file_2016 = (_OME_2016 / submodule.replace(".", "/")).with_suffix(".py") | ||
if file_2016.is_file(): | ||
import warnings | ||
|
||
warnings.warn( | ||
"Importing sumodules from ome_types.model is deprecated. " | ||
"Please import types directly from ome_types.model instead.", | ||
stacklevel=2, | ||
) | ||
module_2016 = fullname.replace(".model.", ".model.ome_2016_06.", 1) | ||
return importlib.util.spec_from_file_location(module_2016, file_2016) | ||
return None | ||
|
||
|
||
sys.meta_path.append(OMEMetaPathFinder()) |