Exemplo n.º 1
0
def standardize_citekey(citekey, warn_if_changed=False):
    """
    Standardize citation keys based on their source
    """
    source, identifier = citekey.split(':', 1)

    if source == 'doi':
        if identifier.startswith('10/'):
            from manubot.cite.doi import expand_short_doi
            try:
                identifier = expand_short_doi(identifier)
            except Exception as error:
                # If DOI shortening fails, return the unshortened DOI.
                # DOI metadata lookup will eventually fail somewhere with
                # appropriate error handling, as opposed to here.
                logging.error(f'Error in expand_short_doi for {identifier} '
                              f'due to a {error.__class__.__name__}:\n{error}')
                logging.info(error, exc_info=True)
        identifier = identifier.lower()

    if source == 'isbn':
        from isbnlib import to_isbn13
        identifier = to_isbn13(identifier)

    standard_citekey = f'{source}:{identifier}'
    if warn_if_changed and citekey != standard_citekey:
        logging.warning(
            f'standardize_citekey expected citekey to already be standardized.\n'
            f'Instead citekey was changed from {citekey!r} to {standard_citekey!r}'
        )
    return standard_citekey
Exemplo n.º 2
0
def test_expand_short_doi():
    doi = expand_short_doi('10/b6vnmd')
    assert doi == "10.1016/s0933-3657(96)00367-3"
Exemplo n.º 3
0
def test_expand_short_doi_invalid():
    with pytest.raises(ValueError,
                       match='Handle not found. Double check short_doi'):
        expand_short_doi('10/b6vnmdxxxxxx')
Exemplo n.º 4
0
def test_expand_short_doi_not_short():
    with pytest.raises(ValueError, match='shortDOIs start with `10/`'):
        expand_short_doi('10.1016/S0933-3657(96)00367-3')
Exemplo n.º 5
0
 def canonic(self):
     from manubot.cite.doi import expand_short_doi
     if self.identifier.startswith('10/'):
         self.identifier = expand_short_doi(self.identifier)
     return self