#!/usr/bin/env python title = "Stopwatch" version = "0.0.4" copyright = "Copyright (C) 2006 George F. Rice" website = "http://drgeorge.org/stopwatch/" authors = ["George F. Rice"] from Stopwatch import stopwatch if __name__ == "__main__": sw = stopwatch() help = """ "s" to start the timer, "x" to stop the timer, "d" to display current time and lap timers, lap timer name to capture and display a lap timer, "w filename" to write a file (CSV format), "r filename" to read a file, "h" to repeat this help message, and "exit" to exit. """ print help filename = None while True: prompt = sw.getFormattedTime() if sw.running: prompt += " (running): " else: prompt += " (stopped): " command = raw_input(prompt) # try: if True: c = command.split() if not command: pass elif c[0] == 's': sw.start() elif c[0] == 'x': sw.stop() elif c[0] == 'd': if filename: print " --- Stopwatch %s ---" % (filename, ) print ' Current timer is ', sw.getFormattedTime() print ' Lap timers:' i = 0 for l in sw.lap.keys(): i = max(i, len(l)) s = ' %-' + str(i) + 's = %s' for l in sorted(sw.lap.keys()): print s % (l, sw.getFormattedTime(l)) elif c[0] == 'h': print help elif c[0] == 'exit': break elif c[0] == 'r': if c[1]: filename = c[1] if True: # try: i = 0 f = open(filename) i = 1 v = f.readline().strip() if v != title: raise ValueError, "Not a stopwatch file" i = 2 fmajor, fminor, fpatch = f.readline().strip().split(".") vmajor, vminor, vpatch = version.split(".") if fmajor != vmajor: raise ValueError, "File created with version %s (this is version $s)\nSee website for help." % (fmajor, vmajor) sw = stopwatch() i = 3 v = f.readline().strip() if v == "running": sw.running = True elif v == "stopped": sw.running = False else: raise ValueError, "Expecting 'running' or 'stopped'" i = 4 sw.startTime = float(f.readline()) sw.elapsedTime = float(f.readline()) while True: l = f.readline() if not l: break i += 1 n, v = l.strip().split(",") sw.lap[n] = float(v) f.close print "Read ", filename else: # except Exception, inst: if i: print "Error while reading line %d of file (partially read):" % (i, ) else: print "Unable to open file (existing stopwatch preserved):" elif c[0] == 'w': if len(c) > 1 and c[1]: filename = c[1] if filename: try: f = open(filename, "w") f.write(title + "\n") f.write(version + "\n"); if sw.running: f.write("running\n") else: f.write("stopped\n") f.write("%3f\n" % (sw.startTime, )) f.write("%3f\n" % (sw.elapsedTime, )) for l in sorted(sw.lap.keys()): f.write('%s,%s\n' % (l, sw.lap[l])) f.close() print "Wrote ", filename except: print "Error writing file ", filename else: print "Error: Please specify filename after 'w '" else: sw.stopLapTimer(command) print " ", command, " is now ", sw.getFormattedTime(command) # except: else: pass