From e78f6e1d3811ccaf7ec4ed694dc65b015b793b9b Mon Sep 17 00:00:00 2001 From: Thomas Kluyver <thomas@kluyver.me.uk> Date: Thu, 27 Apr 2023 09:28:13 +0100 Subject: [PATCH] Find default caldb_root once, based on whether directories exist --- src/cal_tools/calcat_interface.py | 42 +++++++++++++------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/src/cal_tools/calcat_interface.py b/src/cal_tools/calcat_interface.py index 67e5b02ca..ab369a8b8 100644 --- a/src/cal_tools/calcat_interface.py +++ b/src/cal_tools/calcat_interface.py @@ -1,9 +1,6 @@ """Interfaces to calibration constant data.""" -import re -import socket from datetime import date, datetime, time, timezone from functools import lru_cache -from os import getenv from pathlib import Path from weakref import WeakKeyDictionary @@ -367,6 +364,7 @@ class CalibrationData: calibrations = set() default_client = None + _default_caldb_root = ... def __init__( self, @@ -409,9 +407,10 @@ class CalibrationData: self.event_at = event_at self.pdu_snapshot_at = event_at self.module_naming = module_naming - if caldb_root is not None: - caldb_root = Path(caldb_root) - self._caldb_root = caldb_root + if caldb_root is None: + self.caldb_root = self._get_default_caldb_root() + else: + self.caldb_root = Path(caldb_root) if client is None: @@ -492,24 +491,19 @@ class CalibrationData: ) return CalibrationData.default_client - @property - def caldb_root(self): - """Root directory for calibration constant data. - - Returns: - (Path or None) Location of caldb store or - None if not available. - """ - - if self._caldb_root is None: - if getenv("SASE"): - # ONC - self._caldb_root = Path("/common/cal/caldb_store") - elif re.match(r"^max-(.+)\.desy\.de$", socket.getfqdn()): - # Maxwell - self._caldb_root = Path("/gpfs/exfel/d/cal/caldb_store") - - return self._caldb_root + @staticmethod + def _get_default_caldb_root(): + if CalibrationData._default_caldb_root is ...: + onc_path = Path("/common/cal/caldb_store") + maxwell_path = Path("/gpfs/exfel/d/cal/caldb_store") + if onc_path.is_dir(): + CalibrationData._default_caldb_root = onc_path + elif maxwell_path.is_dir(): + CalibrationData._default_caldb_root = maxwell_path + else: + CalibrationData._default_caldb_root = None + + return CalibrationData._default_caldb_root @property def client(self): -- GitLab