import argparse
import logging
import os
import os.path
import time

import powerpod

LOGGER = logging.getLogger(__name__)

def arg_parser():
	parser = argparse.ArgumentParser()
	parser.add_argument('--port')
	return parser

def main():
	logging.basicConfig(level=logging.INFO)
	args = arg_parser().parse_args()
	kwargs = {}
	serial_connection = powerpod.NewtonSerialConnection(port=args.port)
	protocol = powerpod.NewtonSerialProtocol(serial_connection, device_side=False)
	response = protocol.do_command(powerpod.GetFileListCommand())
	LOGGER.debug(repr(response))
	for i, header in enumerate(response.records):
		filename = "powerpod.%s-%0.1fkm.raw" % (header.start_time.as_datetime().strftime("%Y-%m-%dT%H-%M-%S"), header.distance_metres / 1000)
		LOGGER.info("index=%s header=%s filename=%s", i, header, filename)
		time.sleep(1)
		response = protocol.do_command(powerpod.GetFileCommand(i))
		open(os.path.join('rides', filename), 'w').write(response.ride_data.to_binary())

if __name__ == '__main__':
	main()

@@ -49,16 +49,26 @@
 os.path%0A
+import re%0A
 import t
@@ -131,358 +131,97 @@
 _)%0A%0A
-def arg_parser():%0A%09parser = argparse.ArgumentParser()%0A%09parser.add_argument('--port')%0A%09return parser%0A%0Adef main():%0A%09logging.basicConfig(level=logging.INFO)%0A%09args = arg_parser().parse_args()%0A%09kwargs = %7B%7D%0A%09serial_connection = powerpod.NewtonSerialConnection(port=args.port)%0A%09protocol = powerpod.NewtonSerialProtocol(serial_connection, device_side=False)%0A
+class Action(object):%0A%09pass%0A%0Aclass GetAllRides(Action):%0A%09def run(self, protocol, args):%0A%09
 %09res
@@ -279,16 +279,17 @@
 mand())%0A
+%09
 %09LOGGER.
@@ -310,16 +310,17 @@
 ponse))%0A
+%09
 %09for i, 
@@ -358,16 +358,17 @@
 cords):%0A
+%09
 %09%09filena
@@ -495,16 +495,17 @@
 / 1000)%0A
+%09
 %09%09LOGGER
@@ -567,16 +567,17 @@
 name)%0A%09%09
+%09
 time.sle
@@ -582,16 +582,17 @@
 leep(1)%0A
+%09
 %09%09respon
@@ -650,13 +650,20 @@
 )%0A%09%09
-open(
+%09filepath = 
 os.p
@@ -675,69 +675,896 @@
 oin(
-'rides', filename), 'w').write(response.ride_data.to_binary()
+args.ride_directory, filename)%0A%09%09%09open(filepath, 'w').write(response.ride_data.to_binary())%0A%0Adef make_action(string):%0A%09assert string == 'get_all_rides'%0A%09return GetAllRides()%0A%0Adef arg_parser():%0A%09parser = argparse.ArgumentParser()%0A%09parser.add_argument('--port', default='/dev/ttyUSB0')%0A%09parser.add_argument(%0A%09%09%09'--ride-output-directory',%0A%09%09%09dest='ride_directory',%0A%09%09%09default='./rides',%0A%09%09%09help='For --get-all-rides',%0A%09)%0A%09parser.add_argument(%0A%09%09%09'actions',%0A%09%09%09nargs='+',%0A%09%09%09type=make_action,%0A%09%09%09help=%22%22%22%0A%09%09%09%09Actions to perform. See --help-actions for a list.%0A%09%09%09%22%22%22,%0A%09)%0A%09return parser%0A%0Adef main():%0A%09logging.basicConfig(level=logging.INFO)%0A%09args = arg_parser().parse_args()%0A%09kwargs = %7B%7D%0A%09serial_connection = powerpod.NewtonSerialConnection(port=args.port)%0A%09protocol = powerpod.NewtonSerialProtocol(serial_connection, device_side=False)%0A%09for action in args.actions:%0A%09%09action.run(protocol, args
 )%0A%0Ai

make get_rides more generic