Skip to content

Commit

Permalink
quotes in CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-pol committed Nov 30, 2023
1 parent f41e136 commit e7a8a76
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
15 changes: 9 additions & 6 deletions src/datastation/dataverse/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ def __init__(self, dataverse_client: DataverseClient, dry_run: bool = False):
self.dry_run = dry_run

def update_metadata(self, data: dict):
if 'rest.column' in data.keys():
logging.error(data)
raise Exception("Quoting problem or too many values.")
logging.debug(data)
all_fields = []
if '@' in ' '.join(data.keys()):
raise Exception("Compound fields not supported")
for key in [key for key in data.keys() if key != 'PID' and not data[key].startswith('[')]:
all_fields.append({'typeName': key, 'value': data[key]})
for key in [key for key in data.keys() if key != 'PID' and data[key].startswith('[')]:
logging.debug('-------' + data[key] + '=======')
all_fields.append({'typeName': key, 'value': json.loads(data[key])})
all_fields = []
for key in [key for key in data.keys() if key != 'PID' and data[key] is not None]:
if data[key].startswith('['):
all_fields.append({'typeName': key, 'value': (json.loads(data[key]))})
else:
all_fields.append({'typeName': key, 'value': data[key]})
logging.debug(all_fields)
dataset_api = self.dataverse_client.dataset(data['PID'])
result = dataset_api.edit_metadata(data=(json.dumps({'fields': all_fields})), dry_run=self.dry_run)
Expand Down
16 changes: 11 additions & 5 deletions src/datastation/dv_dataset_edit_metadata.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import csv
import re
from argparse import ArgumentParser
from csv import DictReader
Expand All @@ -19,12 +20,16 @@ def main():
"The other columns MUST have a typeName, as for the --value argument.")
parser.add_argument('-v', '--value', action='append',
help="At least once in combination with a PID, not allowed in combination with CSV file. "
"The new values for fields formatted as "
"<typeName>=<value> for example title='New title'. "
"The new values for fields formatted as <typeName>=<value>. "
"for example: title='New title'. "
"A subfield in a compound field must be prefixed with "
"the typeName of the compound field and an @ sign, e.g. "
"the typeName of the compound field and an @ sign, for example: "
"--value author@authorName='the name' "
"--value author@authorAffiliation='the organization'")
"--value author@authorAffiliation='the organization'. "
"The quoting style for repetitive fields is <typeName>='[\"<value>\"]', "
"for example: -v dansRightsHolder='[\"me\",\"O'\"'\"'Neill\"]'. "
"Note that all occurrences of a repetitive field will be replaced. "
"")
add_batch_processor_args(parser, report=False)
add_dry_run_arg(parser)
args = parser.parse_args()
Expand Down Expand Up @@ -59,7 +64,8 @@ def parse_value_args():
if args.value is not None:
parser.error("-v/--value arguments not allowed in combination with CSV file: " + args.pid_or_file)
with open(args.pid_or_file, newline='') as csvfile:
reader = DictReader(csvfile)
reader = DictReader(csvfile, quotechar="'", delimiter=',', quoting=csv.QUOTE_MINIMAL,
skipinitialspace=True, restkey='rest.column', escapechar=None)
validate_fieldnames(reader.fieldnames)
run(reader)
else:
Expand Down

0 comments on commit e7a8a76

Please sign in to comment.