-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
57 lines (50 loc) · 2.05 KB
/
main.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
import json
import sys
import os
import unicodecsv as csv
from argparse import RawTextHelpFormatter, ArgumentParser
from ckanuploader import uploader as u
parser = ArgumentParser(formatter_class=RawTextHelpFormatter)
parser.add_argument("-c", metavar="CONFIG_FILE", help="name of the config file", required=True)
parser.add_argument("-f", metavar="META_FILE", help="name of the meta file", required=True)
parser.add_argument("-d", metavar="FILE_FOLDER", help="name of the folder containing files to upload")
args = parser.parse_args()
c = json.load(open(args.c))
uploader = u.Uploader(c)
bulk_file = open(args.f, "r")
bulk_reader = csv.DictReader(bulk_file)
print "Checking data..."
upload_queue = []
for row in bulk_reader:
upload_queue.extend(uploader.process_row(row))
# Print collected errors.
e_counter = 0
for item in upload_queue:
if item.get("errors"):
print u"Error in data/dataset {title}:".format(title=item["name"])
for error in item["errors"]:
e_counter += 1
print "\t" + error
# If there exist errors, don't upload.
if e_counter > 0:
print "Total {num} error(s).".format(num=e_counter)
sys.exit()
print "Uploading data..."
data_counter = 0
for upload in upload_queue:
if "title" in upload.keys():
# Upload the package.
print "uploading package: %s..." % (upload["name"])
returned_package_info = uploader.instance.call_action("package_create", upload)
if "format" in upload.keys():
# Upload data of the package.
print "uploading resource: %s for above package..." % (upload["name"])
upload.update({"package_id": returned_package_info["id"]})
if upload.get("file_name"):
data_counter += 1
p = os.path.join(args.d, upload.pop("file_name"))
file_newname=c["name_prefix"] + str(data_counter) + "." + upload["format"]
uploader.instance.call_action("resource_create", upload,
files={"upload": (file_newname, open(p))})
else:
uploader.instance.call_action("resource_create", upload)