Permalink
Browse files

Added Unit Based Processors

  • Loading branch information...
dtcooper committed Jan 27, 2013
1 parent 727258f commit d58006ffabd38979057d54bcc0c974433350a60d
Showing with 29 additions and 5 deletions.
  1. +26 −1 fitparse/base.py
  2. +1 −2 fitparse/processors.py
  3. +2 −2 scripts/generate_profile.py
View
@@ -19,6 +19,14 @@ class FitParseError(Exception):
class FitFile(object):
+ # TODO: unit test to make sure that all units in profile.py convert to
+ # sane function names after applying replacements (and there are no
+ # no regressions)
+ UNIT_NAME_TO_FUNC_REPLACEMENTS = (
+ ('/', 'per'),
+ ('%', 'percent'),
+ )
+
def __init__(self, fileish, check_crc=True, data_processor=None):
if hasattr(fileish, 'read'):
self._file = fileish
@@ -269,6 +277,8 @@ def _parse_data_message(self, header):
raw_values = self._parse_raw_values_from_data_message(def_mesg)
field_datas = [] # TODO: I don't love this name, update on DataMessage too
+ # TODO: Maybe refactor this and make it simpler (or at least broken
+ # up into sub-functions)
for field_def, raw_value in zip(def_mesg.field_defs, raw_values):
field, parent_field = field_def.field, None
if field:
@@ -346,15 +356,30 @@ def _parse_data_message(self, header):
# Apply data processors
for field_data in field_datas:
- # Apply type processor
+ # Apply type name processor
type_processor = getattr(self._processor, 'process_type_%s' % field_data.type.name, None)
if type_processor:
type_processor(field_data)
+ # Apply field name processor
field_processor = getattr(self._processor, 'process_field_%s' % field_data.name, None)
if field_processor:
field_processor(field_data)
+ # Apply units name processor
+ if field_data.units:
+ process_func_name = 'process_units_%s' % field_data.units
+ # Do unit name replacements padded with spaces
+ for replace_from, replace_to in self.UNIT_NAME_TO_FUNC_REPLACEMENTS:
+ process_func_name = process_func_name.replace(
+ replace_from, ' %s ' % replace_to,
+ )
+ # Then strip and convert spaces to underscores
+ process_func_name = process_func_name.strip().replace(' ', '_')
+ units_processor = getattr(self._processor, process_func_name, None)
+ if units_processor:
+ units_processor(field_data)
+
data_message = DataMessage(header=header, def_mesg=def_mesg, fields=field_datas)
mesg_processor = getattr(self._processor, 'process_message_%s' % def_mesg.name, None)
View
@@ -35,8 +35,7 @@ def process_field_speed(self, field_data):
field_data.value *= 60.0 * 60.0 / 1000.0
field_data.units = 'km/h'
- def _latlng_fixer(self, field_data):
+ def process_units_semicircles(self, field_data):
if field_data.value is not None:
field_data.value *= 180.0 / (2 ** 31)
field_data.units = 'deg'
- process_field_position_long = process_field_position_lat = _latlng_fixer
@@ -258,8 +258,8 @@ def fix_scale(data):
def fix_units(data):
if data == 'kcal / min':
- return 'kcal/min'
- return data
+ data = 'kcal/min'
+ return data.strip()
def parse_csv_fields(data, num_expected_if_empty):

0 comments on commit d58006f

Please sign in to comment.