Skip to content

Commit

Permalink
Pass codemeta identifier to resolve function
Browse files Browse the repository at this point in the history
This makes initializing the resolver with a context obsolete.
  • Loading branch information
zyzzyxdonta committed Oct 18, 2023
1 parent 66c0cc6 commit 781cd40
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/hermes/commands/deposit/invenio.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
_log = logging.getLogger("cli.deposit.invenio")


# TODO: Update docstrings
class InvenioResolver:
def __init__(self, ctx: CodeMetaContext):
self.ctx = ctx
def __init__(self):
self.session = requests.Session()
self.session.headers.update({"User-Agent": hermes_user_agent})

# TODO: Pass codemeta.identifier rather than context
def resolve_latest_id(self, site_url, record_id=None, doi=None) -> t.Tuple[str, dict]:
def resolve_latest_id(
self, site_url, record_id=None, doi=None, codemeta_identifier=None
) -> t.Tuple[str, dict]:
"""
Using the given configuration and metadata, figure out the latest record id.
Expand All @@ -52,15 +53,12 @@ def resolve_latest_id(self, site_url, record_id=None, doi=None) -> t.Tuple[str,

if record_id is None:
if doi is None:
try:
if codemeta_identifier is not None:
# TODO: There might be more semantic in the codemeta.identifier... (also see schema.org)
identifier = self.ctx['codemeta.identifier']
if identifier.startswith('https://doi.org/'):
doi = identifier[16:]
elif identifier.startswith('http://dx.doi.org/'):
doi = identifier[18:]
except KeyError:
pass
if codemeta_identifier.startswith('https://doi.org/'):
doi = codemeta_identifier[16:]
elif codemeta_identifier.startswith('http://dx.doi.org/'):
doi = codemeta_identifier[18:]

if doi is not None:
# If we got a DOI, resolve it (using doi.org) into a Invenio URL ... and extract the record id.
Expand Down Expand Up @@ -126,7 +124,7 @@ class InvenioDepositPlugin(BaseDepositPlugin):

def __init__(self, click_ctx: click.Context, ctx: CodeMetaContext, resolver=None) -> None:
super().__init__(click_ctx, ctx)
self.resolver = resolver or InvenioResolver(ctx)
self.resolver = resolver or InvenioResolver()

self.session = requests.Session()
self.session.headers.update({"User-Agent": hermes_user_agent})
Expand Down Expand Up @@ -180,8 +178,13 @@ def prepare(self) -> None:
rec_id = self.config.get('record_id')
doi = self.config.get('doi')

try:
codemeta_identifier = self.ctx["codemeta.identifier"]
except KeyError:
codemeta_identifier = None

rec_id, rec_meta = self.resolver.resolve_latest_id(
site_url, record_id=rec_id, doi=doi
site_url, record_id=rec_id, doi=doi, codemeta_identifier=codemeta_identifier
)

version = self.ctx["codemeta"].get("version")
Expand Down

0 comments on commit 781cd40

Please sign in to comment.