Skip to content

Commit 4378a73

Browse files
orvizferag
authored andcommitted
Implement load_config() in EvaluatorBase class
1 parent 8c42a5e commit 4378a73

File tree

3 files changed

+26
-47
lines changed

3 files changed

+26
-47
lines changed

fair_eva/__init__.py

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,8 @@
11
#!/usr/bin/env python3
22

3-
import configparser
4-
import logging
5-
import os
6-
import sys
7-
83
import connexion
94
from connexion.resolver import RestyResolver
105

11-
logging.basicConfig(
12-
stream=sys.stdout,
13-
level=logging.DEBUG,
14-
format="%(levelname)s:'%(name)s:%(lineno)s' | %(message)s",
15-
)
16-
logger = logging.getLogger("api")
17-
18-
app_dirname = os.path.dirname(os.path.abspath(__file__))
19-
20-
21-
def load_config(plugin, fail_if_no_config=True):
22-
config_file_main = os.path.join(app_dirname, "config.ini")
23-
config_file_plugin = os.path.join(app_dirname, "plugins/%s/config.ini" % plugin)
24-
config = configparser.ConfigParser()
25-
try:
26-
config.read([config_file_main, config_file_plugin])
27-
logging.debug(
28-
"Successfully loaded main & plugin's configuration files (%s, %s)"
29-
% (config_file_main, config_file_plugin)
30-
)
31-
except FileNotFoundError as e:
32-
logging.error("Could not load config file: %s" % str(e))
33-
if fail_if_no_config:
34-
raise (e)
35-
except configparser.MissingSectionHeaderError as e:
36-
message = "Could not find any/all of the following config files: %s, %s" % (
37-
config_file_main,
38-
config_file_plugin,
39-
)
40-
logging.error(message)
41-
logging.debug(e)
42-
error = {"code": 500, "message": "%s" % message}
43-
logging.debug("Returning API response: %s" % error)
44-
return json.dumps(error), 500
45-
46-
return config
47-
486

497
def main():
508
app = connexion.FlaskApp(__name__)

fair_eva/api/evaluator.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import xml.etree.ElementTree as ET
1010
from abc import ABC, abstractmethod
1111
from functools import wraps
12+
from importlib.resources import read_text
1213

1314
import idutils
1415
import pandas as pd
@@ -182,6 +183,29 @@ def __init__(
182183
global _
183184
_ = self.translation()
184185

186+
@staticmethod
187+
def load_config(plugin_path, fail_if_no_config=True):
188+
"""Find the path to a data file."""
189+
config = configparser.ConfigParser()
190+
try:
191+
config.read_string(read_text("fair_eva", "config.ini"))
192+
config.read_string(read_text(plugin_path, "config.ini"))
193+
except FileNotFoundError as e:
194+
logging.error("Could not load config file: %s" % str(e))
195+
if fail_if_no_config:
196+
raise (e)
197+
except configparser.MissingSectionHeaderError as e:
198+
message = "Bad INI format in main and/or plugin's configuration files"
199+
logging.error(message)
200+
logging.debug(e)
201+
error = {"code": 500, "message": "%s" % message}
202+
logging.debug("Returning API response: %s" % error)
203+
return json.dumps(error), 500
204+
else:
205+
logging.debug("Successfully loaded main & plugin's configuration files")
206+
207+
return config
208+
185209
def translation(self):
186210
# Translations
187211
t = gettext.translation(

fair_eva/api/rda.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from connexion import NoContent
1010

1111
import fair_eva.api.utils as ut
12-
from fair_eva import app_dirname, load_config
1312
from fair_eva.api import evaluator
1413

1514
PLUGIN_PATH = "fair_eva.plugin" # FIXME get it from main config.ini
@@ -97,7 +96,7 @@ def wrapper(body, **kwargs):
9796
downstream_logger.addHandler(evaluator_handler)
9897

9998
# Load configuration
100-
config_data = load_config(plugin=plugin_name)
99+
config_data = plugin_module.Plugin.load_config(f"{PLUGIN_PATH}.{plugin_name}")
101100

102101
# Collect FAIR checks per metadata identifier
103102
result = {}
@@ -1355,9 +1354,7 @@ def rda_all(body, eva):
13551354
num_of_tests = 10
13561355

13571356
generic_config = eva.config["Generic"]
1358-
api_config = os.path.join(
1359-
app_dirname, generic_config.get("api_config", "fair-api.yaml")
1360-
)
1357+
api_config = generic_config.get("api_config", "fair-api.yaml")
13611358
try:
13621359
with open(api_config, "r") as f:
13631360
documents = yaml.full_load(f)

0 commit comments

Comments
 (0)