Skip to content

Commit

Permalink
fix: use object loader for finding the template
Browse files Browse the repository at this point in the history
  • Loading branch information
fstagni committed Jun 11, 2024
1 parent c903780 commit 2b314e3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
23 changes: 11 additions & 12 deletions src/DIRAC/Core/Utilities/ElasticSearchDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 2 additions & 4 deletions src/DIRAC/Core/Utilities/Extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions src/DIRAC/Core/Utilities/ObjectLoader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 2b314e3

Please sign in to comment.