2019-07-26 22:14:10 +02:00
|
|
|
|
import pandas as pd
|
|
|
|
|
|
2019-07-28 16:47:28 +02:00
|
|
|
|
|
2019-07-26 22:14:10 +02:00
|
|
|
|
def issn(field):
|
|
|
|
|
"""Check if an ISSN is valid.
|
|
|
|
|
|
|
|
|
|
Prints the ISSN if invalid.
|
|
|
|
|
|
|
|
|
|
stdnum's is_valid() function never raises an exception.
|
|
|
|
|
|
|
|
|
|
See: https://arthurdejong.org/python-stdnum/doc/1.11/index.html#stdnum.module.is_valid
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from stdnum import issn
|
|
|
|
|
|
|
|
|
|
# Skip fields with missing values
|
|
|
|
|
if pd.isna(field):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Try to split multi-value field on "||" separator
|
2019-08-29 00:10:39 +02:00
|
|
|
|
for value in field.split("||"):
|
2019-07-26 22:14:10 +02:00
|
|
|
|
|
|
|
|
|
if not issn.is_valid(value):
|
2019-08-29 00:10:39 +02:00
|
|
|
|
print(f"Invalid ISSN: {value}")
|
2019-07-26 22:14:10 +02:00
|
|
|
|
|
2019-07-27 00:28:08 +02:00
|
|
|
|
return field
|
|
|
|
|
|
2019-07-26 22:14:10 +02:00
|
|
|
|
|
|
|
|
|
def isbn(field):
|
|
|
|
|
"""Check if an ISBN is valid.
|
|
|
|
|
|
|
|
|
|
Prints the ISBN if invalid.
|
|
|
|
|
|
|
|
|
|
stdnum's is_valid() function never raises an exception.
|
|
|
|
|
|
|
|
|
|
See: https://arthurdejong.org/python-stdnum/doc/1.11/index.html#stdnum.module.is_valid
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from stdnum import isbn
|
|
|
|
|
|
2019-07-26 22:44:58 +02:00
|
|
|
|
# Skip fields with missing values
|
|
|
|
|
if pd.isna(field):
|
|
|
|
|
return
|
|
|
|
|
|
2019-07-26 22:14:10 +02:00
|
|
|
|
# Try to split multi-value field on "||" separator
|
2019-08-29 00:10:39 +02:00
|
|
|
|
for value in field.split("||"):
|
2019-07-26 22:14:10 +02:00
|
|
|
|
|
|
|
|
|
if not isbn.is_valid(value):
|
2019-08-29 00:10:39 +02:00
|
|
|
|
print(f"Invalid ISBN: {value}")
|
2019-07-26 22:48:24 +02:00
|
|
|
|
|
2019-07-27 00:28:08 +02:00
|
|
|
|
return field
|
|
|
|
|
|
2019-07-26 22:48:24 +02:00
|
|
|
|
|
2020-01-16 11:35:11 +01:00
|
|
|
|
def separators(field, field_name):
|
2019-07-26 22:48:24 +02:00
|
|
|
|
"""Check for invalid multi-value separators (ie "|" or "|||").
|
|
|
|
|
|
|
|
|
|
Prints the field with the invalid multi-value separator.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
# Skip fields with missing values
|
|
|
|
|
if pd.isna(field):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Try to split multi-value field on "||" separator
|
2019-08-29 00:10:39 +02:00
|
|
|
|
for value in field.split("||"):
|
2019-07-26 22:48:24 +02:00
|
|
|
|
|
|
|
|
|
# After splitting, see if there are any remaining "|" characters
|
2019-08-29 00:10:39 +02:00
|
|
|
|
match = re.findall(r"^.*?\|.*$", value)
|
2019-07-26 22:48:24 +02:00
|
|
|
|
|
2019-07-29 15:16:30 +02:00
|
|
|
|
if match:
|
2020-01-16 11:35:11 +01:00
|
|
|
|
print(f"Invalid multi-value separator ({field_name}): {field}")
|
2019-07-27 00:28:08 +02:00
|
|
|
|
|
|
|
|
|
return field
|
2019-07-28 15:11:36 +02:00
|
|
|
|
|
|
|
|
|
|
2019-08-21 14:31:12 +02:00
|
|
|
|
def date(field, field_name):
|
2019-07-28 15:11:36 +02:00
|
|
|
|
"""Check if a date is valid.
|
|
|
|
|
|
|
|
|
|
In DSpace the issue date is usually 1990, 1990-01, or 1990-01-01, but it
|
|
|
|
|
could technically even include time as long as it is ISO8601.
|
|
|
|
|
|
|
|
|
|
Also checks for other invalid cases like missing and multiple dates.
|
|
|
|
|
|
|
|
|
|
Prints the date if invalid.
|
|
|
|
|
"""
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
if pd.isna(field):
|
2019-08-29 00:10:39 +02:00
|
|
|
|
print(f"Missing date ({field_name}).")
|
2019-07-28 15:11:36 +02:00
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Try to split multi-value field on "||" separator
|
2019-08-29 00:10:39 +02:00
|
|
|
|
multiple_dates = field.split("||")
|
2019-07-28 15:11:36 +02:00
|
|
|
|
|
|
|
|
|
# We don't allow multi-value date fields
|
|
|
|
|
if len(multiple_dates) > 1:
|
2019-08-29 00:10:39 +02:00
|
|
|
|
print(f"Multiple dates not allowed ({field_name}): {field}")
|
2019-07-28 15:11:36 +02:00
|
|
|
|
|
|
|
|
|
return field
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# Check if date is valid YYYY format
|
2019-08-29 00:10:39 +02:00
|
|
|
|
datetime.strptime(field, "%Y")
|
2019-07-28 15:11:36 +02:00
|
|
|
|
|
|
|
|
|
return field
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# Check if date is valid YYYY-MM format
|
2019-08-29 00:10:39 +02:00
|
|
|
|
datetime.strptime(field, "%Y-%m")
|
2019-07-28 15:11:36 +02:00
|
|
|
|
|
|
|
|
|
return field
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# Check if date is valid YYYY-MM-DD format
|
2019-08-29 00:10:39 +02:00
|
|
|
|
datetime.strptime(field, "%Y-%m-%d")
|
2019-07-28 15:11:36 +02:00
|
|
|
|
|
|
|
|
|
return field
|
|
|
|
|
except ValueError:
|
2019-08-29 00:10:39 +02:00
|
|
|
|
print(f"Invalid date ({field_name}): {field}")
|
2019-07-29 16:08:49 +02:00
|
|
|
|
|
2019-07-29 16:40:14 +02:00
|
|
|
|
return field
|
|
|
|
|
|
2019-07-29 16:08:49 +02:00
|
|
|
|
|
2019-08-09 00:22:59 +02:00
|
|
|
|
def suspicious_characters(field, field_name):
|
2019-07-29 16:08:49 +02:00
|
|
|
|
"""Warn about suspicious characters.
|
|
|
|
|
|
|
|
|
|
Look for standalone characters that could indicate encoding or copy/paste
|
|
|
|
|
errors for languages with accents. For example: foreˆt should be forêt.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Skip fields with missing values
|
|
|
|
|
if pd.isna(field):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# List of suspicious characters, for example: ́ˆ~`
|
2019-08-29 00:10:39 +02:00
|
|
|
|
suspicious_characters = ["\u00B4", "\u02C6", "\u007E", "\u0060"]
|
2019-07-29 16:08:49 +02:00
|
|
|
|
|
|
|
|
|
for character in suspicious_characters:
|
2019-08-09 00:22:59 +02:00
|
|
|
|
# Find the position of the suspicious character in the string
|
|
|
|
|
suspicious_character_position = field.find(character)
|
|
|
|
|
|
|
|
|
|
# Python returns -1 if there is no match
|
|
|
|
|
if suspicious_character_position != -1:
|
|
|
|
|
# Create a temporary new string starting from the position of the
|
|
|
|
|
# suspicious character
|
|
|
|
|
field_subset = field[suspicious_character_position:]
|
|
|
|
|
|
|
|
|
|
# Print part of the metadata value starting from the suspicious
|
|
|
|
|
# character and spanning enough of the rest to give a preview,
|
|
|
|
|
# but not too much to cause the line to break in terminals with
|
|
|
|
|
# a default of 80 characters width.
|
2019-08-29 00:10:39 +02:00
|
|
|
|
suspicious_character_msg = (
|
|
|
|
|
f"Suspicious character ({field_name}): {field_subset}"
|
|
|
|
|
)
|
|
|
|
|
print(f"{suspicious_character_msg:1.80}")
|
2019-07-29 16:08:49 +02:00
|
|
|
|
|
|
|
|
|
return field
|
2019-07-29 17:59:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def language(field):
|
2019-09-26 06:44:39 +02:00
|
|
|
|
"""Check if a language is valid ISO 639-1 (alpha 2) or ISO 639-3 (alpha 3).
|
2019-07-29 17:59:42 +02:00
|
|
|
|
|
|
|
|
|
Prints the value if it is invalid.
|
|
|
|
|
"""
|
|
|
|
|
|
2019-07-30 15:39:26 +02:00
|
|
|
|
from pycountry import languages
|
2019-07-29 17:59:42 +02:00
|
|
|
|
|
|
|
|
|
# Skip fields with missing values
|
|
|
|
|
if pd.isna(field):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# need to handle "Other" values here...
|
|
|
|
|
|
|
|
|
|
# Try to split multi-value field on "||" separator
|
2019-08-29 00:10:39 +02:00
|
|
|
|
for value in field.split("||"):
|
2019-07-29 17:59:42 +02:00
|
|
|
|
|
|
|
|
|
# After splitting, check if language value is 2 or 3 characters so we
|
2019-09-26 06:44:39 +02:00
|
|
|
|
# can check it against ISO 639-1 or ISO 639-3 accordingly.
|
2019-07-29 17:59:42 +02:00
|
|
|
|
if len(value) == 2:
|
2019-07-30 15:39:26 +02:00
|
|
|
|
if not languages.get(alpha_2=value):
|
2019-09-11 15:36:53 +02:00
|
|
|
|
print(f"Invalid ISO 639-1 language: {value}")
|
2019-07-29 17:59:42 +02:00
|
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
elif len(value) == 3:
|
2019-07-30 15:39:26 +02:00
|
|
|
|
if not languages.get(alpha_3=value):
|
2019-09-26 06:44:39 +02:00
|
|
|
|
print(f"Invalid ISO 639-3 language: {value}")
|
2019-07-29 17:59:42 +02:00
|
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
else:
|
2019-08-29 00:10:39 +02:00
|
|
|
|
print(f"Invalid language: {value}")
|
2019-07-29 17:59:42 +02:00
|
|
|
|
|
|
|
|
|
return field
|
2019-07-29 23:30:31 +02:00
|
|
|
|
|
|
|
|
|
|
2019-08-01 22:51:58 +02:00
|
|
|
|
def agrovoc(field, field_name):
|
2019-07-29 23:30:31 +02:00
|
|
|
|
"""Check subject terms against AGROVOC REST API.
|
|
|
|
|
|
2019-08-01 22:51:58 +02:00
|
|
|
|
Function constructor expects the field as well as the field name because
|
|
|
|
|
many fields can now be validated against AGROVOC and we want to be able
|
|
|
|
|
to inform the user in which field the invalid term is.
|
|
|
|
|
|
2019-07-29 23:30:31 +02:00
|
|
|
|
Logic copied from agrovoc-lookup.py.
|
|
|
|
|
|
|
|
|
|
See: https://github.com/ilri/DSpace/blob/5_x-prod/agrovoc-lookup.py
|
|
|
|
|
|
|
|
|
|
Prints a warning if the value is invalid.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from datetime import timedelta
|
|
|
|
|
import requests
|
|
|
|
|
import requests_cache
|
|
|
|
|
|
|
|
|
|
# Skip fields with missing values
|
|
|
|
|
if pd.isna(field):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Try to split multi-value field on "||" separator
|
2019-08-29 00:10:39 +02:00
|
|
|
|
for value in field.split("||"):
|
|
|
|
|
request_url = (
|
|
|
|
|
f"http://agrovoc.uniroma2.it/agrovoc/rest/v1/agrovoc/search?query={value}"
|
|
|
|
|
)
|
2019-08-21 15:35:29 +02:00
|
|
|
|
|
|
|
|
|
# enable transparent request cache with thirty days expiry
|
|
|
|
|
expire_after = timedelta(days=30)
|
2019-08-29 00:10:39 +02:00
|
|
|
|
requests_cache.install_cache(
|
|
|
|
|
"agrovoc-response-cache", expire_after=expire_after
|
|
|
|
|
)
|
2019-08-21 15:35:29 +02:00
|
|
|
|
|
|
|
|
|
request = requests.get(request_url)
|
|
|
|
|
|
|
|
|
|
# prune old cache entries
|
|
|
|
|
requests_cache.core.remove_expired_responses()
|
|
|
|
|
|
|
|
|
|
if request.status_code == requests.codes.ok:
|
|
|
|
|
data = request.json()
|
|
|
|
|
|
|
|
|
|
# check if there are any results
|
2019-08-29 00:10:39 +02:00
|
|
|
|
if len(data["results"]) == 0:
|
|
|
|
|
print(f"Invalid AGROVOC ({field_name}): {value}")
|
2019-07-29 23:30:31 +02:00
|
|
|
|
|
|
|
|
|
return field
|
2019-08-10 22:41:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def filename_extension(field):
|
|
|
|
|
"""Check filename extension.
|
|
|
|
|
|
|
|
|
|
CSVs with a 'filename' column are likely meant as input for the SAFBuilder
|
|
|
|
|
tool, which creates a Simple Archive Format bundle for importing metadata
|
|
|
|
|
with accompanying PDFs or other files into DSpace.
|
|
|
|
|
|
|
|
|
|
This check warns if a filename has an uncommon extension (that is, other
|
|
|
|
|
than .pdf, .xls(x), .doc(x), ppt(x), case insensitive).
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
# Skip fields with missing values
|
|
|
|
|
if pd.isna(field):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Try to split multi-value field on "||" separator
|
2019-08-29 00:10:39 +02:00
|
|
|
|
values = field.split("||")
|
2019-08-10 22:41:16 +02:00
|
|
|
|
|
|
|
|
|
# List of common filename extentions
|
2019-08-29 00:10:39 +02:00
|
|
|
|
common_filename_extensions = [
|
|
|
|
|
".pdf",
|
|
|
|
|
".doc",
|
|
|
|
|
".docx",
|
|
|
|
|
".ppt",
|
|
|
|
|
".pptx",
|
|
|
|
|
".xls",
|
|
|
|
|
".xlsx",
|
|
|
|
|
]
|
2019-08-10 22:41:16 +02:00
|
|
|
|
|
|
|
|
|
# Iterate over all values
|
|
|
|
|
for value in values:
|
|
|
|
|
# Assume filename extension does not match
|
|
|
|
|
filename_extension_match = False
|
|
|
|
|
|
|
|
|
|
for filename_extension in common_filename_extensions:
|
|
|
|
|
# Check for extension at the end of the filename
|
2019-08-29 00:10:39 +02:00
|
|
|
|
pattern = re.escape(filename_extension) + r"$"
|
2019-08-10 22:41:16 +02:00
|
|
|
|
match = re.search(pattern, value, re.IGNORECASE)
|
|
|
|
|
|
|
|
|
|
if match is not None:
|
|
|
|
|
# Register the match and stop checking for this filename
|
|
|
|
|
filename_extension_match = True
|
|
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
2019-08-10 22:52:53 +02:00
|
|
|
|
if filename_extension_match is False:
|
2019-08-29 00:10:39 +02:00
|
|
|
|
print(f"Filename with uncommon extension: {value}")
|
2019-08-10 22:41:16 +02:00
|
|
|
|
|
|
|
|
|
return field
|