Skip to content

Commit a68b06c

Browse files
committed
more exceptions for invalid formats
also include string that isn't validating
1 parent 2dc637c commit a68b06c

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

allofplos/article_class.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def doi(self, d):
107107
instantiating the article object.
108108
"""
109109
if validate_doi(d) is False:
110-
raise Exception("Invalid format for PLOS DOI")
110+
raise Exception("Invalid format for PLOS DOI: {}".format(d))
111111
self.reset_memoized_attrs()
112112
self._doi = d
113113

allofplos/transformations.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ def filename_to_doi(filename):
9595
:return: full unique identifier for a PLOS article
9696
"""
9797
filename = os.path.basename(filename)
98-
if correction in filename and validate_filename(filename):
98+
if not validate_filename(filename):
99+
raise Exception("Invalid format for PLOS filename: {}".format(filename))
100+
elif correction in filename:
99101
article = 'annotation/' + filename.split('.', 4)[2]
100102
doi = PREFIX + article
101-
elif validate_filename(filename):
102-
doi = PREFIX + os.path.splitext(filename)[0]
103103
else:
104-
doi = ''
104+
doi = PREFIX + os.path.splitext(filename)[0]
105105
return doi
106106

107107

@@ -154,6 +154,8 @@ def doi_to_url(doi):
154154
:param doi: full unique identifier for a PLOS article
155155
:return: online location of a PLOS article's XML
156156
"""
157+
if validate_doi(doi) is False:
158+
raise Exception("Invalid format for PLOS DOI: {}".format(doi))
157159
journal = Journal.doi_to_journal(doi)
158160
base_page = _get_base_page(journal)
159161
return ''.join([base_page, 'article/file?id=', doi, URL_SUFFIX])
@@ -173,13 +175,12 @@ def doi_to_path(doi, directory=None):
173175
"""
174176
if directory is None:
175177
directory = get_corpus_dir()
176-
if doi.startswith(ANNOTATION_DOI) and validate_doi(doi):
178+
if not validate_doi(doi):
179+
raise Exception("Invalid format for PLOS DOI: {}".format(doi))
180+
elif doi.startswith(ANNOTATION_DOI):
177181
article_file = os.path.join(directory, "plos.correction." + doi.split('/')[-1] + SUFFIX_LOWER)
178-
elif validate_doi(doi):
182+
else:
179183
article_file = os.path.join(directory, doi.lstrip(PREFIX) + SUFFIX_LOWER)
180-
# NOTE: The following check is weird, a DOI should never validate as a file name.
181-
elif validate_filename(doi):
182-
article_file = doi
183184
return article_file
184185

185186

0 commit comments

Comments
 (0)