[#95] refactored exception into standardize_doi

main
Bill Flynn 7 years ago
parent e2ad39ca08
commit aea58dea29

@ -10,14 +10,14 @@ from .. import color
from .. import pretty from .. import pretty
from .. import utils from .. import utils
class ValidateDOI(argparse.Action): class ValidateDOI(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None): def __call__(self, parser, namespace, values, option_string=None):
doi = values doi = values
new_doi = utils.standardize_doi(doi) new_doi = utils.standardize_doi(doi)
if not new_doi:
raise ValueError("Not a valid doi: %s", doi)
setattr(namespace, self.dest, new_doi) setattr(namespace, self.dest, new_doi)
def parser(subparsers, conf): def parser(subparsers, conf):
parser = subparsers.add_parser('add', help='add a paper to the repository') parser = subparsers.add_parser('add', help='add a paper to the repository')
parser.add_argument('bibfile', nargs='?', default=None, parser.add_argument('bibfile', nargs='?', default=None,

@ -1,10 +1,10 @@
# Function here may belong somewhere else. In the mean time... # Function here may belong somewhere else. In the mean time...
import re import re
from . import color from . import color
from . import pretty from . import pretty
def resolve_citekey(repo, citekey, ui=None, exit_on_fail=True): def resolve_citekey(repo, citekey, ui=None, exit_on_fail=True):
"""Check that a citekey exists, or autocompletes it if not ambiguous.""" """Check that a citekey exists, or autocompletes it if not ambiguous."""
""" :returns found citekey """ """ :returns found citekey """
@ -47,6 +47,7 @@ def resolve_citekey_list(repo, citekeys, ui=None, exit_on_fail=True):
else: else:
return keys return keys
def standardize_doi(doi): def standardize_doi(doi):
""" """
Given a putative doi, attempts to always return it in the form of Given a putative doi, attempts to always return it in the form of
@ -60,22 +61,22 @@ def standardize_doi(doi):
and attempts to verify doi adherence to DOI handbook standards and and attempts to verify doi adherence to DOI handbook standards and
crossref.org advice: crossref.org advice:
https://www.doi.org/doi_handbook/2_Numbering.html https://www.doi.org/doi_handbook/2_Numbering.html
https://www.crossref.org/blog/dois-and-matching-regular-expressions/""" https://www.crossref.org/blog/dois-and-matching-regular-expressions/
""" :returns standardized doi """
:returns standardized doi
"""
doi_regexes = ( doi_regexes = (
re.compile(r'(10\.\d{4,9}/[-._;()/:A-z0-9\>\<]+)'), '(10\.\d{4,9}/[-._;()/:A-z0-9\>\<]+)',
re.compile(r'(10.1002/[^\s]+)'), '(10.1002/[^\s]+)',
re.compile(r'(10\.\d{4}/\d+-\d+X?(\d+)\d+<[\d\w]+:[\d\w]*>\d+.\d+.\w+;\d)'), '(10\.\d{4}/\d+-\d+X?(\d+)\d+<[\d\w]+:[\d\w]*>\d+.\d+.\w+;\d)',
re.compile(r'(10\.1021/\w\w\d+\+)'), '(10\.1021/\w\w\d+\+)',
re.compile(r'(10\.1207/[\w\d]+\&\d+_\d+)') '(10\.1207/[\w\d]+\&\d+_\d+)')
) doi_pattern = re.compile('|'.join(doi_regexes))
for doi_regex in doi_regexes: match = doi_pattern.search(doi)
match = doi_regex.search(doi) if not match:
if match: raise ValueError("Not a valid doi: %s", doi)
new_doi = match.group(0) new_doi = match.group(0)
break
else:
new_doi = None
return new_doi return new_doi

Loading…
Cancel
Save