Commit 689bd1c5 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Rename CdsException as StoreException

parent ee6f8b28
......@@ -7,7 +7,7 @@ import re
from check_tools import check_publication
from gluon.storage import Storage
from harvest_tools import DRY_RUN
from store_tools import CdsException, load_record
from store_tools import load_record, StoreException
from plugin_dbui import (CALLBACK_ERRORS,
get_foreign_field,
get_id,
......@@ -43,7 +43,7 @@ def affiliation_institute():
try:
record = load_record("inspirehep.net", institute_id)
except CdsException:
except StoreException:
raise HTTP(500, T(MSG_NO_SERVER))
if record is None:
......@@ -96,7 +96,7 @@ def affiliation_publication():
try:
record = load_record(publication_store, publication_id)
except CdsException:
except StoreException:
raise HTTP(500, T(MSG_NO_SERVER))
if record is None:
......@@ -437,7 +437,7 @@ def update_citations():
from json.decoder import JSONDecodeError
from reporting_tools import repr_team_project
from store_tools import CdsException, InspirehepStore
from store_tools import InspirehepStore, StoreException
today = datetime.date.today()
rex_ins = re.compile(r"inspirehep.net/record/(\d+)")
......@@ -477,7 +477,7 @@ def update_citations():
counters.recid += 1
continue
except CdsException as e:
except StoreException as e:
logger.warning(f"failed to get citations for {recid}: {e}")
counters.failed += 1
continue
......
......@@ -12,7 +12,7 @@ from gluon.storage import Storage
from .msg import Msg
from .msgcollection import MsgCollection
from plugin_dbui import CALLBACK_ERRORS, get_id
from store_tools import (CdsException,
from store_tools import (StoreException,
build_store,
OAI_URL)
from store_tools.factory import build_record
......@@ -454,7 +454,7 @@ class Automaton(object):
try:
rec_ids = store.get_ids(**kwargs)
except CdsException as error:
except StoreException as error:
logger.info(f"exit process_collection: {error}")
collection_logs[-1].url = store.last_search_url()
collection_logs[-1].error = error
......
......@@ -21,7 +21,7 @@ from .base import (ARXIV,
REG_YEAR,
THESIS_DIR)
from .exception import (CdsException,
from .exception import (StoreException,
RecordException)
from .factory import build_record, build_store
......@@ -72,7 +72,7 @@ def load_record(host, record_id, shelf=None):
RecordHepThesis, RecordHepInst, RecordPubli or RecordThesis
Raises:
CdsException::
StoreException::
* the server return an HTTP error.
* no JSON object could be decoded.
......
......@@ -3,7 +3,7 @@
"""
import requests
from .exception import CdsException
from .exception import StoreException
from requests.adapters import HTTPAdapter
MSG_HTTP_DECODE = "Fail to decode HTTP response"
......@@ -79,12 +79,12 @@ class BaseStore(object):
* None when the field is not found
Raises:
CdsException:
StoreException:
* the server return an HTTP error.
* JSON object could not be decoded.
"""
raise CdsException(MSG_NOT_IMPLEMENTED)
raise StoreException(MSG_NOT_IMPLEMENTED)
def get_ids(self, **kwargs):
"""Return a list of *record id* matching search criteria.
......@@ -98,13 +98,13 @@ class BaseStore(object):
* The list is empty when the request failed on the server.
Raises:
CdsException::
StoreException::
* the server return an HTTP error;
* JSON object can't be decoded;
* not well formed list of ids.
"""
raise CdsException(MSG_NOT_IMPLEMENTED)
raise StoreException(MSG_NOT_IMPLEMENTED)
def get_record(self, rec_id):
"""Retrieve a record defined by its *record id*.
......@@ -118,13 +118,13 @@ class BaseStore(object):
the record data (recjson).
Raises:
CdsException:
StoreException:
* the server return an HTTP error.
* JSON object could not be decoded.
* more than one record
"""
raise CdsException(MSG_NOT_IMPLEMENTED)
raise StoreException(MSG_NOT_IMPLEMENTED)
def interrogate(self, url, timeout=10, **kwargs):
"""Interrogate the store using the *URL*.
......@@ -172,9 +172,9 @@ class BaseStore(object):
* list of JSON records
Raises:
CdsException:
StoreException:
* the server return an HTTP error.
* JSON object could not be decoded.
"""
raise CdsException(MSG_NOT_IMPLEMENTED)
raise StoreException(MSG_NOT_IMPLEMENTED)
......@@ -3,9 +3,9 @@
"""
class CdsException(Exception):
class RecordException(Exception):
pass
class RecordException(Exception):
class StoreException(Exception):
pass
......@@ -13,7 +13,7 @@ from .base import (CDS,
REG_CONF)
from datetime import datetime
from .exception import CdsException, RecordException
from .exception import RecordException, StoreException
from .inveniostore import InvenioStore
from .inspirehepstore import InspirehepStore, SHELFS
from .recordconf import RecordConf
......@@ -101,7 +101,7 @@ def add_conference_data(recjson):
try:
confjson = get_conference_data(host, **kwargs)
except CdsException:
except StoreException:
return
# ........................................................................
......@@ -265,7 +265,7 @@ def build_store(host=None, shelf=None):
store = InspirehepStore(host=host, shelf=shelf)
else:
raise CdsException(MSG_NO_SHELF % (shelf, host))
raise StoreException(MSG_NO_SHELF % (shelf, host))
return store
......@@ -289,7 +289,7 @@ def get_conference_data(host, conf_id=None, key=None):
The conference data (MarcJSON).
Raises:
CdsException:
StoreException:
- conference record with a wrong identifier
- conference not found
......@@ -303,7 +303,7 @@ def get_conference_data(host, conf_id=None, key=None):
if conf_id is not None and host in CDS:
recjson = store.get_record(conf_id)
if recjson["recid"] != conf_id:
raise CdsException(MSG_INV_CONF)
raise StoreException(MSG_INV_CONF)
return recjson
# ........................................................................
......@@ -319,7 +319,7 @@ def get_conference_data(host, conf_id=None, key=None):
if match_conference_key(recjson, key):
return recjson
raise CdsException(MSG_NO_CONF)
raise StoreException(MSG_NO_CONF)
# ........................................................................
#
......@@ -336,7 +336,7 @@ def get_conference_data(host, conf_id=None, key=None):
key = key.replace("/", "-")
if not REG_CONF.match(key):
raise CdsException(MSG_INV_CONF_KEY)
raise StoreException(MSG_INV_CONF_KEY)
obj = store.search(q=f"cnum:{key}")
......@@ -344,10 +344,10 @@ def get_conference_data(host, conf_id=None, key=None):
recjson = obj[0]["metadata"]
except (KeyError, TypeError):
raise CdsException(MSG_NO_CONF)
raise StoreException(MSG_NO_CONF)
if recjson["cnum"] != key:
raise CdsException(MSG_NO_CONF)
raise StoreException(MSG_NO_CONF)
return recjson
......
......@@ -5,7 +5,7 @@ from .basestore import (BaseStore,
MSG_HTTP_DECODE,
MSG_HTTP_ERROR,
MSG_INVALID_RESPONSE)
from .exception import CdsException
from .exception import StoreException
from requests import HTTPError
SHELFS = ("literature", "conferences", "institutions")
......@@ -60,7 +60,7 @@ class InspirehepStore(BaseStore):
* None when the field is not found
Raises:
CdsException:
StoreException:
* the server return an HTTP error.
* JSON object could not be decoded.
* the record is deleted
......@@ -75,18 +75,18 @@ class InspirehepStore(BaseStore):
except IndexError:
msg = "record not found. it can be deleted"
raise CdsException(msg)
raise StoreException(msg)
except KeyError as e:
if "metadata" not in obj[0]:
msg = f"{MSG_INVALID_RESPONSE}: no 'metadata'"
elif fieldname not in obj[0]["metadata"]:
msg = f"{MSG_INVALID_RESPONSE}: no field '{fieldname}'"
raise CdsException(msg)
raise StoreException(msg)
except TypeError as e:
msg = f"{MSG_INVALID_RESPONSE}: type error {e}"
raise CdsException(msg)
raise StoreException(msg)
def get_ids(self, **kwargs):
"""Return a list of *record id* matching search criteria.
......@@ -112,7 +112,7 @@ class InspirehepStore(BaseStore):
* The list is empty when the request failed on the server.
Raises:
CdsException::
StoreException::
* keyword argument is invalid;
* the server return an HTTP error;
* JSON object can't be decoded;
......@@ -137,7 +137,7 @@ class InspirehepStore(BaseStore):
https://inspire-schemas.readthedocs.io/en/latest/schemas/
Raises:
CdsException:
StoreException:
* the server return an HTTP error.
* JSON object could not be decoded.
* more than one record
......@@ -160,19 +160,19 @@ class InspirehepStore(BaseStore):
msg = dct.get("message")
else:
msg = str(e)
raise CdsException(msg)
raise StoreException(msg)
except ValueError:
msg = f"{MSG_HTTP_DECODE}: {e}"
raise CdsException(msg)
raise StoreException(msg)
except KeyError as e:
msg = f"{MSG_INVALID_RESPONSE}: key error {e}"
raise CdsException(msg)
raise StoreException(msg)
except TypeError as e:
msg = f"{MSG_INVALID_RESPONSE}: type error {e}"
raise CdsException(msg)
raise StoreException(msg)
def search(self, **kwargs):
"""Return a list of *JSON record* matching search criteria.
......@@ -206,7 +206,7 @@ class InspirehepStore(BaseStore):
- updated (str)
Raises:
CdsException:
StoreException:
* the server return an HTTP error.
* JSON object could not be decoded.
......@@ -233,19 +233,19 @@ class InspirehepStore(BaseStore):
except HTTPError as e:
msg = f"{MSG_HTTP_ERROR}: {e}"
raise CdsException(msg)
raise StoreException(msg)
except ValueError:
msg = f"{MSG_HTTP_DECODE}: {e}"
raise CdsException(msg)
raise StoreException(msg)
except KeyError as e:
msg = f"{MSG_INVALID_RESPONSE}: key error {e}"
raise CdsException(msg)
raise StoreException(msg)
except TypeError as e:
msg = f"{MSG_INVALID_RESPONSE}: type error {e}"
raise CdsException(msg)
raise StoreException(msg)
return records
......
......@@ -5,7 +5,7 @@ from .basestore import (BaseStore,
MSG_HTTP_DECODE,
MSG_NO_IDS,
MSG_INVALID_RESPONSE)
from .exception import CdsException
from .exception import StoreException
CDS_SEARCH_KEYS = ("req", "cc", "c", "ec", "p", "f", "rg", "sf", "so", "sp",
"rm", "of", "ot", "as", "p1", "f1", "m1", "op1", "p2", "f2",
......@@ -38,7 +38,7 @@ class InvenioStore(BaseStore):
Define the maximum number of trial
Raises:
CdsException:
StoreException:
* invalid host
"""
......@@ -48,7 +48,7 @@ class InvenioStore(BaseStore):
max_retries=3):
if host not in ("cds.cern.ch", "old.inspirehep.net"):
raise CdsException(MSG_INVALID_HOST)
raise StoreException(MSG_INVALID_HOST)
super().__init__(api_record=f"https://{host}/record",
api_search=f"https://{host}/search",
......@@ -71,7 +71,7 @@ class InvenioStore(BaseStore):
* None when the field is not found
Raises:
CdsException:
StoreException:
* the server return an HTTP error.
* JSON object could not be decoded.
......@@ -84,7 +84,7 @@ class InvenioStore(BaseStore):
obj = rep.json()
except ValueError:
raise CdsException(MSG_HTTP_DECODE)
raise StoreException(MSG_HTTP_DECODE)
if isinstance(obj, list) and len(obj) == 1:
return obj[0][fieldname]
......@@ -108,7 +108,7 @@ class InvenioStore(BaseStore):
* The list is empty when the request failed on the server.
Raises:
CdsException::
StoreException::
* keyword argument is invalid;
* the server return an HTTP error;
* JSON object can't be decoded;
......@@ -117,7 +117,7 @@ class InvenioStore(BaseStore):
"""
for k in kwargs:
if k not in CDS_SEARCH_KEYS:
raise CdsException(MSG_WRONG_KEYWORD, k)
raise StoreException(MSG_WRONG_KEYWORD, k)
ids = []
scan = True
......@@ -144,13 +144,13 @@ class InvenioStore(BaseStore):
# check that the list of identifier is well form
# [1291068, 1352722, 1376692] or [1493820] or []
if len(list(filter(lambda x: not isinstance(x, int), li))) > 0:
raise CdsException(MSG_NO_IDS)
raise StoreException(MSG_NO_IDS)
ids.extend(li)
# trigger when the JSON object cannot be decoded
except ValueError as e:
raise CdsException(e)
raise StoreException(e)
if len(li) != N_IDS:
scan = False
......@@ -169,7 +169,7 @@ class InvenioStore(BaseStore):
the record data (recjson).
Raises:
CdsException:
StoreException:
* the server return an HTTP error.
* JSON object could not be decoded.
* more than one record
......@@ -182,7 +182,7 @@ class InvenioStore(BaseStore):
obj = rep.json()
except ValueError:
raise CdsException(MSG_HTTP_DECODE)
raise StoreException(MSG_HTTP_DECODE)
if isinstance(obj, dict):
return obj
......@@ -190,7 +190,7 @@ class InvenioStore(BaseStore):
if isinstance(obj, list) and len(obj) == 1:
return obj[0]
raise CdsException(MSG_INVALID_RESPONSE)
raise StoreException(MSG_INVALID_RESPONSE)
def search_parameters(self, collection, year_start=None, year_end=None):
"""Build the (key, value) pairs to steer the search for a collection.
......
......@@ -31,7 +31,7 @@ import re
from graph_tools import mplstyle
from matplotlib.backends.backend_pdf import PdfPages
from plugin_dbui import get_id
from store_tools import CdsException, InvenioStore
from store_tools import InvenioStore, StoreException
CSVFN = "/opt/web2py/applications/limbra/scripts/citations.csv"
PDFFN = "/opt/web2py/applications/limbra/scripts/citations.pdf"
......@@ -75,7 +75,7 @@ def collect_citations():
writer.writerow((rowid, url, citations))
logging.debug("FOO")
except (CdsException, ValueError) as e:
except (StoreException, ValueError) as e:
logging.info(f" error {e}")
pass
......
......@@ -5,12 +5,12 @@ Test methods of the class InveniStore
"""
import pytest
from store_tools.exception import CdsException
from store_tools.exception import StoreException
from store_tools.factory import build_store
def test_build_store_exception_01001():
with pytest.raises(CdsException):
with pytest.raises(StoreException):
build_store("inspirehep", shelf="foo")
......@@ -106,7 +106,7 @@ def test_get_record_ins_literature_01025():
# new inspirehep interface (March 2020)
# record has been deleted
store = build_store("inspirehep.net", shelf="literature")
with pytest.raises(CdsException):
with pytest.raises(StoreException):
store.get_record(1744709)
......@@ -125,14 +125,14 @@ def test_get_field_ins_01031():
# new inspirehep interface (March 2020)
# record has been deleted
store = build_store("inspirehep.net", shelf="literature")
with pytest.raises(CdsException):
with pytest.raises(StoreException):
store.get_field(1744709, "citation_count")
def test_get_field_ins_01032():
# new inspirehep interface (March 2020)
store = build_store("inspirehep.net", shelf="literature")
with pytest.raises(CdsException):
with pytest.raises(StoreException):
store.get_field(1319638, "foo")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment