Permalink
Browse files

Code cleanup

* Writer closes files before calling a hook

Should help with #17
  • Loading branch information...
1 parent f79d895 commit 7589729c1ed26d288c82ad046d12aef8ef107f36 @mlt committed Jul 31, 2012
View
0 src/core/__init__.py
No changes.
View
0 src/commands.py → src/core/commands.py
File renamed without changes.
View
10 src/device.py → src/core/device.py
@@ -88,15 +88,15 @@ def connect(self):
def _read_tracks(self, writer, count):
tracks_with_points = 0
- for track in range(count):
+ for i in range(count):
track = self.reader.read_track()
tracks_with_points += (track['Points']>0)
_log.info("There are %d laps in %s" % (track['Laps'], track['Track']))
# _log.debug("Testing", extra = track)
writer.add_track(track)
- for theLap in range(track['Laps']):
+ for j in range(track['Laps']):
lap = self.reader.read_lap()
lap['Track'] = track['Track']
# _log.debug("Lap={:d}, Beats={:d}, Elevation={:f}".format(lap['Lap'], lap['Beats'], lap['Elevation']))
@@ -115,11 +115,11 @@ def read(self, writer, progress=None):
# now all track points
# for tracks containing them only!
for track in range(tracks_with_points):
- if progress:
- progress.track(track, tracks_with_points)
summary = self.reader.read_points_summary()
_log.info("Fetching %d points from %s" % (summary['Points'], summary['Track']))
- writer.begin_points(summary['Track'])
+ if progress:
+ progress.track(summary['Track'], track+1, tracks_with_points, summary['Points'])
+ writer.begin_points(summary)
for thePoint in range(summary['Points']):
if progress:
View
4 src/progress_text.py → src/core/progress_text.py
@@ -4,8 +4,8 @@
class TextProgress:
- def track(self, at, end):
- print("Fetching track {:d}/{:d}".format(at, end))
+ def track(self, name, at, end, points):
+ print("Fetching track {:s} [{:d}/{:d}] with {:d} points".format(name, at, end, points))
def point(self, at, end):
if at % 100 == 0:
View
0 src/reader.py → src/core/reader.py
File renamed without changes.
View
0 src/reader_cresta.py → src/core/reader_cresta.py
File renamed without changes.
View
0 src/reader_schwinn.py → src/core/reader_schwinn.py
File renamed without changes.
View
0 src/utils.py → src/core/utils.py
File renamed without changes.
View
66 src/writer.py → src/core/writer_csv.py
@@ -1,84 +1,90 @@
""" Simple CSV writer for Schwinn 810 """
-from __future__ import print_function
import sys,os
import subprocess
import csv
import logging
_log = logging.getLogger(__name__)
-open_extra = dict();
+open_extra = {}
if sys.version_info >= (3,0):
open_extra["newline"] = ''
class Writer:
""" Default files writer """
+ track_keys = ["Start", "End", "Laps", "MaxHeart", "Heart", "MaxSpeed", \
+ "Speed", "x4", "x5", "Points", "Track"]
+ lap_keys = ["Time", "Speed", "Lap", "Distance", "kcal", "MaxSpeed", \
+ "autolap", "Beats", "sec", "MaxHeart", "MinHeart", \
+ "InZone", "y4", "Elevation", "Track"]
+ point_keys = ["Distance", "Speed", "Time", "Heart", "x1", "InZone", \
+ "Latitude", "Longitude", "kcal", "Elevation", "No", "Track"]
+ waypoint_keys = ["Timestamp", "Name", "Latitude", "Longitude","x1","x2","Elevation","No"]
+
def __init__(self, dir, hook=None):
self.dir = dir
self.hook = hook
+
+ self.lapFile = None
+ self.lapWriter = None
- self.ptsWriters = {}
- self.pts_count = {}
+ self.ptsFile = None
+ self.ptsWriter = None
name = os.path.join(self.dir, "waypoints.csv")
wptFile = open(name, "wb", **open_extra)
- waypoint_keys = ["Timestamp", "Name", "Latitude", "Longitude","x1","x2","Elevation","No"]
- self.wptWriter = csv.DictWriter(wptFile, waypoint_keys)
+ self.wptWriter = csv.DictWriter(wptFile, self.waypoint_keys)
self.wptWriter.writeheader()
def add_track(self, track):
""" Append track to database """
name = os.path.join(self.dir, track['Track'])
trkFile = open('%s.track' % name, "wb", **open_extra)
- track_keys = ["Start", "End", "Laps", "MaxHeart", "Heart", "MaxSpeed", \
- "Speed", "x4", "x5", "Points", "Track"]
- trkWriter = csv.DictWriter(trkFile, track_keys)
+ trkWriter = csv.DictWriter(trkFile, self.track_keys)
trkWriter.writeheader()
trkWriter.writerow(track)
trkFile.close()
- lapFile = open('%s.laps' % name, "wb", **open_extra)
- lap_keys = ["Time", "Speed", "Lap", "Distance", "kcal", "MaxSpeed", \
- "autolap", "Beats", "sec", "MaxHeart", "MinHeart", \
- "InZone", "y4", "Elevation", "Track"]
- self.lapWriter = csv.DictWriter(lapFile, lap_keys)
+ self.lapFile = open('%s.laps' % name, "wb", **open_extra)
+ self.lapWriter = csv.DictWriter(self.lapFile, self.lap_keys)
self.lapWriter.writeheader()
- point_keys = ["Distance", "Speed", "Time", "Heart", "x1", "InZone", \
- "Latitude", "Longitude", "kcal", "Elevation", "No", "Track"]
- if track['Points']>0:
- ptsFile = open('%s.points' % name, "wb", **open_extra)
- ptsWriter = csv.DictWriter(ptsFile, point_keys)
- ptsWriter.writeheader()
- name = track['Track']
- self.ptsWriters[name] = ptsWriter
- self.pts_count[name] = track['Points']
-
def add_lap(self, lap):
""" Append lap to a database """
self.lapWriter.writerow(lap)
- def begin_points(self, track):
+ def begin_points(self, summary):
""" Points for track will follow """
+ track = summary['Track']
+ if self.lapFile:
+ self.lapFile.close()
+ self.lapFile = None
self.track = track
+ name = os.path.join(self.dir, track)
+ self.ptsFile = open('%s.points' % name, "wb", **open_extra)
+ self.ptsWriter = csv.DictWriter(self.ptsFile, self.point_keys)
+ self.ptsWriter.writeheader()
def add_point(self, point):
""" Append point to a database """
- self.ptsWriters[self.track].writerow(point)
-
- def add_waypoint(self, wp):
- """ Append point to a database """
- self.wptWriter.writerow(wp)
+ self.ptsWriter.writerow(point)
def commit(self):
""" Commit last track in a database """
+ if self.ptsFile:
+ self.ptsFile.close()
+ self.ptsFile = None
if self.hook:
name = os.path.join(self.dir, self.track)
_log.info("Calling %s\n\twith %s", self.hook, name)
subprocess.Popen([self.hook, name])
+ def add_waypoint(self, wp):
+ """ Append point to a database """
+ self.wptWriter.writerow(wp)
+
def __del__(self):
# self.lapFile.close()
_log.info("Writer destroyed")
View
8 src/download.py
@@ -1,8 +1,8 @@
#!/usr/bin/env python
from __future__ import print_function
-from device import Device, SerialException
-from writer import Writer
-from progress_text import TextProgress
+from core.device import Device, SerialException
+from core.writer_csv import Writer
+from core.progress_text import TextProgress
import argparse, os
import logging
@@ -52,5 +52,5 @@ def main():
if __name__ == '__main__':
# FORMAT = FORMAT = '%(asctime)-15s %(message)s'
# logging.basicConfig(level=logging.DEBUG, format=FORMAT)
- logging.basicConfig(level=logging.DEBUG)
+ logging.basicConfig(level=logging.WARNING)
main()

0 comments on commit 7589729

Please sign in to comment.