-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils2.py
82 lines (71 loc) · 2.59 KB
/
utils2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 CERN.
#
# Zenodo-Uploader is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
import csv
import json
import os
from os.path import basename, dirname, exists, isfile, join
import config
def iter_uploads(path):
"""Iterate CSV files in a direcotry."""
for p in os.listdir(path):
entry = join(path, p)
if not entry.startswith('.') and isfile(entry):
for row in iter_csv(entry):
yield make_upload(entry, row)
def iter_csv(path):
"""Iterate over rows in a CSV file."""
with open(path) as fp:
reader = csv.reader(fp, delimiter=',', quotechar='"')
for row in reader:
yield {'barcode': row[0], 'name': row[1], 'family': row[2]}
def make_upload(path, row):
"""Make an upload from a row in CSV file."""
metadata = {
'upload_type': 'image',
'image_type': 'photo',
'title': '{name} ({barcode})'.format(**row),
'creators': [{'name': 'Meise Botanic Garden'}],
'description': (
'Belgium Herbarium image of <a href="https://www.plantentuin'
'meise.be">Meise Botanic Garden</a>.'),
'access_right': 'open',
'license': 'CC-BY-SA-4.0',
# 'communities': [{'identifier': 'belgiumherbarium'}],
'grants': [{'id': '777483'}],
'related_identifiers': [{
'identifier': 'http://www.botanicalcollections.be/'
'specimen/{barcode}'.format(**row),
'relation': 'isAlternateIdentifier',
}],
'keywords': [
'Biodiversity',
'Taxonomy',
'Terrestrial',
'Herbarium',
row['family'],
],
'imprint_publisher': 'Meise Botanic Garden Herbarium'
}
# Find files for this upload
files = []
for path_tpl, name_tpl in config.FILES_PATH_TEMPLATES:
# Render each template into a a) full path on disk and b) name of file
# on Zenodo.
fpath = path_tpl.format(**row)
name = name_tpl.format(basename=basename(fpath), **row)
# Check if the file exists, and if so add it to the list of files
# to upload.
if exists(fpath) and isfile(fpath):
files.append((fpath, name))
if len(files) == 0:
return {'error': 'Missing files.', 'path': path + '#' + row['barcode']}
return {
'metadata': metadata,
'files': files,
'path': path,
'id': '{basename}#{barcode}'.format(basename=basename(path), **row),
}