Permalink
Browse files

Added Sample `fitdump` Script

  • Loading branch information...
1 parent bfb440a commit 1d759156951a3b1a42bbc3bc13a1a9060bff43e6 @dtcooper committed Dec 10, 2012
Showing with 48 additions and 9 deletions.
  1. +2 −0 MANIFEST.in
  2. +4 −8 fitparse/records.py
  3. +41 −0 scripts/fitdump
  4. +1 −1 setup.py
View
@@ -0,0 +1,2 @@
+include LICENSE
+include README.md
View
@@ -102,6 +102,9 @@ def as_dict(self):
'fields': [f.as_dict() for f in self.fields],
}
+ def __iter__(self):
+ return iter(self.fields)
+
def __repr__(self):
return '<DataMessage: %s (#%d) -- local mesg: #%d, fields: [%s]>' % (
self.name, self.mesg_num, self.local_mesg_num,
@@ -165,14 +168,7 @@ def as_dict(self):
'raw_value': self.raw_value,
}
- def __iter__(self):
- for field in sorted(
- self.fields, key=lambda f: (int(f.name is None), f.name, f.def_num),
- ):
- if not field.components:
- yield field
-
- def __str__(self):
+ def __str__(self): # TODO: not sure I like this
return '%s: %s%s' % (
self.name if self.name else 'unknown-%d' % self.def_num,
self.value, ' [%s]' % self.units if self.units else '',
View
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+
+import os
+import sys
+
+from fitparse import FitFile, FitParseError
+
+
+def dump(filename):
+ try:
+ fitfile = FitFile(filename)
+
+ for data_message in fitfile:
+ print "%s (mesg num: #%d)" % (data_message.name, data_message.mesg_num)
+ for field_data in sorted(
+ data_message,
+ key=lambda fd: (
+ # Sort by known fields first
+ int(fd.name.startswith('unknown')),
+ # Then by def num if known, otherwise name
+ fd.def_num if fd.name.startswith('unknown') else fd.name,
+ ),
+ ):
+ print " * %s" % field_data
+ print
+
+ except FitParseError, e:
+ print "Error parsing %s: %s" % (filename, e)
+
+
+if __name__ == '__main__':
+ if len(sys.argv) < 2:
+ print "Usage: %s <file.fit> [<file2.fit> ...]" % os.path.basename(__file__)
+ sys.exit(0)
+
+ for filename in sys.argv[1:]:
+ print "%(hashes)s\n# %(filename)s #\n%(hashes)s\n" % {
+ 'filename': filename, 'hashes': '#' * (len(filename) + 4),
+ }
+ dump(filename)
+ print
View
@@ -9,5 +9,5 @@
url='http://www.github.com/dtcooper/python-fitparse',
license=open('LICENSE').read(),
packages=['fitparse'],
- data_files=[("", ["LICENSE"])],
+ scripts=['scripts/fitdump'], # Don't include generate_profile.py
)

0 comments on commit 1d75915

Please sign in to comment.