-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.py
149 lines (122 loc) · 6.07 KB
/
upload.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from os.path import join, exists
from os import listdir
import json
from datetime import date
def __write_id_to_results(results_dir_path, key, id):
for result_file in listdir(results_dir_path):
with open(path := join(results_dir_path, result_file), 'r') as result_file:
payload = json.load(result_file)
payload[key] = id
with open(path, 'w') as result_file:
json.dump(payload, result_file)
def __write_variety_id_to_results(results_dir_path, variety_name, id):
for site_type in listdir(results_dir_path):
for fips in listdir(site_type_path := join(results_dir_path, site_type)):
for result_file in listdir(fips_path := join(site_type_path, fips)):
with open(path := join(fips_path, result_file), 'r') as result_file:
payload = json.load(result_file)
if payload.get('name').strip() != variety_name.strip():
continue
payload['variety_id'] = id
with open(path, 'w') as result_file:
json.dump(payload, result_file)
def sites(cli):
for site_type in listdir((sites_basepath := join(cli.args.inpath, 'trial_sites'))):
for fips_json in listdir(site_type_path := join(sites_basepath, site_type)):
with open((path := join(site_type_path, fips_json)), 'r+') as sites_file:
payload = json.load(sites_file)
if payload.get('id') is not None and cli.args.rewrite:
try:
resp = cli.client.site.delete(payload.get('id'))
except Exception as e:
pass
del(payload['id'])
payload['harvest_year_pub_id'] = cli.args.year_id
if payload.get('published_at') in ['', None]:
payload['published_at'] = str(date.today())
try:
if payload.get('id') is None:
resp = cli.client.site.store(payload)
else:
resp = cli.client.site.update(payload.get('id'), payload)
except Exception as e:
cli.write_to_manifest("WRITE_EXCEPTION", path, e)
continue
id = resp.json().get('id')
if id is None:
cli.write_to_manifest(
"WRITE_FAILED", path, resp.status_code, resp.reason, resp.json())
continue
payload['id'] = id
with open((path := join(site_type_path, fips_json)), 'w') as sites_file:
json.dump(payload, sites_file)
if exists(results_path := join(cli.args.inpath, 'results', site_type, fips_json[:5])):
__write_id_to_results(results_path, 'trial_site_id', id)
def varieties(cli):
for variety_file in listdir((varieties_basepath := join(cli.args.inpath, 'varieties'))):
with open((path := join(varieties_basepath, variety_file)), 'r+') as varieties_file:
payload = json.load(varieties_file)
if payload.get('id') is not None and cli.args.rewrite:
try:
cli.client.variety.delete(payload.get('id'))
except Exception:
pass
del(payload['id'])
payload['crop_harvest_year_publication_id'] = cli.args.year_id
payload['aquisition_year'] = cli.args.year
if payload.get('published_at') in ['', None]:
payload['published_at'] = str(date.today())
try:
if payload.get('id') is None:
resp = cli.client.variety.store(payload)
else:
resp = cli.client.variety.update(payload.get('id'), payload)
except Exception as e:
cli.write_to_manifest("WRITE_EXCEPTION", path, e)
continue
id = resp.json().get('id')
if id is None:
cli.write_to_manifest("WRITE_FAILED", path,
resp.status_code, resp.reason, resp.json())
continue
payload['id'] = id
with open((path := join(varieties_basepath, variety_file)), 'w') as varieties_file:
json.dump(payload, varieties_file)
__write_variety_id_to_results(
join(cli.args.inpath, 'results'), payload.get('name'), id)
def results(cli):
for site_type in listdir((results_basepath := join(cli.args.inpath, 'results'))):
for fips in listdir(results_site_type_path := join(results_basepath, site_type)):
for results_filename in listdir(payload_filepath := join(results_site_type_path, fips)):
with open((path := join(payload_filepath, results_filename)), 'r+') as resuts_file:
payload = json.load(resuts_file)
if payload.get('id') is not None and cli.args.rewrite:
try:
cli.client.results.delete(payload.get('id'))
except Exception as e:
pass
del(payload['id'])
if payload.get('trial_site_id') is None or payload.get('variety_id') is None:
cli.write_to_manifest("WRITE_IGNORED",
path, "Missing trial site and/or variety id")
continue
try:
if payload.get('id') is None:
resp = cli.client.results.store(payload)
else:
resp = cli.client.results.update(payload.get('id'), payload)
except Exception as e:
cli.write_to_manifest("WRITE_EXCEPTION", path, e)
continue
id = resp.json().get('id')
if id is None:
cli.write_to_manifest(
"WRITE_FAILED", path, resp.status_code, resp.reason, resp.json())
continue
payload['id'] = id
with open((path := join(payload_filepath, results_filename)), 'w') as resuts_file:
json.dump(payload, resuts_file)
def all(cli):
sites(cli)
varieties(cli)
results(cli)