Skip to content

Commit

Permalink
Add command line argument support and support Python 2.6
Browse files Browse the repository at this point in the history
- Support Python 2.6, 2.7, 3.2 and 3.3.
- Add command line argument support for passing in the output
  filename. Handle stdout by using -.
  • Loading branch information
Thomas O'Dowd committed Oct 25, 2013
1 parent f8fc8ea commit 5716a11
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 17 deletions.
37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# lemondcsv v1.0.1
# lemondcsv v1.1.0

This script converts a native Lemond Revolution Power Pilot CVS
workout file to a Garmin TCX file. The TCX file can be then imported
Expand All @@ -21,8 +21,8 @@ script if possible.

## Strava and Speed

This version of the script includes a fix for Strava so that it
correctly shows distance and speed.
This script converts the data in such a way that Strava can
correctly show speed.

## Merging Multiple CSV files to one TCX workout

Expand All @@ -34,14 +34,35 @@ group of CSV files into one TCX workout. If you have a workout
session (on firmware 63) with multiple files, please send them
to me so that I can adjust the script to work with it.

## Running the Script
## Running the lemondcsv.py

Simply run the script from the shell as follows:
The script can be run in a number of ways. Here are some examples.

./lemondcsv.py 09261300.CSV > 09261300.tcx
The simpliest way is to pass a single workout argument. This will take
convert the given CSV filename to a *TCX* filename of a similar name.
Note that if the TCX file already exists, the script will not overwrite it.

The TCX file can then be uploaded to Strava etc as a file
upload.
./lemondcsv.py 09261300.CSV

Another way is to specify a filename to use as the *TCX* filename. You
can do this by using the '-f' argument. Note that when you use -f, the
script will overwrite the target file if it exists as it presumes you
know what you want to do.

./lemondcsv.py -f 09261300.tcx 09261300.CSV

If you would like to output the TCX to stdout for some fancy processing
then you can always use the '-f' argument with the '-' option.

./lemondcsv.py -f - 09261300.CSV | xmllint --format - > 09261300.tcx

Once you have generated your TCX file, you can directly upload the file
to Strava and analyze your workout there.

## Python Version Support

This script has been tested on Python 2.6, 2.7, 3.2 and 3.3. It will not
work on Python 2.5 or earlier.

## Contact/Questions/Bugs/Updates etc

Expand Down
60 changes: 51 additions & 9 deletions lemondcsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@
# file. If you have a session with multiple files, please send them
# to me so that I can adjust the script to work with it.

import os
import sys
import csv
import time
import getopt
from xml.etree import ElementTree
from xml.etree.ElementTree import Element, SubElement

Expand Down Expand Up @@ -228,13 +230,14 @@ def isoTimestamp(seconds):
def metersPerSec(speed):
return speed / 3.6

def writeTCX(self):
def writeTCX(self, file):
tcdb = self.trainingCenterDB()
et = ElementTree.ElementTree(tcdb)
if hasattr(sys.stdout, 'buffer'):
et.write(sys.stdout.buffer, 'UTF-8', True)
else:
et.write(sys.stdout, 'UTF-8', True)
try:
et.write(file, 'UTF-8', True)
except TypeError:
# pre-python 2.7
et.write(file, 'UTF-8')

def trainingCenterDB(self):
dict = {'xsi:schemaLocation': XML_NS + ' ' + XSD,
Expand Down Expand Up @@ -334,9 +337,48 @@ def trackElement(self):
t.append(p.trackpointElement(self.startsec))
return t

if len(sys.argv) != 2:
sys.stderr.write("Usage: %s workout.csv > workout.tcx\n" % sys.argv[0])

def output_name(iname):
# validate name ends with .CSV
if iname.lower().endswith(".csv"):
prefix = iname[:-3]
oname = prefix + "tcx"
if not os.path.exists(oname):
return oname
else:
raise Exception("File %s already exists. Cannot continue." % oname)
else:
raise Exception("%s does not end with .csv" % iname)


def usage_exit():
sys.stderr.write("Usage: lemondcsv.py [-f file.tcx] workout.csv\n")
sys.exit(1)

opts, args = getopt.getopt(sys.argv[1:], 'f:h')
oname = None
for opt, arg in opts:
if opt == '-f':
oname = arg
elif opt == '-h':
usage_exit()

if len(args) != 1:
# TODO: support multiple CSV files to join them up to one TCX.
usage_exit()
else:
revo = Revolution(sys.argv[1])
revo.writeTCX()
iname = args[0]
if oname is None:
oname = output_name(iname)
revo = Revolution(iname)
if oname == '-':
if hasattr(sys.stdout, 'buffer'):
ofile = sys.stdout.buffer
else:
ofile = sys.stdout
else:
sys.stderr.write("Writing to: %s\n" % oname)
ofile = open(oname, "wb")
revo.writeTCX(ofile)
if oname != '-':
ofile.close()

0 comments on commit 5716a11

Please sign in to comment.