Skip to content

Commit 22cb374

Browse files
authored
Merge pull request #250 from IFCA-Advanced-Computing/feature/load_config
Simultaneously load main and plugin's config.ini
2 parents 716964e + bdc9ffb commit 22cb374

File tree

12 files changed

+57
-56
lines changed

12 files changed

+57
-56
lines changed

api/evaluator.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@
1313
import requests
1414

1515
import api.utils as ut
16-
from fair import load_config
1716

18-
logging.basicConfig(
19-
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
20-
)
21-
logger = logging.getLogger(os.path.basename(__file__))
17+
logger = logging.getLogger("api.plugin.evaluation_steps")
2218

2319

2420
class ConfigTerms(property):
@@ -130,13 +126,13 @@ class Evaluator(object):
130126
lang : Language
131127
"""
132128

133-
def __init__(self, item_id, oai_base=None, lang="en", plugin=None):
129+
def __init__(self, item_id, oai_base=None, lang="en", plugin=None, config=None):
134130
self.item_id = item_id
135131
self.oai_base = oai_base
136132
self.metadata = None
137133
self.access_protocols = []
138134
self.cvs = []
139-
self.config = load_config(plugin=plugin)
135+
self.config = config
140136
# configuration terms
141137
self.terms_access_metadata = pd.DataFrame()
142138
self.terms_license_metadata = pd.DataFrame()

api/rda.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
logging.basicConfig(
1616
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
1717
)
18-
logger = logging.getLogger(os.path.basename(__file__))
19-
20-
config = load_config()
18+
logger = logging.getLogger("api")
2119

2220

2321
def load_evaluator(wrapped_func):
@@ -35,9 +33,9 @@ def wrapper(body, **kwargs):
3533
msg = "Neither the identifier nor the pattern to query was provided. Exiting.."
3634
logger.error(msg)
3735
return msg, 400
38-
3936
# Get the identifiers through a search query
4037
ids = [item_id]
38+
4139
# FIXME oai-pmh should be no different
4240
downstream_logger = evaluator.logger
4341
if repo not in ["oai-pmh"]:
@@ -61,15 +59,18 @@ def wrapper(body, **kwargs):
6159
evaluator_handler = ut.EvaluatorLogHandler()
6260
downstream_logger.addHandler(evaluator_handler)
6361

62+
# Load configuration
63+
config_data = load_config(plugin=repo)
64+
6465
# Collect FAIR checks per metadata identifier
6566
result = {}
6667
exit_code = 200
6768
for item_id in ids:
6869
# FIXME oai-pmh should be no different
6970
if repo in ["oai-pmh"]:
70-
eva = evaluator.Evaluator(item_id, oai_base, lang)
71+
eva = evaluator.Evaluator(item_id, oai_base, lang, config=config_data)
7172
else:
72-
eva = plugin.Plugin(item_id, oai_base, lang)
73+
eva = plugin.Plugin(item_id, oai_base, lang, config=config_data)
7374
_result, _exit_code = wrapped_func(body, eva=eva)
7475
logger.debug(
7576
"Raw result returned for indicator ID '%s': %s" % (item_id, _result)
@@ -99,8 +100,8 @@ def endpoints(plugin=None, plugins_path="plugins"):
99100

100101
# Obtain endpoint from each plugin's config
101102
for plug in plugins_list:
102-
config = load_config(plugin=plug, fail_if_no_config=False)
103-
endpoint = config.get("Generic", "endpoint", fallback="")
103+
_config = load_config(plugin=plug, fail_if_no_config=False)
104+
endpoint = _config.get("Generic", "endpoint", fallback="")
104105
if not endpoint:
105106
logger.debug(
106107
"Plugin's config does not contain 'Generic:endpoint' section: %s" % plug
@@ -1339,7 +1340,7 @@ def rda_all(body, eva):
13391340
result_points = 10
13401341
num_of_tests = 10
13411342

1342-
generic_config = config["Generic"]
1343+
generic_config = eva.config["Generic"]
13431344
api_config = os.path.join(
13441345
app_dirname, generic_config.get("api_config", "fair-api.yaml")
13451346
)

api/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
import requests
1313
from bs4 import BeautifulSoup
1414

15-
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
15+
logging.basicConfig(
16+
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
17+
)
18+
logger = logging.getLogger("api.utils")
1619

1720

1821
class EvaluatorLogHandler(logging.Handler):

fair.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,30 @@
1313
level=logging.DEBUG,
1414
format="%(levelname)s:'%(name)s:%(lineno)s' | %(message)s",
1515
)
16-
17-
logger = logging.getLogger(os.path.basename(__file__))
16+
logger = logging.getLogger("api")
1817

1918
app_dirname = os.path.dirname(os.path.abspath(__file__))
2019

2120

22-
def load_config(plugin=None, fail_if_no_config=True):
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)
2324
config = configparser.ConfigParser()
24-
if plugin:
25-
config_file = os.path.join(app_dirname, "plugins/%s/config.ini" % plugin)
26-
else:
27-
config_file = os.path.join(app_dirname, "config.ini")
28-
if "CONFIG_FILE" in os.environ:
29-
config_file = os.getenv("CONFIG_FILE")
30-
3125
try:
32-
config.read_file(open(config_file))
33-
logging.debug("Main configuration successfully loaded: %s" % config_file)
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+
)
3431
except FileNotFoundError as e:
3532
logging.error("Could not load config file: %s" % str(e))
3633
if fail_if_no_config:
3734
raise (e)
3835
except configparser.MissingSectionHeaderError as e:
39-
message = "Could not find main config file: %s" % config_file
36+
message = "Could not find any/all of the following config files: %s, %s" % (
37+
config_file_main,
38+
config_file_plugin,
39+
)
4040
logging.error(message)
4141
logging.debug(e)
4242
error = {"code": 500, "message": "%s" % message}

plugins/digital_csic/config.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[Generic]
2-
endpoint='http://digital.csic.es/dspace-oai/request'
2+
endpoint=http://digital.csic.es/dspace-oai/request
33
[digital_csic]
44
db_host =
55
db_port =

plugins/digital_csic/plugin.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
logging.basicConfig(
2222
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
2323
)
24-
25-
logger = logging.getLogger(os.path.basename(__file__))
24+
logger = logging.getLogger("api.plugin")
2625

2726

2827
class ConfigTerms(property):
@@ -87,7 +86,7 @@ class Plugin(Evaluator):
8786
lang : Language
8887
"""
8988

