tcxkit 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +5 -1
- data/SPECIFICATION.md +5 -5
- data/lib/tcxkit/activity.rb +26 -17
- data/lib/tcxkit/version.rb +1 -1
- data/lib/tcxkit/xml/source.rb +29 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 62fac31d98e0040e2ea328126f899f8f345c2263c804e678d9e017b185d5d813
|
|
4
|
+
data.tar.gz: 7eb8f2f39a4b06131a017bf29377d6106a1df3503e8bb3a2fdde85d115573c14
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 449d6541f113a704564fe6688c6eb9542f8015651f5c85410073242e577145980a9733ab7b867fbfa4133f23f39c515b967c550bc0a9e63e1ef30522892273af
|
|
7
|
+
data.tar.gz: '027529cde9b0c475c34553d5c864e74c9f37bfd3e6a4f24596add4ceaceb9aa2b59003a3d7d6d0844a82ee6a9cf4a8ead6515f3f50612a5599589480d18b29c4'
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.1
|
|
4
|
+
|
|
5
|
+
Fixes from an external review.
|
|
6
|
+
|
|
7
|
+
- `Activity#records` and `Session#records` left out more than the repeated lap boundary trackpoints of decision 5: identical consecutive trackpoints inside one lap, at the end of one session and the start of the next, and in different extension namespaces. Now only a lap's first trackpoint that exactly repeats the previous lap's last one in the same session is left out, and namespaces count when comparing.
|
|
8
|
+
- A UTF-8 byte order mark split across reads, from an IO that hands over a few bytes at a time, made a valid file invalid; so did a UTF-16 file read a byte at a time. The reader now reads such an IO until it has the bytes asked for, and the DTD check waits for a whole byte order mark. Files and `StringIO` were not affected, and a DTD was never parsed.
|
|
9
|
+
- Decision 4 now says that an invalid `Cadence` falls back to the running cadence, as it already did.
|
|
10
|
+
- `bench/read_24h.rb` reproduces the README's timing on a synthetic 24-hour file.
|
|
11
|
+
|
|
3
12
|
## 0.1.0
|
|
4
13
|
|
|
5
14
|
First release: a read-only, streaming reader for Garmin Training Center Database v2 files, with typed messages and fields, enforced limits, refusal of DTDs, and an `Activity` view in the shape of `FitKit::Activity`. Requires Ruby 3.1 and Nokogiri 1.17.
|
data/README.md
CHANGED
|
@@ -89,7 +89,11 @@ Test fixtures must be synthetic. Never commit real activity files: they contain
|
|
|
89
89
|
|
|
90
90
|
## Performance
|
|
91
91
|
|
|
92
|
-
Reading streams, so memory stays flat however long the file is (the `Activity` view keeps every message, by design). A one-hour activity reads in well under a second. A synthetic 24-hour file with a trackpoint every second takes about 11 s of CPU time on a laptop; set `limits: { max_seconds: ... }` for untrusted files.
|
|
92
|
+
Reading streams, so memory stays flat however long the file is (the `Activity` view keeps every message, by design). A one-hour activity reads in well under a second. A synthetic 24-hour file with a trackpoint every second (37 MB) takes about 11 to 14 s of CPU time on a laptop; set `limits: { max_seconds: ... }` for untrusted files. To time it yourself:
|
|
93
|
+
|
|
94
|
+
```sh
|
|
95
|
+
bundle exec ruby -Ilib bench/read_24h.rb
|
|
96
|
+
```
|
|
93
97
|
|
|
94
98
|
## Releasing
|
|
95
99
|
|
data/SPECIFICATION.md
CHANGED
|
@@ -88,7 +88,7 @@ end
|
|
|
88
88
|
|
|
89
89
|
Same contract as `FitKit::Reader`:
|
|
90
90
|
|
|
91
|
-
- `Reader.new(source, close_io: false, source_label: nil, limits: Limits::DEFAULT)`. `source` is a path, a `Pathname` or an IO-like object that responds to `read`. A `Pathname` is read as a path even though it responds to `read`.
|
|
91
|
+
- `Reader.new(source, close_io: false, source_label: nil, limits: Limits::DEFAULT)`. `source` is a path, a `Pathname` or an IO-like object that responds to `read`. A `Pathname` is read as a path even though it responds to `read`. An IO that hands over fewer bytes than asked for (a socket, a decompressing stream) is read again until it has them or ends, so it reads the same as a file.
|
|
92
92
|
- `each_message` streams, and returns an Enumerator without a block.
|
|
93
93
|
- Seekable sources can be iterated again; a non-seekable IO can be read once, and a second iteration raises `Error`.
|
|
94
94
|
- Caller-owned IO is closed only with `close_io: true`.
|
|
@@ -234,9 +234,9 @@ Courses are not activities: their laps and trackpoints are not in `laps` or `rec
|
|
|
234
234
|
|
|
235
235
|
`activity.sport` is the TCX `Sport_t` value. Mapping it to an application's sports is the caller's job.
|
|
236
236
|
|
|
237
|
-
**Cadence** (decision 4). Garmin writes cycling cadence in `Cadence` and running cadence in the Activity Extension (`TPX RunCadence`, `LX AvgRunCadence`). `cadence`, `avg_cadence` and `max_cadence` report whichever is stated, as fitkit's `cadence` does for both sports; the running-specific methods stay available.
|
|
237
|
+
**Cadence** (decision 4). Garmin writes cycling cadence in `Cadence` and running cadence in the Activity Extension (`TPX RunCadence`, `LX AvgRunCadence`). `cadence`, `avg_cadence` and `max_cadence` report whichever is stated, as fitkit's `cadence` does for both sports; the running-specific methods stay available. The `Cadence` element counts only when its value is valid: an out-of-range `Cadence` (`255`) falls back to the running cadence, as gpxkit's sources do, and the invalid field stays on the message with its reason.
|
|
238
238
|
|
|
239
|
-
**Repeated lap boundary points** (decision 5). Garmin repeats the trackpoint at a lap boundary as the last point of one lap and the first of the next. `activity.records` and `session.records` leave out a trackpoint
|
|
239
|
+
**Repeated lap boundary points** (decision 5). Garmin repeats the trackpoint at a lap boundary as the last point of one lap and the first of the next. `activity.records` and `session.records` leave out a lap's first trackpoint when it exactly repeats the last trackpoint of the lap before it in the same session: the same time and the same fields (namespace, path and text, as written, in order). `lap.records` and the messages keep every one. Nothing else is left out: identical trackpoints inside one lap, in different sessions or with any different value are all kept.
|
|
240
240
|
|
|
241
241
|
## 7. Leniency and strictness
|
|
242
242
|
|
|
@@ -318,6 +318,6 @@ Names match fitkit's where the meaning matches, so an application can map all th
|
|
|
318
318
|
1. **Times without a UTC offset.** The schema's `xsd:dateTime` allows them; their time zone is unknown. Decided: `value` nil, `invalid_reason` `:no_utc_offset`, raw text kept, so the gem never guesses a zone.
|
|
319
319
|
2. **Session totals.** TCX has no activity-level totals. Decided: `Session#total_timer_time`, `total_distance` and `total_calories` are the sum of the laps' stated values when every lap states it, otherwise nil; `max_heart_rate` and `max_speed` the largest lap value, under the same rule that every lap states one (refined 2026-11-23: otherwise the largest of the others could understate the session's); averages nil. Documented as sums of stated values.
|
|
320
320
|
3. **Whitespace before the XML declaration.** Decided: skip it, as described in section 7.
|
|
321
|
-
4. **Cadence in the Activity view** (2026-11-23). Checked against FIT exports of the same activities: Garmin's TCX states running cadence only in the Activity Extension, with the same values as FIT's `cadence`. Decided: `cadence`, `avg_cadence` and `max_cadence` report the `Cadence` element, else the running cadence. Accepted by the maintainer on 2026-11-23.
|
|
322
|
-
5. **Repeated lap boundary points** (2026-11-23). Garmin's TCX writes the lap boundary trackpoint in both laps, identically; FIT has it once. Decided: the Activity's and sessions' `records` leave out exact repeats; laps and messages keep them. Accepted by the maintainer on 2026-11-23.
|
|
321
|
+
4. **Cadence in the Activity view** (2026-11-23). Checked against FIT exports of the same activities: Garmin's TCX states running cadence only in the Activity Extension, with the same values as FIT's `cadence`. Decided: `cadence`, `avg_cadence` and `max_cadence` report the `Cadence` element, else the running cadence. Accepted by the maintainer on 2026-11-23. Clarified for 0.1.1 (accepted 2026-11-23): an invalid `Cadence` counts as not stated, so the running cadence is used, as in gpxkit's decision 2.
|
|
322
|
+
5. **Repeated lap boundary points** (2026-11-23). Garmin's TCX writes the lap boundary trackpoint in both laps, identically; FIT has it once. Decided: the Activity's and sessions' `records` leave out exact repeats; laps and messages keep them. Accepted by the maintainer on 2026-11-23. Narrowed for 0.1.1 (accepted 2026-11-23): 0.1.0 also left out identical consecutive trackpoints inside a lap, across sessions, and in different extension namespaces; now only a lap's first trackpoint that repeats the previous lap's last one in the same session is left out.
|
|
323
323
|
6. **Namespace errors** (2026-11-23). A file whose exporter used an undeclared namespace prefix was rejected outright, although libxml2 reads on. Decided: such namespace errors do not stop reading; the elements involved have no namespace, so their values are untyped fields. Accepted by the maintainer on 2026-11-23.
|
data/lib/tcxkit/activity.rb
CHANGED
|
@@ -19,9 +19,9 @@ module TcxKit
|
|
|
19
19
|
#
|
|
20
20
|
# Garmin repeats the trackpoint at a lap boundary as the last point of one
|
|
21
21
|
# lap and the first of the next. Activity#records and Session#records leave
|
|
22
|
-
# out a
|
|
23
|
-
#
|
|
24
|
-
# (decision
|
|
22
|
+
# out a lap's first trackpoint when it exactly repeats the last trackpoint
|
|
23
|
+
# of the lap before it in the same session (same time, identical fields);
|
|
24
|
+
# Lap#records and the messages keep every one (decision 5).
|
|
25
25
|
class Activity
|
|
26
26
|
SUMMARY = %i[
|
|
27
27
|
sport sub_sport total_distance total_elapsed_time total_timer_time total_calories
|
|
@@ -47,6 +47,7 @@ module TcxKit
|
|
|
47
47
|
def initialize(messages)
|
|
48
48
|
@messages = messages.freeze
|
|
49
49
|
records_by_lap = Hash.new { |hash, key| hash[key] = [] }
|
|
50
|
+
repeats = {}.compare_by_identity
|
|
50
51
|
laps_by_activity = Hash.new { |hash, key| hash[key] = [] }
|
|
51
52
|
creators = {}
|
|
52
53
|
@sessions = []
|
|
@@ -63,12 +64,14 @@ module TcxKit
|
|
|
63
64
|
next unless context.key?(:activity) || context.key?(:transition)
|
|
64
65
|
|
|
65
66
|
record = Record.new(message)
|
|
66
|
-
@records << record
|
|
67
|
+
@records << record
|
|
67
68
|
records_by_lap[lap_key(context)] << record
|
|
68
69
|
when :lap
|
|
69
70
|
next unless context.key?(:activity)
|
|
70
71
|
|
|
71
72
|
lap = Lap.new(message, records_by_lap.delete(lap_key(context)) || [])
|
|
73
|
+
previous = laps_by_activity[context[:activity]].last
|
|
74
|
+
repeats[lap.records.first] = true if Activity.boundary_repeat?(previous, lap)
|
|
72
75
|
@laps << lap
|
|
73
76
|
laps_by_activity[context[:activity]] << lap
|
|
74
77
|
when :transition
|
|
@@ -83,6 +86,7 @@ module TcxKit
|
|
|
83
86
|
end
|
|
84
87
|
end
|
|
85
88
|
|
|
89
|
+
@records.reject! { |record| repeats.key?(record) }
|
|
86
90
|
[@sessions, @laps, @transitions, @records, @multi_sport_sessions].each(&:freeze)
|
|
87
91
|
freeze
|
|
88
92
|
end
|
|
@@ -110,19 +114,24 @@ module TcxKit
|
|
|
110
114
|
define_method(name) { session&.public_send(name) }
|
|
111
115
|
end
|
|
112
116
|
|
|
113
|
-
|
|
117
|
+
# True when lap's first trackpoint exactly repeats the last trackpoint of
|
|
118
|
+
# previous, the lap before it in the same session: the same time and the
|
|
119
|
+
# same fields (namespace, path and text, in order). Used by Session;
|
|
120
|
+
# not part of the API.
|
|
121
|
+
def self.boundary_repeat?(previous, lap)
|
|
122
|
+
last = previous&.records&.last
|
|
123
|
+
first = lap.records.first
|
|
124
|
+
return false unless last && first && first.timestamp && last.timestamp == first.timestamp
|
|
114
125
|
|
|
115
|
-
|
|
116
|
-
self.class.repeat?(previous, record)
|
|
126
|
+
identity(last) == identity(first)
|
|
117
127
|
end
|
|
118
128
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return false unless previous && record.timestamp && previous.timestamp == record.timestamp
|
|
122
|
-
|
|
123
|
-
previous.raw_message.fields.map { |field| [field.path, field.text] } ==
|
|
124
|
-
record.raw_message.fields.map { |field| [field.path, field.text] }
|
|
129
|
+
def self.identity(record)
|
|
130
|
+
record.raw_message.fields.map { |field| [field.namespace, field.path, field.text] }
|
|
125
131
|
end
|
|
132
|
+
private_class_method :identity
|
|
133
|
+
|
|
134
|
+
private
|
|
126
135
|
|
|
127
136
|
# Trackpoints belong to the lap or transition whose context they share.
|
|
128
137
|
def lap_key(context)
|
|
@@ -171,11 +180,11 @@ module TcxKit
|
|
|
171
180
|
@raw_message.context[:multi_sport_session]
|
|
172
181
|
end
|
|
173
182
|
|
|
174
|
-
# Every lap's trackpoints, leaving out
|
|
183
|
+
# Every lap's trackpoints, leaving out a lap's first trackpoint when it
|
|
184
|
+
# exactly repeats the previous lap's last one.
|
|
175
185
|
def records
|
|
176
|
-
@laps.flat_map
|
|
177
|
-
|
|
178
|
-
records << record unless previous && Activity.repeat?(previous, record)
|
|
186
|
+
@laps.each_with_index.flat_map do |lap, index|
|
|
187
|
+
index.positive? && Activity.boundary_repeat?(@laps[index - 1], lap) ? lap.records.drop(1) : lap.records
|
|
179
188
|
end
|
|
180
189
|
end
|
|
181
190
|
|
data/lib/tcxkit/version.rb
CHANGED
data/lib/tcxkit/xml/source.rb
CHANGED
|
@@ -76,7 +76,7 @@ module TcxKit
|
|
|
76
76
|
@pending ||= prefix
|
|
77
77
|
return nil if @error
|
|
78
78
|
|
|
79
|
-
data = @pending.empty? ?
|
|
79
|
+
data = @pending.empty? ? read_full(length) : take(length)
|
|
80
80
|
return nil if data.nil? || @error
|
|
81
81
|
|
|
82
82
|
unless @delivered
|
|
@@ -89,6 +89,21 @@ module TcxKit
|
|
|
89
89
|
|
|
90
90
|
private
|
|
91
91
|
|
|
92
|
+
# Reads up to length bytes, reading again when the IO hands over fewer
|
|
93
|
+
# (as a socket or a decompressing stream may) until there are length
|
|
94
|
+
# bytes or the input ends. libxml2 misreads some inputs handed over a
|
|
95
|
+
# byte at a time, such as UTF-16.
|
|
96
|
+
def read_full(length)
|
|
97
|
+
data = read_raw(length)
|
|
98
|
+
while data && data.bytesize < length
|
|
99
|
+
return refuse(time_limit_error) if @clock.exceeded?
|
|
100
|
+
|
|
101
|
+
more = read_raw(length - data.bytesize) or break
|
|
102
|
+
data << more
|
|
103
|
+
end
|
|
104
|
+
@error ? nil : data
|
|
105
|
+
end
|
|
106
|
+
|
|
92
107
|
def read_raw(length)
|
|
93
108
|
return nil if @eof
|
|
94
109
|
|
|
@@ -192,6 +207,8 @@ module TcxKit
|
|
|
192
207
|
# there is no more input, and anything unfinished is left for the XML
|
|
193
208
|
# parser to report.
|
|
194
209
|
def scan(buffer, complete:)
|
|
210
|
+
return :more if !complete && encoding_start?(buffer)
|
|
211
|
+
|
|
195
212
|
utf16 = utf16(buffer)
|
|
196
213
|
return scan(utf16, complete: complete) if utf16
|
|
197
214
|
|
|
@@ -219,6 +236,17 @@ module TcxKit
|
|
|
219
236
|
UTF16_LE_BOM = "\xFF\xFE".b.freeze
|
|
220
237
|
UTF16_BE_BOM = "\xFE\xFF".b.freeze
|
|
221
238
|
UTF32_LE_BOM = "\xFF\xFE\x00\x00".b.freeze
|
|
239
|
+
# How a document can start, decided by its first bytes: a byte order
|
|
240
|
+
# mark, or "<" in UTF-16 big-endian. ("<" alone, which may begin
|
|
241
|
+
# UTF-16 little-endian, is handled as unfinished markup.)
|
|
242
|
+
ENCODING_STARTS = [Source::BOM, UTF16_LE_BOM, UTF16_BE_BOM, UTF32_LE_BOM, "\x00<".b].freeze
|
|
243
|
+
|
|
244
|
+
# True when buffer is only the start of one of ENCODING_STARTS, so an
|
|
245
|
+
# input that hands over a few bytes at a time is read further before
|
|
246
|
+
# deciding what it is.
|
|
247
|
+
def encoding_start?(buffer)
|
|
248
|
+
ENCODING_STARTS.any? { |start| start.bytesize > buffer.bytesize && start.start_with?(buffer) }
|
|
249
|
+
end
|
|
222
250
|
|
|
223
251
|
# A UTF-8 copy of a UTF-16 buffer (by its byte order mark, or by "<"
|
|
224
252
|
# followed or preceded by a zero byte), so a DTD in a UTF-16 document
|