[#95] refactored exception into standardize_doi
This commit is contained in:
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,
|
||||||
@ -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.',
|
parser.add_argument('-k', '--citekey', help='citekey associated with the paper;\nif not provided, one will be generated automatically.',
|
||||||
default=None)
|
default=None)
|
||||||
parser.add_argument('-L', '--link', action='store_false', dest='copy', default=True,
|
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,
|
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
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
@ -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 """
|
|
||||||
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+)')
|
|
||||||
)
|
|
||||||
|
|
||||||
for doi_regex in doi_regexes:
|
:returns standardized doi
|
||||||
match = doi_regex.search(doi)
|
"""
|
||||||
if match:
|
|
||||||
new_doi = match.group(0)
|
doi_regexes = (
|
||||||
break
|
'(10\.\d{4,9}/[-._;()/:A-z0-9\>\<]+)',
|
||||||
else:
|
'(10.1002/[^\s]+)',
|
||||||
new_doi = None
|
'(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))
|
||||||
|
|
||||||
|
match = doi_pattern.search(doi)
|
||||||
|
if not match:
|
||||||
|
raise ValueError("Not a valid doi: %s", doi)
|
||||||
|
new_doi = match.group(0)
|
||||||
|
|
||||||
return new_doi
|
return new_doi
|
||||||
|
Loading…
x
Reference in New Issue
Block a user