forked from cooperhewitt/collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_objects.py
executable file
·75 lines (53 loc) · 1.74 KB
/
export_objects.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
#!/usr/bin/env python
# I think there might still be some weirdness in the UTF-8 wrangling
# (20120225/straup)
"""
Export the contents of objects.csv in to individual JSON files.
"""
import sys
import json
import csv
import utils
import os
import os.path
class UnicodeCsvReader(object):
def __init__(self, f, encoding="utf-8", **kwargs):
self.csv_reader = csv.reader(f, **kwargs)
self.encoding = encoding
def __iter__(self):
return self
def next(self):
# read and split the csv row into fields
row = self.csv_reader.next()
# now decode
try:
return [unicode(cell, self.encoding) for cell in row]
except Exception, e:
return []
@property
def line_num(self):
return self.csv_reader.line_num
class UnicodeDictReader(csv.DictReader):
def __init__(self, f, encoding="utf-8", fieldnames=None, **kwds):
csv.DictReader.__init__(self, f, fieldnames=fieldnames, **kwds)
self.reader = UnicodeCsvReader(f, encoding=encoding, **kwds)
if __name__ == '__main__':
whoami = os.path.abspath(sys.argv[0])
bindir = os.path.dirname(whoami)
collection = os.path.dirname(bindir)
objects = os.path.join(collection, 'objects')
obj_csv = os.path.join(collection, 'objects.csv')
obj_fh = open(obj_csv, 'r')
reader = UnicodeDictReader(obj_fh)
for row in reader:
fname = "%s.json" % row['id']
root = utils.id2path(row['id'])
root = os.path.join(objects, root)
out = os.path.join(root, fname)
print out
continue
if not os.path.exists(root):
os.makedirs(root)
out_fh = open(out, 'w')
json.dump(row, out_fh, indent=2)
print out