[#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 utils
class ValidateDOI(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
doi = values
new_doi = utils.standardize_doi(doi)
if not new_doi:
raise ValueError("Not a valid doi: %s", doi)
setattr(namespace, self.dest, new_doi)
def parser(subparsers, conf):
parser = subparsers.add_parser('add', help='add a paper to the repository')
parser.add_argument('bibfile', nargs='?', default=None,
@ -30,9 +30,9 @@ def parser(subparsers, conf):
parser.add_argument('-k', '--citekey', help='citekey associated with the paper;\nif not provided, one will be generated automatically.',
default=None)
parser.add_argument('-L', '--link', action='store_false', dest='copy', default=True,
help="don't copy document files, just create a link.")
help="don't copy document files, just create a link.")
parser.add_argument('-M', '--move', action='store_true', dest='move', default=False,
help="move document instead of of copying (ignored if --link).")
help="move document instead of of copying (ignored if --link).")
return parser

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

Loading…
Cancel
Save