Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simultaneously load main and plugin's config.ini #250

Merged
merged 10 commits into from
Sep 17, 2024
10 changes: 3 additions & 7 deletions api/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@
import requests

import api.utils as ut
from fair import load_config

logging.basicConfig(
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
)
logger = logging.getLogger(os.path.basename(__file__))
logger = logging.getLogger("api.plugin.evaluation_steps")


class ConfigTerms(property):
Expand Down Expand Up @@ -130,13 +126,13 @@ class Evaluator(object):
lang : Language
"""

def __init__(self, item_id, oai_base=None, lang="en", plugin=None):
def __init__(self, item_id, oai_base=None, lang="en", plugin=None, config=None):
self.item_id = item_id
self.oai_base = oai_base
self.metadata = None
self.access_protocols = []
self.cvs = []
self.config = load_config(plugin=plugin)
self.config = config
# configuration terms
self.terms_access_metadata = pd.DataFrame()
self.terms_license_metadata = pd.DataFrame()
Expand Down
19 changes: 10 additions & 9 deletions api/rda.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
logging.basicConfig(
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
)
logger = logging.getLogger(os.path.basename(__file__))

config = load_config()
logger = logging.getLogger("api")


def load_evaluator(wrapped_func):
Expand All @@ -35,9 +33,9 @@ def wrapper(body, **kwargs):
msg = "Neither the identifier nor the pattern to query was provided. Exiting.."
logger.error(msg)
return msg, 400

# Get the identifiers through a search query
ids = [item_id]

# FIXME oai-pmh should be no different
downstream_logger = evaluator.logger
if repo not in ["oai-pmh"]:
Expand All @@ -61,15 +59,18 @@ def wrapper(body, **kwargs):
evaluator_handler = ut.EvaluatorLogHandler()
downstream_logger.addHandler(evaluator_handler)

# Load configuration
config_data = load_config(plugin=repo)

# Collect FAIR checks per metadata identifier
result = {}
exit_code = 200
for item_id in ids:
# FIXME oai-pmh should be no different
if repo in ["oai-pmh"]:
eva = evaluator.Evaluator(item_id, oai_base, lang)
eva = evaluator.Evaluator(item_id, oai_base, lang, config=config_data)
else:
eva = plugin.Plugin(item_id, oai_base, lang)
eva = plugin.Plugin(item_id, oai_base, lang, config=config_data)
_result, _exit_code = wrapped_func(body, eva=eva)
logger.debug(
"Raw result returned for indicator ID '%s': %s" % (item_id, _result)
Expand Down Expand Up @@ -99,8 +100,8 @@ def endpoints(plugin=None, plugins_path="plugins"):

# Obtain endpoint from each plugin's config
for plug in plugins_list:
config = load_config(plugin=plug, fail_if_no_config=False)
endpoint = config.get("Generic", "endpoint", fallback="")
_config = load_config(plugin=plug, fail_if_no_config=False)
endpoint = _config.get("Generic", "endpoint", fallback="")
if not endpoint:
logger.debug(
"Plugin's config does not contain 'Generic:endpoint' section: %s" % plug
Expand Down Expand Up @@ -1339,7 +1340,7 @@ def rda_all(body, eva):
result_points = 10
num_of_tests = 10

generic_config = config["Generic"]
generic_config = eva.config["Generic"]
api_config = os.path.join(
app_dirname, generic_config.get("api_config", "fair-api.yaml")
)
Expand Down
5 changes: 4 additions & 1 deletion api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import requests
from bs4 import BeautifulSoup

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logging.basicConfig(
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
)
logger = logging.getLogger("api.utils")


class EvaluatorLogHandler(logging.Handler):
Expand Down
26 changes: 13 additions & 13 deletions fair.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@
level=logging.DEBUG,
format="%(levelname)s:'%(name)s:%(lineno)s' | %(message)s",
)

logger = logging.getLogger(os.path.basename(__file__))
logger = logging.getLogger("api")

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


def load_config(plugin=None, fail_if_no_config=True):
def load_config(plugin, fail_if_no_config=True):
config_file_main = os.path.join(app_dirname, "config.ini")
config_file_plugin = os.path.join(app_dirname, "plugins/%s/config.ini" % plugin)
config = configparser.ConfigParser()
if plugin:
config_file = os.path.join(app_dirname, "plugins/%s/config.ini" % plugin)
else:
config_file = os.path.join(app_dirname, "config.ini")
if "CONFIG_FILE" in os.environ:
config_file = os.getenv("CONFIG_FILE")

try:
config.read_file(open(config_file))
logging.debug("Main configuration successfully loaded: %s" % config_file)
config.read([config_file_main, config_file_plugin])
logging.debug(
"Successfully loaded main & plugin's configuration files (%s, %s)"
% (config_file_main, config_file_plugin)
)
except FileNotFoundError as e:
logging.error("Could not load config file: %s" % str(e))
if fail_if_no_config:
raise (e)
except configparser.MissingSectionHeaderError as e:
message = "Could not find main config file: %s" % config_file
message = "Could not find any/all of the following config files: %s, %s" % (
config_file_main,
config_file_plugin,
)
logging.error(message)
logging.debug(e)
error = {"code": 500, "message": "%s" % message}
Expand Down
2 changes: 1 addition & 1 deletion plugins/digital_csic/config.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[Generic]
endpoint='http://digital.csic.es/dspace-oai/request'
endpoint=http://digital.csic.es/dspace-oai/request
[digital_csic]
db_host =
db_port =
Expand Down
5 changes: 2 additions & 3 deletions plugins/digital_csic/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
logging.basicConfig(
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
)

logger = logging.getLogger(os.path.basename(__file__))
logger = logging.getLogger("api.plugin")


class ConfigTerms(property):
Expand Down Expand Up @@ -87,7 +86,7 @@ class Plugin(Evaluator):
lang : Language
"""

