From 3e54dc914ea5db42c82017e7bfe9f8070174d886 Mon Sep 17 00:00:00 2001 From: Janosh Riebesell Date: Fri, 21 Oct 2022 12:26:40 -0700 Subject: [PATCH] _load_pmg_settings() make SETTINGS_FILE read op atomic --- pymatgen/core/__init__.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pymatgen/core/__init__.py b/pymatgen/core/__init__.py index b542acdd10c..da7edb5b1e2 100644 --- a/pymatgen/core/__init__.py +++ b/pymatgen/core/__init__.py @@ -34,16 +34,16 @@ def _load_pmg_settings() -> dict[str, str]: settings = {} # Load .pmgrc.yaml file - if os.path.exists(SETTINGS_FILE): - try: - yaml = YAML() - with open(SETTINGS_FILE) as f: - d_yml = yaml.load(f) - settings.update(d_yml) - except Exception as ex: - # If there are any errors, default to using environment variables - # if present. - warnings.warn(f"Error loading .pmgrc.yaml: {ex}. You may need to reconfigure your yaml file.") + try: + yaml = YAML() + with open(SETTINGS_FILE) as yml_file: + settings = yaml.load(yml_file) + except FileNotFoundError: + pass + except Exception as ex: + # If there are any errors, default to using environment variables + # if present. + warnings.warn(f"Error loading .pmgrc.yaml: {ex}. You may need to reconfigure your yaml file.") # Override .pmgrc.yaml with env vars if present for k, v in os.environ.items():