-
Notifications
You must be signed in to change notification settings - Fork 11
/
zerg.py
288 lines (235 loc) · 10.3 KB
/
zerg.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
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python
#
# Zerg - simple ERG/MRC -> Zwift Workout File convertor
#
# This is a deliberately simple, self-contained script to avoid any complications
# with using external libraries, whether standard (e.g. shlex) or 3rd party (e.g.
# PLY, pyparsing). As such, and also because it's had to be wipped up inbetween
# actually working on other code, it's a bit shonky and decidely odd-looking in
# places... but it may help someone do a couple of simple conversions if they're
# not fussy.
#
# Copyright (c) 2015 Tim Parker
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
# to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
import os, sys, re
import xml.etree.ElementTree as XmlDoc
import xml.dom.minidom as MiniDom
class ErgParser :
sectionStartRe = re.compile( '[[](?P<tag>[^]]+)[]].*' )
sectionEndRe = re.compile( '[[]END (?P<tag>[^]]+)[]].*' )
def __init__( self, path, fileOutput = None, fileType = None, outputDir = None ) :
self.fileType = fileType
self.input = path
self.output = fileOutput
self.outputDir = outputDir
self.rootNode = XmlDoc.Element( 'workout_file' )
self.currentNode = self.rootNode
self.sectionTokens = {
'COURSE HEADER' : ( self.headerStart, self.headerParse, self.headerEnd ) ,
'COURSE DATA' : ( self.dataStart, self.dataParse, self.dataEnd ) ,
'COURSE TEXT' : ( self.textStart, self.textParse, self.textEnd )
}
self.parsers = [ self.startOfSection ]
self.parse( self.input )
def write( self ) :
if not self.output :
stem, ext = os.path.splitext( self.input )
self.output = '%s.zwo' % stem
if self.outputDir :
self.output = os.path.join( self.outputDir, os.path.basename( self.output ) )
try :
strm = open( self.output, 'w' )
strm.write( '%s\n' % MiniDom.parseString( XmlDoc.tostring( self.rootNode, 'utf-8')
).toprettyxml(indent=" ") )
strm.close()
except Exception, err :
raise Exception( "Error writing output file %s : " % ( outputFile, err ) )
def parser( self ) :
return self.parsers[-1]
def startOfSection( self, line ) :
match = self.sectionStartRe.match( line )
if match :
start, parse, _ = self.sectionTokens[ match.group( 'tag' ) ]
start()
self.parsers.append( parse )
return True
return False
def endOfSection( self, line ) :
match = self.sectionEndRe.match( line )
if match :
_, _, end = self.sectionTokens[ match.group( 'tag' ) ]
end()
self.parsers.pop()
return True
return False
def power( self, percent ) :
# Hard-coded for percentage FTP for now (e.g. MRC files)
# Zwift uses decimal FTP ratio
return percent / 100
def duration( self, mins ) :
# Hard-coded for input interval times in minutes (ERG/MRC)
# Zwift uses seconds (single in file, 5 sec rounding in GUI)
return round(60 * mins, 0)
def addInterval( self, intervalType, mins, powerLow, powerHigh ) :
# NB. duration is minutes in ERG/MRC for data, and seconds for Zwift
# workout files (and also time offset for text events in ERG/MRC).
return XmlDoc.SubElement(
self.currentNode, intervalType, Duration = "%s" % self.duration( mins ) ,
PowerLow = '%.3f' % self.power( powerLow ), PowerHigh = '%.3f' % self.power( powerHigh ) )
def addNode( self, name, value, parent = None ) :
if not parent : parent = self.currentNode
node = XmlDoc.SubElement( parent, name )
node.text = value
return node
#-----------------------------
# Course Header
#-----------------------------
def headerStart( self ) : pass
def headerEnd( self ) : pass
def headerParse( self, line ) :
headerTokens = {
'FILE NAME' : 'name' ,
'DESCRIPTION' : 'description'
}
if not self.endOfSection( line ) :
tokens = line.split( '=' )
if len( tokens ) == 1 :
# e..g. MINUTES PERCENT
pass
elif len( tokens ) == 2 :
try :
self.addNode( headerTokens[ tokens[0].strip() ], tokens[1] )
except Exception, err :
print "Muuuh : %s" % err
pass
#-----------------------------
# Course Data
#-----------------------------
def dataStart( self ) :
self.data = []
self.currentNode = XmlDoc.SubElement( self.rootNode, 'workout' )
def dataEnd( self ) :
if self.data :
numPoints = len( self.data )
print "Data points : %d" % numPoints
# numPoints == 1 is a special case (time datum must be non-negative)
# Deal with that later,
prevTime, prevEffort = self.data[0]
for currTime, currEffort in self.data[1:] :
duration = currTime - prevTime
deltaEffort = currEffort - prevEffort
print "Duration = %.3f, start = %.3f, end = %.3f (D = %.3f)" % (
duration, prevEffort, currEffort, deltaEffort )
if duration :
# Increasing effort is 'Warmup' in Zwift parlance, decreasing effort
# is 'Cooldown' and no change is 'SteadyState' (note case).
intervalType = 'SteadyState'
if deltaEffort > 0 : intervalType = 'Warmup'
elif deltaEffort < 0 : intervalType = 'Cooldown'
interval = self.addInterval( intervalType, duration, prevEffort, currEffort )
# .. and around we go again
prevTime = currTime
prevEffort = currEffort
del self.data
def dataParse( self, line ) :
if not self.endOfSection( line ) :
#print '%s' % line
try :
mins, percent = map( float, line.split()[0:2] )
self.data.append( ( mins, percent ) )
except :
pass
#-----------------------------
# Course Text (TBD)
#-----------------------------
def textStart( self ) : pass
def textEnd( self ) : pass
def textParse( self, line ) :
print '%s' % line
def parse( self, path ) :
try :
for line in open( path ) :
print "Parser '%s', line : %s" % ( self.parser(), line )
self.parser()( line )
except Exception, err :
print 'Muuuh : %s' % err
finally :
self.write()
#---------------------------------------------------------
# Mainline - options parsing and drivers
#---------------------------------------------------------
if __name__ == '__main__' :
import getopt
def printUsage() :
print """
zerg [options] file1.mrc [file2.mrc ...]
Convert one or more ERG/MRC files to Zwift Workout (zwo) files.
Currently only MRC files are supported (workouts are relative to
FTP, effort is NOT defined in Watts).
Options :
-D <dir> Place all converted files into directory <dir>.
Default is to write the converted data file to
the same directory as the original data file.
-h Help. Show this page and exit.
-o <name> Save converted file to <name>. Default is to
name the converted data file the same as the
original data file except for the extension
which will be changed to '.zwo', e.g.
example.mrc -> example.zwo
This obviously only makes sense when one file
is being converted. No check is made that the
converted data file name is different from the
original data file name currently - beware.
"""
optFileType = None
optFileOutput = None
optOutputDir = None
fileTypes = {
'erg' : ( 'ERG file', 'WATTS' ) ,
'mrc' : ( 'MRC file', 'PERCENT' )
}
try :
opts, files = getopt.getopt( sys.argv[1:], "D:ho:t:" )
except getopt.GetoptError, msg :
print "Invalid option(s) : %s" % msg
printUsage()
sys.exit( 1 )
except Exception, err :
print "Internal error, exiting : %s" % err
sys.exit( 2 )
else :
for opt, val in opts :
if opt == '-D' :
optOutputDir = val
elif opt == '-h' :
printUsage()
sys.exit( 0 )
elif opt == '-o' :
optFileOutput = val
elif opt == '-t' :
try :
desc, _ = fileTypes[ val ]
print "Forcing file type to '%s' (%s)" % ( val, desc )
optFileType = val
except :
print "Invalid file type '%s', should be one of %s" % (
val, ', '.join( fileTypes.keys() ) )
sys.exit( 3 )
for path in files :
ErgParser( path,
fileType = optFileType, fileOutput = optFileOutput, outputDir = optOutputDir )