-
Notifications
You must be signed in to change notification settings - Fork 2
/
process-devices.py
executable file
·276 lines (245 loc) · 7.64 KB
/
process-devices.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python3
from __future__ import print_function
import csv
import fileinput
import re
import sys
class Item(object):
def __init__(self, name):
self.name = name
self.parent = None
self.child_names = []
self.children = dict()
self.link = None
self.positions = None
self.raw_positions = None
self.power = None
self.lock = None
self.notes = None
@staticmethod
def ProcessNumbers(str):
"""Process 'foo [1..2]' and return ['foo 1', 'foo 2'].
>>> Item.ProcessNumbers('foo [1..2]')
['foo 1', 'foo 2']
>>> Item.ProcessNumbers('Hatch')
['Hatch']
>>> Item.ProcessNumbers('foobar 1')
['foobar 1']
>>> Item.ProcessNumbers('Door[1..2]')
['Door1', 'Door2']
"""
p = re.compile(r'^(.*)\[(\d+)\.\.(\d+)\](.*)$', re.IGNORECASE)
m = p.match(str)
if m:
before = m.group(1)
minval = int(m.group(2))
maxval = int(m.group(3))
after = m.group(4)
if maxval <= minval:
raise Exception('Min must be less than max')
return ['%s%d%s' % (before, i, after) for i in range(minval, maxval+1)]
else:
return [str]
def AddChild(self, child):
if child.name in self.children:
raise Exception('%s already has child %s' % (self.name, child.name))
self.child_names.append(child.name)
self.children[child.name] = child
child.parent = self
def IsMoveable(self):
return self.positions and len(self.positions) > 0
def SetLink(self, value):
self.link = value
def SetPositions(self, value):
if not value:
return
self.raw_positions = value
if '/' in value:
self.positions = value.split('/')
elif ',' in value:
self.positions = value.split(',')
else:
self.positions = Item.ProcessNumbers(value)
def SetPower(self, value):
if value == ['']:
return
self.power = value
def SetLock(self, value):
if value == ['']:
return
self.lock = value
def SetNotes(self, value):
self.notes = value
def GetEnumBaseString(self):
if self.name:
return ''.join(self.name.split())
else:
return None
def GetEnumString(self):
if self.parent:
parent_name = self.parent.GetEnumString()
if parent_name:
return '%s_%s' % (parent_name, self.GetEnumBaseString())
else:
return self.GetEnumBaseString()
else:
return self.GetEnumBaseString()
def Flatten(self):
all_children = [self]
for child_name in self.child_names:
all_children.extend(self.children[child_name].Flatten())
return all_children
def NumMoveables(self):
num_moveable = 0
for item in self.Flatten():
if item.IsMoveable():
num_moveable += 1
return num_moveable
def Depth(self):
if self.parent:
return self.parent.Depth() + 1
return 0
def WantsToBeHeading(self):
"""Does this item want to be a heading?"""
if len(self.children) > 0:
return True
return not self.IsMoveable()
def MakeAllChildrenHeadings(self):
"""Should *all* of this item's children be headings?"""
if len(self.children) == 0:
return False
for child_name in self.child_names:
child = self.children[child_name]
if not child.WantsToBeHeading():
return False
return True
@staticmethod
def DuplicateChar(ch, num_times):
return ''.join(ch for i in range(num_times))
def WikiSuffixText(self):
items = []
if self.positions:
if len(self.positions) <= 2:
items.append('[' + '/'.join(self.positions) + ']')
else:
items.append('[' + self.raw_positions + ']')
if self.power:
items.append('[' + '/'.join(self.power) + ']')
if self.lock:
items.append('[' + '/'.join(self.lock) + ']')
if self.notes:
items.append('(%s)' % self.notes)
return ', '.join(items)
def PrintWiki(self, list_depth):
depth = self.Depth()
if self.link:
name = '[%s %s]' % (self.link, self.name)
else:
name = self.name
if list_depth >= 0:
# myself and all children need to be lists.
list_depth += 1
v = Item.DuplicateChar('*', list_depth)
suffix = self.WikiSuffixText()
if suffix:
print(v, name, suffix)
else:
print(v, name)
else:
# I'm a heading.
v = Item.DuplicateChar('=', depth + 1)
print(v, name, v)
if not self.MakeAllChildrenHeadings():
list_depth = 0
if self.notes:
print(self.notes)
for child_name in self.child_names:
child = self.children[child_name]
child.PrintWiki(list_depth)
class Interactable(object):
def __init__(self, csv_stream):
# No name is required for root item.
self.riven = Item(None)
self.LoadFromStream(csv_stream)
def LoadFromStream(self, csv_stream):
parent_stack = [self.riven]
column_names = ['Island', 'L2', 'L3', 'L4', 'L5']
row_num = 1 # Start at 1 for ignored header row.
try:
reader = csv.DictReader(csv_stream)
for row in reader:
row_num += 1
item_name = None
for column_name in column_names:
if row[column_name]:
item_name = row[column_name]
parent_stack = parent_stack[:1+column_names.index(column_name)]
break
if not item_name:
msg = 'must have a value in one and only %s' % str(column_names)
raise Exception(msg)
parent = parent_stack[-1]
item = None
for iname in Item.ProcessNumbers(item_name):
item = Item(iname)
parent.AddChild(item)
if 'Link' in row:
item.SetLink(row['Link'])
if 'Positions' in row:
item.SetPositions(row['Positions'])
if 'Power' in row:
item.SetPower(row['Power'].split('/'))
if 'Lock' in row:
item.SetLock(row['Lock'].split('/'))
if 'Notes' in row:
item.SetNotes(row['Notes'])
parent_stack.append(item)
except Exception as e:
new_msg = 'CSV line %d: %s' % (row_num, str(e))
raise Exception(new_msg)
def PrintEnums(self):
enums = []
for item in self.riven.Flatten():
if True or item.IsMoveable():
name = item.GetEnumString()
if name:
enums.append(name)
for enum in sorted(enums):
print(enum)
def PrintWiki(self):
script_url = 'https://github.com/starryexpanse/StarryExpanse/blob/master/process-devices.py'
with open('spreadsheet_url.txt', 'r') as f:
spreadsheet_url = f.readline()
msg = """'''Do not edit this document directly'''. It is automatically generated by [%s a script]. To change this document make changes to [%s the spreadsheet] and re-run the script.""" % (script_url, spreadsheet_url)
banner = '{{Note|note=%s}}' % msg
print(banner)
# Print the statistics
print('== Statistics ==')
print('{| class="wikitable"')
print('|-')
print('! Island || Movable Devices')
total_moveable = 0
for child_name in self.riven.child_names:
child = self.riven.children[child_name]
print('|-')
print('|', child.name)
print('| style="text-align:right;" |', child.NumMoveables())
total_moveable += child.NumMoveables()
print('|-')
print('! Total || style="text-align:right;" | %d' % total_moveable)
print('|}')
# Now the island wiki text
for island_name in self.riven.child_names:
island = self.riven.children[island_name]
island.PrintWiki(-1)
if __name__ == '__main__':
import doctest
doctest.testmod()
try:
interactable = Interactable(fileinput.input())
#interactable.PrintEnums()
interactable.PrintWiki()
except Exception as e:
raise
print('ERROR: %s' % str(e), file=sys.stderr)
sys.exit(1)