Skip to content

Commit

Permalink
Added Unit Based Processors
Browse files Browse the repository at this point in the history
  • Loading branch information
dtcooper committed Feb 2, 2013
1 parent 727258f commit d58006f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
27 changes: 26 additions & 1 deletion fitparse/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions fitparse/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions scripts/generate_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit d58006f

Please sign in to comment.