-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.py
131 lines (104 loc) · 3.58 KB
/
updater.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
# -*- coding: utf-8 -*-
"""
kintone上のデータを、バックアップを取ってから一括アップデートするスクリプト
オプション指定なし→ローカルキャッシュを用いてDry Run
-r(--real) →最新のデータを取得してバックアップし、更新
-f(--from-backup) →-rで問題が起きたとき用。バックアップを指定して、そのデータを元に更新する。
"""
from cache import get_all, get_app
import time
import argparse
from render import pretty
def concat_lines(x, y):
if isinstance(y, str): y = y.decode('utf-8')
return x.rstrip('\n') + '\n' + y
def add_a_tag(tags, a_tag, length=None):
assert not '\n' in a_tag
if length:
assert len(a_tag[1:].split(a_tag[0])) == length
tags.append(a_tag)
def rows_to_csv(rows):
import cStringIO
import unicodecsv as csv
f = cStringIO.StringIO()
csv.writer(f, encoding='utf-8').writerows(rows)
return f.getvalue()
def convert(xs, args):
"add new creators from 2015_creators_170113.csv"
import unicodecsv as csv
name2x = dict((x.name, x) for x in xs)
to_update = []
to_add = []
rd = csv.reader(file('2015_creators_170113.csv'), encoding='utf-8')
for row in rd:
year = row[2]
kubun = row[3]
sc = row[4]
theme = row[5]
name = row[6]
pm = row[9]
affil1 = row[7]
affil2 = row[8]
if name in name2x:
x = name2x[name]
to_update.append(x)
else:
from mymodel import Person
x = Person()
x.name = name
to_add.append(x)
tags = [
["未踏採択", year, kubun, sc, theme, pm],
["所属", affil1, "{}年時点".format(year), affil2]]
tags = rows_to_csv(tags)
x.tags = concat_lines(x.tags, tags)
print name
print tags
return to_add, to_update
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--real', '-r',
action='store_true', help='read from kintone and write to kintone')
parser.add_argument(
'--from-backup', '-f',
action='store', help='read from backup and write to kintone')
parser.add_argument(
'--converter', '-c',
action='store', help='use specific converter')
parser.add_argument(
'--infile', '-i',
action='store', help='input file')
args = parser.parse_args()
if args.real:
dumpdir = time.strftime('backup_%m%d_%H%M')
xs = get_all(cache=False, name=dumpdir)
elif args.from_backup:
xs = get_all(cache=True, name=args.from_backup)
else:
xs = get_all(cache=True)
if not args.converter:
to_add, to_update = convert(xs, args)
else:
import imp
info = imp.find_module('converter/' + args.converter)
m = imp.load_module('m', *info)
to_add, to_update = m.convert(xs, args)
print "{} items to update, {} items to add".format(len(to_update), len(to_add))
# when recover from backup we need to ignore revision
if args.from_backup:
for x in xs:
x.revision = -1 # ignore revision
if args.real or args.from_backup:
app = get_app()
result = app.batch_create(to_add)
assert result.ok
for i in range(0, len(to_update), 100):
print i, to_update[i].name
result = app.batch_update(to_update[i:i + 100])
assert result.ok
else:
# for debug: Run this script with `ipython -i`
globals()['xs'] = xs
if __name__ == '__main__':
main()