Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nest all resters #833

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 85 additions & 19 deletions mp_api/client/mprester.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
GrainBoundaryRester,
MagnetismRester,
MaterialsRester,
MoleculesRester,
OxidationStatesRester,
PhononRester,
PiezoRester,
Expand Down Expand Up @@ -111,7 +110,6 @@ class MPRester:
magnetism: MagnetismRester
summary: SummaryRester
robocrys: RobocrysRester
molecules: MoleculesRester
synthesis: SynthesisRester
insertion_electrodes: ElectrodeRester
charge_density: ChargeDensityRester
Expand Down Expand Up @@ -242,12 +240,34 @@ def __init__(
if not self.endpoint.endswith("/"):
self.endpoint += "/"

# Set rester attributes
resters = []

for _cls in BaseRester.__subclasses__():
sub_resters = _cls.__subclasses__()
if sub_resters:
resters.extend(sub_resters)
else:
resters.append(_cls)

core_suffix = ["molecules/core", "materials/core"]

core_resters = {
cls.suffix.split("/")[0]: cls(
api_key=api_key,
endpoint=endpoint,
include_user_agent=include_user_agent,
session=self.session,
monty_decode=monty_decode,
use_document_model=use_document_model,
headers=self.headers,
)
for cls in resters
if cls.suffix in core_suffix
}

resters = sub_resters if sub_resters else [_cls]

for cls in resters:
for cls in resters:
if cls.suffix not in core_suffix:
rester = cls(
api_key=api_key,
endpoint=endpoint,
Expand All @@ -264,27 +284,33 @@ def __init__(

suffix_split = cls.suffix.split("/")

att_map = {"molecules/core": "molecules", "materials/core": "materials"}

if len(suffix_split) == 1:
setattr(
self,
suffix_split[0],
rester,
)
else:
if cls.suffix in att_map:
attr = att_map[cls.suffix]
elif "materials" in suffix_split:
attr = "_".join(suffix_split[1:])
else:
attr = "_".join(suffix_split)
attr = "_".join(suffix_split[1:])
if "materials" in suffix_split:
setattr(
core_resters["materials"],
attr,
rester,
)
elif "molecules" in suffix_split:
setattr(
core_resters["molecules"],
attr,
rester,
)

setattr(
self,
attr,
rester,
)
for attr, rester in core_resters.items():
setattr(
self,
attr,
rester,
)

def __enter__(self):
"""Support for "with" context."""
Expand All @@ -295,12 +321,51 @@ def __exit__(self, exc_type, exc_val, exc_tb):
self.session.close()

def __getattribute__(self, attr):
_deprecated_attributes = [
"eos",
"similarity",
"tasks",
"xas",
"fermi",
"grain_boundary",
"substrates",
"surface_properties",
"phonon",
"elasticity",
"thermo",
"dielectric",
"piezoelectric",
"magnetism",
"summary",
"robocrys",
"synthesis",
"insertion_electrodes",
"charge_density",
"electronic_structure",
"electronic_structure_bandstructure",
"electronic_structure_dos",
"oxidation_states",
"provenance",
"bonds",
"alloys",
"absorption",
"chemenv",
]

if "molecules" in attr:
warnings.warn(
"NOTE: You are accessing a new set of molecules data to be officially released very soon. "
"This dataset includes many new properties, and is designed to be more easily expanded. "
"For the previous (legacy) molecules data, use the MPRester.legacy_jcesr rester. "
"For the previous (legacy) molecules data, use the MPRester.molecules.jcesr rester. "
)
elif attr in _deprecated_attributes:
warnings.warn(
f"Accessing {attr} data through MPRester.{attr} is deprecated. "
f"Please use MPRester.materials.{attr} instead.",
DeprecationWarning,
stacklevel=2,
)
return super().__getattribute__("materials").__getattribute__(attr)
return super().__getattribute__(attr)

def __getattr__(self, attr):
Expand All @@ -314,6 +379,7 @@ def __getattr__(self, attr):
"boto3 not installed. "
"To query charge density data first install with: 'pip install boto3'"
)

else:
raise AttributeError(
f"{self.__class__.__name__!r} object has no attribute {attr!r}"
Expand Down
8 changes: 4 additions & 4 deletions mp_api/client/routes/_user_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def set_user_settings(self, consumer_id, settings): # pragma: no cover
f"Invalid setting key {key}. Must be one of institution, sector, job_role, is_email_subscribed"
)
body[f"settings.{key}"] = settings[key]
return self._patch_resource(
body=body, params={"consumer_id": consumer_id}
).get("data")

return self._patch_resource(body=body, params={"consumer_id": consumer_id}).get(
"data"
)

def patch_user_time_settings(self, consumer_id, time): # pragma: no cover
"""Set user settings.
Expand Down
2 changes: 1 addition & 1 deletion mp_api/client/routes/materials/molecules.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class MoleculesRester(BaseRester[MoleculesDoc]):
suffix = "legacy/jcesr"
suffix = "molecules/jcesr"
document_model = MoleculesDoc # type: ignore
primary_key = "task_id"

Expand Down