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
|
import os
from multiprocessing import cpu_count
from typing import List
from pydantic import Field
from pydantic_settings import BaseSettings
from pymatgen.core import _load_pmg_settings
from mp_api.client import __file__ as root_dir
PMG_SETTINGS = _load_pmg_settings()
_NUM_PARALLEL_REQUESTS = min(PMG_SETTINGS.get("MPRESTER_NUM_PARALLEL_REQUESTS", 4), 4)
_MAX_RETRIES = min(PMG_SETTINGS.get("MPRESTER_MAX_RETRIES", 3), 3)
_MUTE_PROGRESS_BAR = PMG_SETTINGS.get("MPRESTER_MUTE_PROGRESS_BARS", False)
_MAX_HTTP_URL_LENGTH = PMG_SETTINGS.get("MPRESTER_MAX_HTTP_URL_LENGTH", 2000)
_MAX_LIST_LENGTH = min(PMG_SETTINGS.get("MPRESTER_MAX_LIST_LENGTH", 10000), 10000)
try:
CPU_COUNT = cpu_count()
except NotImplementedError:
pass
class MAPIClientSettings(BaseSettings):
"""Special class to store settings for MAPI client
python module.
"""
TEST_FILES: str = Field(
os.path.join(os.path.dirname(os.path.abspath(root_dir)), "../../test_files"),
description="Directory with test files",
)
QUERY_NO_PARALLEL: List[str] = Field(
[
"elements",
"exclude_elements",
"possible_species",
"coordination_envs",
"coordination_envs_anonymous",
"has_props",
"gb_plane",
"rotation_axis",
"keywords",
"substrate_orientation",
"film_orientation",
"synthesis_type",
"operations",
"condition_mixing_device",
"condition_mixing_media",
"condition_heating_atmosphere",
"operations",
"_fields",
],
description="List API query parameters that do not support parallel requests.",
)
NUM_PARALLEL_REQUESTS: int = Field(
_NUM_PARALLEL_REQUESTS,
description="Number of parallel requests to send.",
)
MAX_RETRIES: int = Field(
_MAX_RETRIES, description="Maximum number of retries for requests."
)
BACKOFF_FACTOR: float = Field(
0.1,
description="A backoff factor to apply between retry attempts. To disable, set to 0.",
)
MUTE_PROGRESS_BARS: bool = Field(
_MUTE_PROGRESS_BAR,
description="Whether to mute progress bars when data is retrieved.",
)
MAX_HTTP_URL_LENGTH: int = Field(
_MAX_HTTP_URL_LENGTH,
description="Number of characters to use to define the maximum length of a given HTTP URL.",
)
MIN_EMMET_VERSION: str = Field(
"0.54.0", description="Minimum compatible version of emmet-core for the client."
)
MAX_LIST_LENGTH: int = Field(
_MAX_LIST_LENGTH, description="Maximum length of query parameter list"
)
class Config:
env_prefix = "MPRESTER_"
|