diff --git a/src/DIRAC/Core/Utilities/ElasticSearchDB.py b/src/DIRAC/Core/Utilities/ElasticSearchDB.py index 3de2703c7d6..f10f3ff83da 100644 --- a/src/DIRAC/Core/Utilities/ElasticSearchDB.py +++ b/src/DIRAC/Core/Utilities/ElasticSearchDB.py @@ -48,6 +48,7 @@ from DIRAC import S_ERROR, S_OK, gLogger from DIRAC.Core.Utilities import DErrno, TimeUtilities +from DIRAC.Core.Utilities.ObjectLoader import ObjectLoader from DIRAC.FrameworkSystem.Client.BundleDeliveryClient import BundleDeliveryClient sLog = gLogger.getSubLogger(__name__) @@ -211,21 +212,19 @@ def __init__( sLog.error(repr(e)) # look for an index template (json file) in the current directory - self.indexTemplate = None - self._loadIndexTemplate() - if self.indexTemplate: - self.client.indices.put_template(name=self.__class__.__name, body=self.indexTemplate) - - def _loadIndexTemplate(self): - """ - Load the index template from the current directory - """ try: - with open(f"{self.__class__.__name__}.json") as f: - self.indexTemplate = json.load(f) + result = ObjectLoader().loadObject( + f"DIRAC.{str(self.__class__).split('.')[1]}.DB.{self.__class__.__name__}.json", python_object=False + ) + if not result["OK"]: + sLog.warn("Cannot load index template", result["Message"]) + return + sLog.debug("Loading index template", result["Value"]) + with open(f"{result['Value']}") as f: + self.client.indices.put_template(name=self.__class__.__name__, body=json.load(f)) except Exception as e: sLog.error("Cannot load index template", repr(e)) - + def getIndexPrefix(self): """ It returns the DIRAC setup. diff --git a/src/DIRAC/Core/Utilities/Extensions.py b/src/DIRAC/Core/Utilities/Extensions.py index c23cefa4ed0..6d0f9cb0cd5 100644 --- a/src/DIRAC/Core/Utilities/Extensions.py +++ b/src/DIRAC/Core/Utilities/Extensions.py @@ -160,11 +160,10 @@ def recurseImport(modName, parentModule=None, hideExceptions=False): def recurseFind(fileName, hideExceptions=False): from DIRAC import S_ERROR, S_OK, gLogger - + name_of_file = ".".join(fileName.split(".")[-2:]) container_module = ".".join(fileName.split(".")[:-2]) - print(container_module, name_of_file) # find the path to the module that contains fileName try: imported_module = importlib.import_module(container_module) @@ -173,9 +172,8 @@ def recurseFind(fileName, hideExceptions=False): if not hideExceptions: gLogger.exception(errMsg) return S_ERROR(errMsg) - + file_absolute_location = os.path.join(os.path.dirname(imported_module.__file__), name_of_file) - print(file_absolute_location) if os.path.exists(file_absolute_location): return S_OK(file_absolute_location) else: diff --git a/src/DIRAC/Core/Utilities/ObjectLoader.py b/src/DIRAC/Core/Utilities/ObjectLoader.py index 7b51218fabd..d9c89872897 100644 --- a/src/DIRAC/Core/Utilities/ObjectLoader.py +++ b/src/DIRAC/Core/Utilities/ObjectLoader.py @@ -49,7 +49,7 @@ def __rootImport(self, modName: str, hideExceptions: bool = False, python_object if rootModule: impName = f"{rootModule}.{impName}" gLogger.debug(f"Trying to load {impName}") - if python_object: + if python_object: result = recurseImport(impName, hideExceptions=hideExceptions) else: result = recurseFind(impName, hideExceptions=hideExceptions) @@ -81,7 +81,9 @@ def loadModule(self, importString: str, hideExceptions: bool = False, python_obj return S_ERROR(DErrno.EIMPERR, f"No module {importString} found") return S_OK(result["Value"][1]) - def loadObject(self, importString: str, objName: str = "", hideExceptions: bool = False, python_object: bool = True): + def loadObject( + self, importString: str, objName: str = "", hideExceptions: bool = False, python_object: bool = True + ): """Load an object from inside a module""" if not objName: objName = importString.split(".")[-1]