-
Notifications
You must be signed in to change notification settings - Fork 1
/
genteams.py
executable file
·171 lines (129 loc) · 4.86 KB
/
genteams.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
#!/usr/bin/env python
from collections import OrderedDict
import csv
import sys
import os
from os.path import abspath, dirname, exists, join
import optparse
import frontmatter
import code_from_gh
import yaml
def read_team_csv(csv_fname):
with open(csv_fname) as fp:
reader = csv.reader(fp)
for row in reader:
yield [r.strip() for r in row]
def add_maybe(d, f, v):
if not v:
if f not in d:
d[f] = None
else:
d[f] = v
def add_maybe_web(d, k, nv):
if nv:
v = d.get(k)
if v is None or v.lower().strip('/') != nv.lower().strip('/'):
d[k] = nv
def main():
# input is teams csv datafile from TBA
# -> https://github.com/the-blue-alliance/the-blue-alliance-data
csv_fname = abspath(sys.argv[1])
max_team = int(sys.argv[2])
mode = sys.argv[3]
if mode not in ['new', 'update']:
print("Error: invalid mode")
return
os.chdir(abspath(join(dirname(__file__), '..')))
cwd = os.getcwd()
for row in read_team_csv(csv_fname):
# this changes on occasion...
number, name, sponsors, l1, l2, l3, website, rookie_year, \
facebook, twitter, youtube, github, instagram, periscope = row
name = name
rookie_year = rookie_year
if rookie_year:
rookie_year = int(rookie_year)
number = number[3:]
if int(number) > max_team:
continue
d1 = '%04d' % (int(int(number)/1000)*1000,)
d2 = '%03d' % (int(int(number)/100)*100,)
f = join(cwd, 'frc%s' % d1, '_frc', d2, '%s.md' % number)
if mode == 'new' and exists(f):
continue
if 'firstinspires' in website:
website = ''
if l3:
location = '%s, %s, %s' % (l1, l2, l3)
elif l2:
location = '%s, %s' % (l1, l2)
else:
location = l1
sponsors = [s.strip() for s in sponsors.split('/')]
if sponsors == ['']:
sponsors = None
else:
if '&' in sponsors[-1]:
sN = sponsors[-1].split('&')
del sponsors[-1]
sponsors += [s.strip() for s in sN]
if mode == 'update':
try:
fm = frontmatter.load(f)
except:
print("Error at %s" % f)
raise
reformatted = str(frontmatter.dumps(fm))
if 'team' not in fm.metadata:
raise Exception("Error in %s" % f)
team = fm.metadata['team']
if 'links' not in fm.metadata['team']:
links = OrderedDict()
else:
links = fm.metadata['team']['links']
else:
data = OrderedDict()
team = OrderedDict()
links = OrderedDict()
data['title'] = 'FRC Team %s' % number
data['team'] = team
team['type'] = 'FRC'
team['number'] = int(number)
add_maybe(team, 'name', name)
add_maybe(team, 'rookie_year', rookie_year)
add_maybe(team, 'location', location)
if sponsors and mode != 'update':
team['sponsors'] = sponsors
if 'Github' in links:
links['GitHub'] = links['Github']
del links['Github']
add_maybe_web(links, 'Website', website)
add_maybe_web(links, 'Facebook', facebook)
add_maybe_web(links, 'Twitter', twitter)
add_maybe_web(links, 'YouTube', youtube)
add_maybe_web(links, 'GitHub', github)
add_maybe_web(links, 'Instagram', instagram)
add_maybe_web(links, 'Periscope', periscope)
if mode == 'update':
if links:
fm.metadata['team']['links'] = links
if fm.content.strip() == 'No content has been added for this team':
fm.content = '{% include remove_this_line_and_add_a_paragraph %}'
page = str(frontmatter.dumps(fm))
if reformatted == page:
# don't make gratuitious changes
continue
elif mode == 'new':
if links:
team['links'] = links
page = '---\n%s\n---\n\n{%% include remove_this_line_and_add_a_paragraph %%}\n' % (
yaml.safe_dump(data)
)
# roundtrip through frontmatter to get the formatting consistent
page = frontmatter.dumps(frontmatter.loads(page))
if not exists(dirname(f)):
os.makedirs(dirname(f))
with open(f, 'w') as fp:
fp.write(page)
if __name__ == '__main__':
main()