Skip to content

Commit

Permalink
Use @ml-evs's trick with adapter dependencies
Browse files Browse the repository at this point in the history
If an adapter dependency is not installed, a new type will be created
with the same name.
This is done for the sake of the docs.

In order to still warn and return about the non-existent dependency,
when trying to convert an OPTIMADE entry resource, the string
"optimade.adapters" is searched for in the representation of the "new"
type created.
This is because the new type will show up as belonging to the file in
which is was created.
This depends on a dependency never coming from within
"optimade.adapters", which makes sense, since it will always be external
libraries and packages.
  • Loading branch information
CasperWA committed Jul 31, 2020
1 parent 7285888 commit 0561f7b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 19 deletions.
4 changes: 2 additions & 2 deletions optimade/adapters/structures/aiida.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
except (ImportError, ModuleNotFoundError):
from warnings import warn

StructureData = None
StructureData = type("StructureData", (), {})
AIIDA_NOT_FOUND = (
"AiiDA not found, cannot convert structure to an AiiDA StructureData"
)
Expand All @@ -35,7 +35,7 @@ def get_aiida_structure_data(optimade_structure: OptimadeStructure) -> Structure
AiiDA `StructureData` Node.
"""
if globals().get("StructureData", None) is None:
if "optimade.adapters" in repr(globals().get("StructureData")):
warn(AIIDA_NOT_FOUND)
return None

Expand Down
4 changes: 2 additions & 2 deletions optimade/adapters/structures/ase.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
try:
from ase import Atoms, Atom
except (ImportError, ModuleNotFoundError):
Atoms = None
Atoms = type("Atoms", (), {})
ASE_NOT_FOUND = "ASE not found, cannot convert structure to an ASE Atoms"


Expand All @@ -39,7 +39,7 @@ def get_ase_atoms(optimade_structure: OptimadeStructure) -> Atoms:
ASE `Atoms` object.
"""
if globals().get("Atoms", None) is None:
if "optimade.adapters" in repr(globals().get("Atoms")):
warn(ASE_NOT_FOUND)
return None

Expand Down
4 changes: 2 additions & 2 deletions optimade/adapters/structures/cif.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
except ImportError:
from warnings import warn

np = None
np = type("np", (), {})
NUMPY_NOT_FOUND = "NumPy not found, cannot convert structure to CIF"


Expand All @@ -51,7 +51,7 @@ def get_cif( # pylint: disable=too-many-locals,too-many-branches
"""
# NumPy is needed for calculations
if globals().get("np", None) is None:
if "optimade.adapters" in repr(globals().get("np")):
warn(NUMPY_NOT_FOUND)
return None

Expand Down
4 changes: 2 additions & 2 deletions optimade/adapters/structures/jarvis.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
try:
from jarvis.core.atoms import Atoms
except (ImportError, ModuleNotFoundError):
Atoms = None
Atoms = type("Atoms", (), {})
JARVIS_NOT_FOUND = "jarvis-tools package not found, cannot convert structure to a JARVIS Atoms. Visit https://github.com/usnistgov/jarvis"


Expand All @@ -38,7 +38,7 @@ def get_jarvis_atoms(optimade_structure: OptimadeStructure) -> Atoms:
A jarvis `Atoms` object.
"""
if globals().get("Atoms", None) is None:
if "optimade.adapters" in repr(globals().get("Atoms")):
warn(JARVIS_NOT_FOUND)
return None

Expand Down
4 changes: 2 additions & 2 deletions optimade/adapters/structures/proteindatabank.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
except ImportError:
from warnings import warn

np = None
np = type("np", (), {})
NUMPY_NOT_FOUND = "NumPy not found, cannot convert structure to your desired format"

from optimade.models import Species as OptimadeStructureSpecies
Expand Down Expand Up @@ -60,7 +60,7 @@ def get_pdbx_mmcif( # pylint: disable=too-many-locals
A modern PDBx/mmCIF file as a single Python `str` object.
"""
if globals().get("np", None) is None:
if "optimade.adapters" in repr(globals().get("np")):
warn(NUMPY_NOT_FOUND)
return None

Expand Down
6 changes: 3 additions & 3 deletions optimade/adapters/structures/pymatgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
except (ImportError, ModuleNotFoundError):
from warnings import warn

Structure = None
Molecule = None
Structure = type("Structure", (), {})
Molecule = type("Molecule", (), {})
PYMATGEN_NOT_FOUND = "Pymatgen not found, cannot convert structure to a pymatgen Structure or Molecule"


Expand All @@ -46,7 +46,7 @@ def get_pymatgen(optimade_structure: OptimadeStructure) -> Union[Structure, Mole
OPTIMADE structure.
"""
if globals().get("Structure", None) is None:
if "optimade.adapters" in repr(globals().get("Structure")):
warn(PYMATGEN_NOT_FOUND)
return None

Expand Down
13 changes: 7 additions & 6 deletions optimade/adapters/structures/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
except ImportError:
from warnings import warn

np = None
np = type("np", (), {})
NUMPY_NOT_FOUND = "NumPy not found, cannot convert structure to your desired format"


Expand All @@ -32,7 +32,8 @@ def scaled_cell(
A scaled 3x3 cell.
"""
if globals().get("np", None) is None:
if "optimade.adapters" in repr(globals().get("np")):
print(repr(globals().get("np")))
warn(NUMPY_NOT_FOUND)
return None

Expand Down Expand Up @@ -65,7 +66,7 @@ def fractional_coordinates(
A list of fractional coordinates for the atomic positions.
"""
if globals().get("np", None) is None:
if "optimade.adapters" in repr(globals().get("np")):
warn(NUMPY_NOT_FOUND)
return None

Expand Down Expand Up @@ -104,7 +105,7 @@ def cell_to_cellpar(
The unit cell parameters as a `list` of `float` values.
"""
if globals().get("np", None) is None:
if "optimade.adapters" in repr(globals().get("np")):
warn(NUMPY_NOT_FOUND)
return None

Expand Down Expand Up @@ -137,7 +138,7 @@ def unit_vector(x: Vector3D) -> Vector3D:
A unit vector in the same direction as `x`.
"""
if globals().get("np", None) is None:
if "optimade.adapters" in repr(globals().get("np")):
warn(NUMPY_NOT_FOUND)
return None

Expand Down Expand Up @@ -191,7 +192,7 @@ def cellpar_to_cell(
[`lattice_vectors`][optimade.models.structures.StructureResourceAttributes.lattice_vectors] attribute.
"""
if globals().get("np", None) is None:
if "optimade.adapters" in repr(globals().get("np")):
warn(NUMPY_NOT_FOUND)
return None

Expand Down

0 comments on commit 0561f7b

Please sign in to comment.