-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy path__init__.py
More file actions
101 lines (87 loc) · 3.44 KB
/
Copy path__init__.py
File metadata and controls
101 lines (87 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
openff-toolkit
A modern, extensible library for molecular mechanics force field science from the Open Force Field Consortium.
"""
import importlib
from importlib.metadata import version
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# These types are imported lazily at runtime, but we need to tell type
# checkers what they are
from openff.units import Quantity, unit
from openff.toolkit.topology import Molecule, Topology
from openff.toolkit.typing.engines.smirnoff import (
ForceField,
get_available_force_fields,
)
from openff.toolkit.utils.toolkits import (
GLOBAL_TOOLKIT_REGISTRY,
AmberToolsToolkitWrapper,
BuiltInToolkitWrapper,
NAGLToolkitWrapper,
OpenEyeToolkitWrapper,
RDKitToolkitWrapper,
ToolkitRegistry,
)
__version__ = version("openff.toolkit")
__all__ = [
"GLOBAL_TOOLKIT_REGISTRY",
"AmberToolsToolkitWrapper",
"BuiltInToolkitWrapper",
"ForceField",
"Molecule",
"OpenEyeToolkitWrapper",
"Quantity",
"RDKitToolkitWrapper",
"ToolkitRegistry",
"Topology",
"__version__",
"get_available_force_fields",
"unit",
]
# Dictionary of objects to lazily import; maps the object's name to its module path
_lazy_imports_obj = {
"ForceField": "openff.toolkit.typing.engines.smirnoff",
"get_available_force_fields": "openff.toolkit.typing.engines.smirnoff",
"Molecule": "openff.toolkit.topology",
"Topology": "openff.toolkit.topology",
"GLOBAL_TOOLKIT_REGISTRY": "openff.toolkit.utils.toolkits",
"AmberToolsToolkitWrapper": "openff.toolkit.utils.toolkits",
"BuiltInToolkitWrapper": "openff.toolkit.utils.toolkits",
"NAGLToolkitWrapper": "openff.toolkit.utils.toolkits",
"OpenEyeToolkitWrapper": "openff.toolkit.utils.toolkits",
"RDKitToolkitWrapper": "openff.toolkit.utils.toolkits",
"ToolkitRegistry": "openff.toolkit.utils.toolkits",
"Quantity": "openff.units",
"unit": "openff.units",
# Remember to add new lazy imports to __all__ and the if TYPE_CHECKING imports
}
# Dictionary of modules to lazily import; maps the modules's name to its path
_lazy_imports_mod = {
"topology": "openff.toolkit.topology",
"typing": "openff.toolkit.typing",
"utils": "openff.toolkit.utils",
"unit": "openff.units.units",
}
def __getattr__(name):
"""Lazily import objects from _lazy_imports_obj or _lazy_imports_mod
Note that this method is only called by Python if the name cannot be found
in the current module."""
obj_mod = _lazy_imports_obj.get(name)
if obj_mod is not None:
mod = importlib.import_module(obj_mod)
# This only causes an infinite recursive loop if an object that does not
# exist is declared in `_lazy_imports_obj` to be lazy loaded from the
# current module. Since there is no reason to include either an object
# from the current module or an object that does not exist in
# `_lazy_imports_obj`, and since the function would fail in this case
# anyway, this is safe.
return getattr(mod, name)
lazy_mod = _lazy_imports_mod.get(name)
if lazy_mod is not None:
return importlib.import_module(lazy_mod)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
def __dir__():
"""Add _lazy_imports_obj and _lazy_imports_mod to dir(<module>)"""
keys = (*globals().keys(), *_lazy_imports_obj.keys(), *_lazy_imports_mod.keys())
return sorted(keys)