90-
def __init__(self, item_id, oai_base=None, lang="en"):
89+
def __init__(self, item_id, oai_base=None, lang="en", config=None):
9190
logger.debug("Call parent")
9291
plugin = "digital_csic"
9392
super().__init__(item_id, oai_base, lang, plugin)

plugins/dspace7/plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
logging.basicConfig(
1717
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
1818
)
19-
logger = logging.getLogger(os.path.basename(__file__))
19+
logger = logging.getLogger("api.plugin")
2020

2121

2222
class DSpace_7(Evaluator):
@@ -32,7 +32,7 @@ class DSpace_7(Evaluator):
3232
lang : Language
3333
"""
3434

35-
def __init__(self, item_id, oai_base=None, lang="en"):
35+
def __init__(self, item_id, oai_base=None, lang="en", config=None):
3636
if oai_base == "":
3737
oai_base = None
3838
logger.debug("Call parent")

plugins/epos/plugin.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818

1919
import api.utils as ut
2020
from api.evaluator import ConfigTerms, Evaluator
21-
from fair import load_config
2221

2322
logging.basicConfig(
2423
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
2524
)
26-
logger = logging.getLogger(os.path.basename(__file__))
25+
logger = logging.getLogger("api.plugin")
2726

2827

2928
class Plugin(Evaluator):
@@ -46,15 +45,20 @@ class Plugin(Evaluator):
4645

4746
name = "epos"
4847

49-
def __init__(self, item_id, oai_base=None, lang="en", config=None):
50-
logger.debug("Creating instance of %s plugin" % self.name)
51-
super().__init__(item_id, oai_base, lang, self.name)
52-
# TO REDEFINE - WHICH IS YOUR PID TYPE?
53-
self.id_type = "uuid"
54-
global _
55-
_ = super().translation()
48+
def __init__(self, item_id, oai_base=None, lang="en", config=None, name="epos"):
49+
# FIXME: Disable calls to parent class until a EvaluatorBase class is implemented
50+
# super().__init__(item_id, oai_base, lang, self.name)
51+
# global _
52+
# _ = super().translation()
5653

57-
# You need a way to get your metadata in a similar format
54+
self.name = name
55+
self.item_id = item_id
56+
self.api_endpoint = oai_base
57+
self.config = config
58+
59+
logger.debug("Using FAIR-EVA's plugin: %s" % self.name)
60+
61+
# Metadata gathering
5862
metadata_sample = self.get_metadata()
5963
self.metadata = pd.DataFrame(
6064
metadata_sample,
@@ -148,7 +152,7 @@ def get_metadata(self):
148152
eml_schema = "epos"
149153

150154
final_url = (
151-
self.oai_base + "/resources/details/" + self.item_id + "?extended=true"
155+
self.api_endpoint + "/resources/details/" + self.item_id + "?extended=true"
152156
)
153157

154158
error_in_metadata = False

plugins/example_plugin/plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
logging.basicConfig(
1414
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
1515
)
16-
logger = logging.getLogger(os.path.basename(__file__))
16+
logger = logging.getLogger("api.plugin")
1717

1818

1919
class Plugin(Evaluator):
@@ -29,7 +29,7 @@ class Plugin(Evaluator):
2929
lang : Language
3030
"""
3131

32-
def __init__(self, item_id, oai_base=None, lang="en"):
32+
def __init__(self, item_id, oai_base=None, lang="en", config=None):
3333
plugin = "example_plugin"
3434
super().__init__(item_id, oai_base, lang, plugin)
3535
# TO REDEFINE - WHICH IS YOUR PID TYPE?

plugins/gbif/plugin.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
logging.basicConfig(
1818
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
1919
)
20-
21-
logger = logging.getLogger(os.path.basename(__file__))
20+
logger = logging.getLogger("api.plugin")
2221

2322

2423
class Plugin(Evaluator):
@@ -37,7 +36,7 @@ class Plugin(Evaluator):
3736
lang : Language
3837
"""
3938

40-
def __init__(self, item_id, oai_base=None, lang="en"):
39+
def __init__(self, item_id, oai_base=None, lang="en", config=None):
4140
logger.debug("Creating GBIF")
4241
plugin = "gbif"
4342
super().__init__(item_id, oai_base, lang, plugin)

0 commit comments

Comments
 (0)