-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.py
248 lines (208 loc) · 7.32 KB
/
render.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
# -*- coding: utf-8 -*-
import pykintone
from mymodel import Person
from cache import get_all
from jinja2.environment import Environment
from jinja2 import Template, FileSystemLoader
import codecs
import os
import argparse
import unicodecsv as csv
from cStringIO import StringIO
OUTPUT_DIR = './output'
# OUTPUT_DIR = '../mitou/webtools/mitou.github.io/people'
env = Environment()
env.loader = FileSystemLoader('.')
def output(x):
data = x.__dict__
t = env.get_template('template_v01.html')
html = t.render(data)
fo = codecs.open(os.path.join(OUTPUT_DIR, '%s.html' % x.name), 'w', 'utf-8')
fo.write(html)
fo.close()
def assure_length(xs, length):
if args.non_strict:
if len(xs) != length:
print 'length mismatch:', length, ','.join(xs)
xs += [''] * length
else:
if len(xs) != length:
raise RuntimeError(u'length mismatch: {} and {}'.format(length, u','.join(xs)).encode('utf-8'))
def process_tags(x):
resume_list = []
activity_list = []
mitou_list = []
photo = None
rows = csv.reader(StringIO(x.tags.strip('\n').encode('utf8')), encoding='utf8')
for items in rows:
if not items: continue
if items[0] == u'所属':
# syntax: 所属,where,when,note
# whenは「20xx-xx-xx時点」や「2014-2016」などのフリーフォーマット
assure_length(items, 4)
if items[1] in [u"", u"フリー", u"フリーランス"]: continue
resume_list.append(dict(
where=items[1],
when=items[2],
who=[], # be filled by collect_tags
note=items[3],
ref_id=''
))
# TODO when note include '#1' etc, put it in ref_id
elif items[0] == u'未踏採択':
# e.g. 未踏採択,2014,未踏,SC,任意キャラクターへの衣装転写システム,首藤 一幸
assure_length(items, 6)
mitou_list.append(dict(
when=items[1],
kubun=items[2],
members=[], # be filled by collect_tags
sc=items[3],
theme=items[4],
pm=items[5]
))
elif items[0] == u'Photo':
photo = ':'.join(items[1:]).strip(':')
elif items[0] == u'講演':
activity_list.append(dict(
type=items[0],
title=u"{}「{}」".format(items[1], items[2]),
when=items[3],
who=[],
note=items[4],
ref_id=''
))
else:
assure_length(items, 4)
activity_list.append(dict(
type=items[0],
title=items[1],
when=items[2],
who=[],
note=items[3],
ref_id=''
))
x.activity_list = activity_list
x.resume_list = resume_list
x.mitou_list = mitou_list
x.photo = photo
return x
def put_all():
for x in get_all(args.use_cache):
x = process_tags(x)
output(x)
def collect_tags():
from collections import defaultdict
mitou_theme = defaultdict(set)
mitou_sc = defaultdict(set)
mitou_kubun = defaultdict(set)
affiliation = defaultdict(set)
event = defaultdict(set)
xs = get_all(args.use_cache)
for x in xs:
x = process_tags(x)
for m in x.mitou_list:
if m['theme']:
# PMs don't have `theme`
mitou_theme[m['theme']].add(x)
m['pretty_kubun'] = pretty(m['when'], m['kubun'])
mitou_kubun[m['pretty_kubun']].add(x)
if m['sc']:
mitou_sc[m['pretty_kubun']].add(x)
for m in x.resume_list:
affiliation[m['where']].add(x)
for m in x.activity_list:
event[m['title']].add(x)
for x in xs:
me = set([x])
for m in x.mitou_list:
m['members'] = sorted(mitou_theme[m['theme']] - me)
for m in x.resume_list:
m['who'] = sorted(affiliation[m['where']] - me)
for m in x.activity_list:
m['who'] = sorted(event[m['title']] - me)
if not(args.index_only):
for x in xs:
output(x)
t = env.get_template('template_list.html')
print 'output kubun'
data = list(sorted((k, mitou_kubun[k]) for k in mitou_kubun))
html = t.render(title=u'採択区分別一覧', data=data)
fo = codecs.open(os.path.join(OUTPUT_DIR, 'kubun.html'), 'w', 'utf-8')
fo.write(html)
fo.close()
print 'output sc'
data = list(sorted((k, mitou_sc[k]) for k in mitou_sc))
html = t.render(title=u'スパクリ一覧', data=data)
fo = codecs.open(os.path.join(OUTPUT_DIR, 'sc.html'), 'w', 'utf-8')
fo.write(html)
fo.close()
print 'output affiliation'
data = list(sorted(
((k, affiliation[k])
for k in affiliation),
key=lambda x:-len(x[1])))
html = t.render(title=u'所属別一覧', data=data)
fo = codecs.open(os.path.join(OUTPUT_DIR, 'affiliation.html'), 'w', 'utf-8')
fo.write(html)
fo.close()
print 'output index.html'
t = env.get_template('template_index.html')
html = t.render(title=u'未踏名鑑')
fo = codecs.open(os.path.join(OUTPUT_DIR, 'index.html'), 'w', 'utf-8')
fo.write(html)
fo.close()
def find_links(s):
'convert name to link'
assert isinstance(s, basestring)
import re
def make_link(m):
name = m.groups()[0]
return to_link(name)
s = re.sub('\[(.+?)\]', make_link, s)
return s
env.filters['find_links'] = find_links
def to_link(name):
'get a name and make it to link'
assert isinstance(name, basestring)
return u'<nobr><a href="{0}.html">[{0}]</a></nobr>'.format(name)
env.filters['to_link'] = to_link
def abbrev_people(people):
if len(people) < 8:
return show_people(people)
# 情報量の多い順に表示,同点なら名前順
rest = len(people) - 7
people = sorted(
people,
key=lambda p: (len(p.tags) + len(p.note), p.name),
reverse=True)
from itertools import islice
people = islice(people, 7)
return ' '.join(to_link(p.name) for p in people) + u'他{}人'.format(rest)
env.filters['abbrev_people'] = abbrev_people
def show_people(people):
return ' '.join(map(to_link, sorted(p.name for p in people)))
env.filters['show_people'] = show_people
def pretty(when, kubun=None):
if '.' in when:
year, ki = when.split('.')
result = u'{}年{}期'.format(year, ki)
else:
result = when + u'年'
if kubun:
if kubun == u'ユース':
kubun = u'未踏ユース'
elif kubun == u'本体':
kubun = u'未踏本体'
elif kubun == u'未踏':
pass # do nothing
else:
raise RuntimeError(u'{} should be in ユース, 本体, 未踏'.format(kubun))
result += kubun
return result
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--use-cache', '-c', action='store_true', help='use local cache for input instead of reading from kintone')
parser.add_argument('--index-only', action='store_true', help='render index only')
parser.add_argument('--non-strict', action='store_true', help='skip strict format check')
args = parser.parse_args()
collect_tags()