1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """
22 Handle converting TCX files to SABX 1.0 files.
23 """
24
25 import os.path
26 import sys
27 import uuid
28
29 from sabx10.oxm import parse_no_def_namespaces
30 from sabx10.templating import TemplateProcessor
31
33 """
34 Process a TCX file, pulling out all its point data for processing. Add in
35 a uuid and title, then process it all through a jinja2 template. This
36 class sub-classes L{TemplateProcessor} to do most of the grunt work in
37 handling command-line options and processing the jinja2 template.
38 """
39
40 - def __init__(self, template_file=None, man=None):
41 """
42 Add C{optparse} options for the infile and the index. Seed
43 template_data with uuid.
44
45 @param template_file: (optional) file name of template file
46 @type template_file: C{string}
47 @param man: (optional) extended program help
48 @type man: C{string}
49 """
50 TemplateProcessor.__init__(self, template_file, man)
51
52 self.parser.add_option("-i", "--infile", dest="in_file",
53 help="input tcx data from FILE",
54 metavar="FILE")
55 self.parser.add_option("-n", "--index", dest="start_index",
56 type="int", default=1,
57 help="starting point index")
58
59 self.template_data['uuid'] = uuid.uuid4()
60
62 """
63 Get the point data from the TCX file and save it in template_data.
64 """
65 if self.options.in_file:
66 self.in_file = open(self.options.in_file, "r")
67 self.template_data['title'] = \
68 os.path.basename(self.options.in_file)
69 else:
70 self.in_file = sys.stdin
71 self.template_data['title'] = "stdin"
72
73 tree = parse_no_def_namespaces(self.in_file)
74 count = self.options.start_index
75 laps = tree.findall('Activities/Activity/Lap')
76 points = []
77 for lap in laps:
78 pts = lap.findall('Track/Trackpoint')
79 for pt in pts:
80 if pt.find('Position') and \
81 pt.findtext('AltitudeMeters'):
82 points.append( {'index': count,
83 'lat': pt.findtext(
84 'Position/LatitudeDegrees'),
85 'lon': pt.findtext(
86 'Position/LongitudeDegrees'),
87 'ele': pt.findtext(
88 'AltitudeMeters')} )
89 count += 1
90 self.template_data['points'] = points
91