-
Notifications
You must be signed in to change notification settings - Fork 185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add FitFileEncoder for writing FIT files #58
Open
xmedeko
wants to merge
1
commit into
dtcooper:master
Choose a base branch
from
xmedeko:write
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+950
−72
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
#!/usr/bin/env python | ||
import copy | ||
import datetime | ||
import io | ||
import os | ||
import sys | ||
|
||
from fitparse import FitFile | ||
from fitparse.encoder import FitFileEncoder, DataMessageCreator | ||
|
||
if sys.version_info >= (2, 7): | ||
import unittest | ||
else: | ||
import unittest2 as unittest | ||
|
||
|
||
def testfile(filename): | ||
return os.path.join(os.path.dirname(os.path.realpath(__file__)), 'files', filename) | ||
|
||
|
||
class FitFileEncoderTestCase(unittest.TestCase): | ||
|
||
def test_header(self): | ||
file = io.BytesIO() | ||
with FitFileEncoder(file) as fwrite: | ||
fwrite.finish() | ||
buff = file.getvalue() | ||
pass | ||
self.assertTrue(fwrite.completed) | ||
self.assertEqual(16, len(buff)) | ||
|
||
with FitFile(buff) as fread: | ||
self.assertEqual(0, len(fread.messages)) | ||
self.assertEqual(fwrite.protocol_version, fread.protocol_version) | ||
self.assertEqual(fwrite.profile_version, fread.profile_version) | ||
self.assertEqual(fwrite.data_size, fread.data_size) | ||
|
||
def test_basic_activity_create(self): | ||
file = io.BytesIO() | ||
# copy of written messages | ||
messages = [] | ||
time_created = datetime.datetime(2017, 12, 13, 14, 15, 16) | ||
with FitFileEncoder(file) as fwrite: | ||
def write(mesg): | ||
fwrite.write(mesg) | ||
messages.append(copy.deepcopy(mesg.mesg)) | ||
|
||
mesg = DataMessageCreator('file_id') | ||
mesg.set_values(( | ||
('serial_number', 123456), | ||
('manufacturer', 'dynastream'), | ||
('garmin_product', 'hrm1'), # test subfield | ||
('type', 'activity'), | ||
('time_created', time_created) | ||
)) | ||
write(mesg) | ||
|
||
mesg = DataMessageCreator('device_info') | ||
mesg.set_values(( | ||
('manufacturer', 284), | ||
('product', 1), | ||
('product_name', 'unit test') # test string | ||
)) | ||
write(mesg) | ||
|
||
rec_mesg = DataMessageCreator('record', local_mesg_num=1) | ||
rec_mesg.set_values(( | ||
('timestamp', time_created), | ||
('altitude', 100), | ||
('distance', 0) | ||
)) | ||
write(rec_mesg) | ||
|
||
rec_mesg2 = DataMessageCreator('record', local_mesg_num=2) | ||
rec_mesg2.set_values(( | ||
('altitude', 102), | ||
('distance', 2) | ||
)) | ||
rec_mesg2.set_header_timestamp(time_created + datetime.timedelta(seconds=2)) | ||
write(rec_mesg2) | ||
|
||
rec_mesg2.set_values(( | ||
('altitude', 40000), # out of sint16 range | ||
('distance', 4) | ||
)) | ||
rec_mesg2.set_header_timestamp(time_created + datetime.timedelta(seconds=4)) | ||
write(rec_mesg2) | ||
messages[-1].get('altitude').value = None # to conform the assert | ||
|
||
mesg = DataMessageCreator('session') | ||
mesg.set_values(( | ||
('start_time', time_created), | ||
('timestamp', time_created), | ||
('total_distance', 20.5), | ||
('total_ascent', 1234), | ||
('total_descent', 654), | ||
('total_elapsed_time', 3661.5), | ||
('avg_altitude', 821), | ||
('sport', 'cycling'), | ||
('event', 'session'), | ||
('event_type', 'start') | ||
)) | ||
write(mesg) | ||
|
||
fwrite.finish() | ||
buff = file.getvalue() | ||
|
||
with FitFile(buff) as fread: | ||
rmessages = fread.messages | ||
|
||
self._assert_messages(messages, rmessages) | ||
|
||
def test_basic_activity_read_write(self): | ||
# note: 'Activity.fit' has some useless definition messages | ||
with FitFile(testfile('Activity.fit')) as fread: | ||
messages = fread.messages | ||
|
||
file = io.BytesIO() | ||
with FitFileEncoder(file) as fwrite: | ||
for m in messages: | ||
# current encoder can do just basic fields | ||
m.fields = [f for f in m.fields if f.field_def or FitFileEncoder._is_ts_field(f)] | ||
# need to unset raw_value | ||
for field_data in m.fields: | ||
field_data.raw_value = None | ||
fwrite.write(m) | ||
fwrite.finish() | ||
buff = file.getvalue() | ||
|
||
with FitFile(buff) as fread: | ||
messages_buff = fread.messages | ||
|
||
self._assert_messages(messages, messages_buff) | ||
|
||
def _assert_messages(self, expected, actual): | ||
self.assertEqual(len(expected), len(actual), msg='#messages') | ||
for emsg, amsg in zip(expected, actual): | ||
self.assertEqual(emsg.name, amsg.name) | ||
self._assert_message_headers(emsg.header, amsg.header) | ||
self.assertEqual(self._get_header_ts(emsg.fields), self._get_header_ts(amsg.fields), msg='message: {} header timestamp'.format(emsg.name)) | ||
efields = self._filter_fields_for_test(emsg.fields) | ||
afields = self._filter_fields_for_test(amsg.fields) | ||
self.assertEqual(len(efields), len(afields), msg='message: {} #fields'.format(emsg.name)) | ||
for efield, afield in zip(efields, afields): | ||
self.assertEqual(efield.name, afield.name, msg='message: {} field names'.format(emsg.name)) | ||
self.assertEqual(efield.value, afield.value, | ||
msg='message: {}, field: {} values'.format(emsg.name, efield.name)) | ||
|
||
def _assert_message_headers(self, expected, actual): | ||
self.assertEqual(expected.is_definition, actual.is_definition) | ||
self.assertEqual(expected.is_developer_data, actual.is_developer_data) | ||
self.assertEqual(expected.local_mesg_num, actual.local_mesg_num) | ||
self.assertEqual(expected.time_offset, actual.time_offset) | ||
|
||
@staticmethod | ||
def _filter_fields_for_test(fields): | ||
"""Take only base field for the test.""" | ||
return [f for f in fields if f.field_def] | ||
|
||
@staticmethod | ||
def _get_header_ts(fields): | ||
"""Get timestamp related to the compressed header.""" | ||
field_data = next((f for f in fields if f.field_def is None and FitFileEncoder._is_ts_field(f)), None) | ||
return field_data.value if field_data else None | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env python | ||
import datetime | ||
import sys | ||
|
||
from fitparse import FitFileDataProcessor | ||
from fitparse.profile import FIELD_TYPE_TIMESTAMP | ||
from fitparse.records import FieldData | ||
|
||
if sys.version_info >= (2, 7): | ||
import unittest | ||
else: | ||
import unittest2 as unittest | ||
|
||
|
||
class ProcessorsTestCase(unittest.TestCase): | ||
|
||
def test_fitfiledataprocessor(self): | ||
raw_value = 3600 + 60 + 1 | ||
fd = FieldData( | ||
field_def=None, | ||
field=FIELD_TYPE_TIMESTAMP, | ||
parent_field=None, | ||
value=raw_value, | ||
raw_value=raw_value, | ||
) | ||
pr = FitFileDataProcessor() | ||
# local_date_time | ||
pr.process_type_local_date_time(fd) | ||
self.assertEqual(datetime.datetime(1989, 12, 31, 1, 1, 1), fd.value) | ||
pr.unparse_type_local_date_time(fd) | ||
self.assertEqual(raw_value, fd.raw_value) | ||
# localtime_into_day | ||
fd.value = raw_value | ||
fd.raw_value = None | ||
pr.process_type_localtime_into_day(fd) | ||
self.assertEqual(datetime.time(1, 1, 1), fd.value) | ||
pr.unparse_type_localtime_into_day(fd) | ||
self.assertEqual(raw_value, fd.raw_value) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
str
is still used as a typecheck inget_messages
in the following code:However, looking at it now, this code can probably be refactored to:
This would remove the typecheck so the shim wouldn't be needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this code should be removed - the caller should make sure a message number is int, not str if wants to use the number.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pR0Ps Should I remove this from the
FitFile
?