-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4 - check_csv_files.py
352 lines (288 loc) · 10.2 KB
/
4 - check_csv_files.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import os
import logging
from pathlib import Path
import csv
from dateutil.parser import parse
from datetime import datetime
################################################################################################
# 1. functions #
################################################################################################
def validate_csv_files():
validate_csv(main_csv_arkid_filepath, func_invalid_cols_from_main_row)
validate_csv(resp_csv_arkid_filepath, func_invalid_cols_from_resp_row)
def validate_csv(csv_filepath, func):
row_hash = {}
num = 1
with open(csv_filepath, "r", encoding="utf-8") as csvfile:
csv_reader = csv.DictReader(csvfile)
for row in csv_reader:
num += 1
invalid_cols = func(row)
if len(invalid_cols):
arkid = row.get("arkid")
key = f"line->{num}, arkid->{arkid}:"
row_hash[key] = invalid_cols
if row_hash:
filepath = file_name(csv_filepath, result_directory_path)
print(f"{csv_filepath} is not valid. Please check details in {filepath}")
write_file(row_hash, filepath)
else:
print(f"{csv_filepath} is valid.")
# resp csv file has no *_O column names
def func_invalid_cols_from_resp_row(row):
cols = get_empty_cols(row, RESP_REQUIRED_FIELDS)
cols.extend(invalid_resp_cols(row))
return cols
def func_invalid_cols_from_main_row(row):
cols = get_empty_cols(row, MAIN_REQUIRED_FIELDS)
cols.extend(invalid_main_value_cols(row))
cols.extend(invalid_main_date_cols(row))
cols.extend(invalid_main_range_cols(row))
return cols
def invalid_main_value_cols(row):
value_hash = {
"gbl_resourceClass_sm": LS_gbl_resourceClass_sm,
"dct_accessRights_s": LS_dct_accessRights_s,
"dct_format_s": LS_dct_format_s,
}
return get_invalid_cols(row, value_hash, get_unexpected_f_v)
def invalid_main_date_cols(row):
date_hash = {
"gbl_mdModified_dt": LS_gbl_mdModified_dt,
"gbl_indexYear_im": LS_gbl_indexYear_im,
"dct_issued_s": LS_dct_issued_s,
}
return get_invalid_cols(row, date_hash, get_invalid_date_f_v)
def invalid_main_range_cols(row):
range_hash = {"dcat_theme_sm": RG_dcat_theme_sm}
return get_invalid_cols(row, range_hash, get_invalid_range_f_v)
def get_empty_cols(row, fieldnames):
empty = []
for fieldname in fieldnames:
value = get_value(row, fieldname)
if not value:
field_value = f_v(fieldname, ' " " ', "and")
empty.append(field_value)
return empty
def get_invalid_cols(row, hash, func):
invalid = []
for fieldname, expected_values in hash.items():
f_v = func(row, fieldname, expected_values)
if f_v:
invalid.append(f_v)
return invalid
def get_value(row, fieldname):
value = row.get(fieldname)
if value:
return value
o_fieldname = f"{fieldname}_o"
return row.get(o_fieldname)
def f_v(fieldname, value, word="or"):
fieldname_o = f"{fieldname}_o"
new_name = (
f"{fieldname} {word} {fieldname_o}"
if fieldname_o in main_csv_headers
else fieldname
)
return [new_name, value]
def get_unexpected_f_v(row, fieldname, expected_values):
value = get_value(row, fieldname)
if value:
vals = value.split("$") if fieldname.endswith("m") else [value]
for val in vals:
if not val in expected_values:
return f_v(fieldname, value)
return None
def get_invalid_date_f_v(row, fieldname, expected_date_formats):
value = get_value(row, fieldname)
if value:
vals = value.split("$") if fieldname.endswith("m") else [value]
for val in vals:
if fieldname == "dct_issued_s":
val = val.replace('"', "")
expected = expected_date(val, expected_date_formats)
if not expected:
return f_v(fieldname, value)
return None
def get_invalid_range_f_v(row, fieldname, expected_range):
value = get_value(row, fieldname)
if value:
vals = value.split("$") if fieldname.endswith("m") else [value]
for val in vals:
try:
r = int(val.strip())
if not r in expected_range:
return f_v(fieldname, value)
except ValueError:
return [fieldname, f"{fieldname} value '{val}' is not a valid integer"]
return None
def valid_date(str, format):
try:
return bool(datetime.strptime(str, format))
except ValueError:
return False
def expected_date(val, formats):
for format in formats:
if valid_date(val, format):
return True
return False
def csv_headers(csv_filepath):
with open(csv_filepath, "r", encoding="utf-8") as csvfile:
csv_reader = csv.DictReader(csvfile)
return csv_reader.fieldnames
def file_name(csvfilepath, output_dir):
name = Path(csvfilepath).stem
return os.path.join(output_dir, f"{name}_log.txt")
def write_file(hash, filepath):
with open(filepath, "w", encoding="utf-8") as file:
file.write(f"\n Invalid fields:\n")
for k, ls in hash.items():
file.write(f"\n {k}:\n")
for l in ls:
file.write(f"{l[0]}: {l[1]} \n")
# resp_csv -
# 1. When role is 006 (originator), a row should have either an individual or organization value
# 2. When role is not 006, a row should not allow to have an individual value
# 3. When role is 010 (publisher), it should have an organization value
def invalid_resp_cols(row):
str = row["role"].strip()
individual = row["individual"]
organization = row["organization"]
if not str:
return [["role", "Role should not be empty."]]
try:
num = int(str)
if not num in RESP_ROLE:
return [["role", f"Role value {str} is not between 1 and 11"]]
except ValueError:
return [["role", f"Role value '{str}' is not a valid integer"]]
role = str.zfill(3)
if role == "006":
if not organization and not individual:
return [
[
"individual or organization",
"When role = 006, either organization or individual should have a value",
]
]
else:
cols = []
if individual:
cols.append(
[
"individual",
f"When role != 006, individual should not have a value: {individual}",
]
)
if role == "010" and not organization:
cols.append(
["organization", "When role = 010, organization should have a value"]
)
return cols
return []
################################################################################################
# 2. variables #
################################################################################################
MAIN_REQUIRED_FIELDS = [
"arkid",
"geofile",
"dct_title_s",
"gbl_resourceClass_sm",
"dct_accessRights_s",
"gbl_mdModified_dt",
"dct_format_s",
]
LS_gbl_mdModified_dt = ["%Y-%m-%dT%H:%M:%SZ"]
LS_gbl_indexYear_im = ["%Y"]
LS_dct_issued_s = ["%Y", "%Y-%m", "%Y-%m-%d"]
LS_gbl_resourceClass_sm = [
"Collections",
"Datasets",
"Imagery",
"Maps",
"Web services",
"Websites",
"Other",
]
RG_dcat_theme_sm = range(1, 20)
LS_dct_accessRights_s = ["Public", "Restricted"]
LS_dct_format_s = [
"ArcGRID",
"CD-ROM",
"DEM",
"DVD-ROM",
"Feature Class",
"Geodatabase",
"GeoJPEG",
"GeoJSON",
"GeoPackage",
"GeoPDF",
"GeoTIFF",
"JPEG",
"JPEG2000",
"KML",
"KMZ",
"LAS",
"LAZ",
"Mixed",
"MrSID",
"PDF",
"PNG",
"Pulsewaves",
"Raster Dataset",
"Shapefile",
"SQLite Database",
"Tabular Data",
"TIFF",
]
RESP_REQUIRED_FIELDS = ["arkid", "geofile"]
RESP_ROLE = range(1, 12)
################################################################################################
# 3. setup #
################################################################################################
# 1. setup log file path
logfile = r"C:\process_data\log\process.log"
logging.basicConfig(
filename=logfile,
level=logging.INFO,
format="%(message)s - %(asctime)s",
)
# 2. Please provide csv files path which have been assigned with arkids
main_csv_arkid_filepath = r"C:\process_data\csv_files_arkid\main_arkid.csv"
resp_csv_arkid_filepath = r"C:\process_data\csv_files_arkid\resp_arkid.csv"
# 3. please provide result directory path:
result_directory_path = r"C:\process_data\csv_files_arkid"
################################################################################################
# 4. Run
# Example:
# input:
# main_csv_arkid_filepath = r"D:\results\main_sample_raster_arkids8.csv"
# resp_csv_arkid_filepath = r"D:\results\main_sample_raster1.csv"
# result_directory_path = r"D:\results"
# any invalid field values found from above csv files will be written to:
# D:\results\main_sample_raster_arkids8.txt
# D:\results\main_sample_raster1.txt
################################################################################################
def output(msg):
logging.info(msg)
print(msg)
def verify_setup(file_paths, directory_paths):
verified = True
for file_path in file_paths:
if not Path(file_path).is_file():
print(f"{file_path} does not exit.")
verified = False
for directory_path in directory_paths:
if not Path(directory_path).is_dir():
print(f"{directory_path} does not exit.")
verified = False
return verified
main_csv_headers = []
script_name = "4 - check_csv_files.py"
output(f"***starting {script_name}")
if verify_setup(
[main_csv_arkid_filepath, resp_csv_arkid_filepath], [result_directory_path]
):
main_csv_headers = csv_headers(main_csv_arkid_filepath)
validate_csv_files()
output(f"***completed {script_name}")