def __init__(self, item_id, oai_base=None, lang="en"):
def __init__(self, item_id, oai_base=None, lang="en", config=None):
logger.debug("Call parent")
plugin = "digital_csic"
super().__init__(item_id, oai_base, lang, plugin)
Expand Down
4 changes: 2 additions & 2 deletions plugins/dspace7/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
logging.basicConfig(
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
)
logger = logging.getLogger(os.path.basename(__file__))
logger = logging.getLogger("api.plugin")


class DSpace_7(Evaluator):
Expand All @@ -32,7 +32,7 @@ class DSpace_7(Evaluator):
lang : Language
"""

def __init__(self, item_id, oai_base=None, lang="en"):
def __init__(self, item_id, oai_base=None, lang="en", config=None):
if oai_base == "":
oai_base = None
logger.debug("Call parent")
Expand Down
26 changes: 15 additions & 11 deletions plugins/epos/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@

import api.utils as ut
from api.evaluator import ConfigTerms, Evaluator
from fair import load_config

logging.basicConfig(
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
)
logger = logging.getLogger(os.path.basename(__file__))
logger = logging.getLogger("api.plugin")


class Plugin(Evaluator):
Expand All @@ -46,15 +45,20 @@ class Plugin(Evaluator):

name = "epos"

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

# You need a way to get your metadata in a similar format
self.name = name
self.item_id = item_id
self.api_endpoint = oai_base
self.config = config

logger.debug("Using FAIR-EVA's plugin: %s" % self.name)

# Metadata gathering
metadata_sample = self.get_metadata()
self.metadata = pd.DataFrame(
metadata_sample,
Expand Down Expand Up @@ -148,7 +152,7 @@ def get_metadata(self):
eml_schema = "epos"

final_url = (
self.oai_base + "/resources/details/" + self.item_id + "?extended=true"
self.api_endpoint + "/resources/details/" + self.item_id + "?extended=true"
)

error_in_metadata = False
Expand Down
4 changes: 2 additions & 2 deletions plugins/example_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
logging.basicConfig(
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
)
logger = logging.getLogger(os.path.basename(__file__))
logger = logging.getLogger("api.plugin")


class Plugin(Evaluator):
Expand All @@ -29,7 +29,7 @@ class Plugin(Evaluator):
lang : Language
"""

def __init__(self, item_id, oai_base=None, lang="en"):
def __init__(self, item_id, oai_base=None, lang="en", config=None):
plugin = "example_plugin"
super().__init__(item_id, oai_base, lang, plugin)
# TO REDEFINE - WHICH IS YOUR PID TYPE?
Expand Down
5 changes: 2 additions & 3 deletions plugins/gbif/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
logging.basicConfig(
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
)

logger = logging.getLogger(os.path.basename(__file__))
logger = logging.getLogger("api.plugin")


class Plugin(Evaluator):
Expand All @@ -37,7 +36,7 @@ class Plugin(Evaluator):
lang : Language
"""

def __init__(self, item_id, oai_base=None, lang="en"):
def __init__(self, item_id, oai_base=None, lang="en", config=None):
logger.debug("Creating GBIF")
plugin = "gbif"
super().__init__(item_id, oai_base, lang, plugin)
Expand Down
4 changes: 2 additions & 2 deletions plugins/signposting/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
logging.basicConfig(
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
)
logger = logging.getLogger(os.path.basename(__file__))
logger = logging.getLogger("api.plugin")


class Plugin(Evaluator):
Expand All @@ -43,7 +43,7 @@ class Plugin(Evaluator):
Prints the animals name and what sound it makes
"""

def __init__(self, item_id, oai_base=None, lang="en"):
def __init__(self, item_id, oai_base=None, lang="en", config=None):
plugin = "signposting"
super().__init__(item_id, oai_base, lang, plugin)
# TO REDEFINE - WHICH IS YOUR PID TYPE?
Expand Down
3 changes: 1 addition & 2 deletions web.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
logging.basicConfig(
stream=sys.stdout, level=logging.DEBUG, format="'%(name)s:%(lineno)s' | %(message)s"
)

logger = logging.getLogger(os.path.basename(__file__))
logger = logging.getLogger("web")


def set_parser():
Expand Down
Loading