1
# -*- coding: iso-8859-1 -*-
3
#Copyright (C) Fiz Vazquez vud1@sindominio.net
6
#This program is free software; you can redistribute it and/or
7
#modify it under the terms of the GNU General Public License
8
#as published by the Free Software Foundation; either version 2
9
#of the License, or (at your option) any later version.
11
#This program is distributed in the hope that it will be useful,
12
#but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
#GNU General Public License for more details.
16
#You should have received a copy of the GNU General Public License
17
#along with this program; if not, write to the Free Software
18
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
from lxml import etree
24
from pytrainer.lib.date import Date
26
#from pytrainer.lib.xmlUtils import XMLParser
27
#from pytrainer.gui.dialogs import fileChooserDialog, guiFlush
28
from pytrainer.lib.system import checkConf
31
def __init__(self, parent = None, data_path = None):
33
self.conf = checkConf()
34
self.tmpdir = self.conf.getValue("tmpdir")
35
self.main_data_path = data_path
36
self.data_path = os.path.dirname(__file__)
38
self.activitiesSummary = []
41
''' Function to return parsed xmlfile '''
44
def getFileType(self):
45
return _("Garmin tools dump file")
47
def getActivitiesSummary(self):
48
return self.activitiesSummary
50
def testFile(self, filename):
52
logging.debug("Testing " + filename)
53
#Check if file is a garmintools dump
55
with open(filename, 'r') as f:
57
#add a root element to make correct xml
58
fileString = StringIO.StringIO("<root>"+xmlString+"</root>")
60
xmldoc = etree.parse(fileString)
62
xmlschema_doc = etree.parse(self.main_data_path+"schemas/garmintools.xsd")
63
xmlschema = etree.XMLSchema(xmlschema_doc)
64
if (xmlschema.validate(xmldoc)):
65
#Valid garmintools file
67
startTime = self.getDateTime(self.startTimeFromFile(xmldoc))
68
indatabase = self.inDatabase(xmldoc, startTime)
69
sport = self.getSport(xmldoc)
70
distance, duration = self.getDetails(xmldoc, startTime)
71
distance = distance / 1000.0
72
self.activitiesSummary.append( (0,
74
startTime[1].strftime("%Y-%m-%dT%H:%M:%S"),
81
#Not garmintools dump file
85
def getDateTime(self, time_):
86
return Date().getDateTime(time_)
88
def inDatabase(self, tree, startTime):
89
#comparing date and start time (sport may have been changed in DB after import)
93
time = time[0].strftime("%Y-%m-%dT%H:%M:%SZ")
94
if self.parent.parent.ddbb.select("records","*","date_time_utc=\"%s\"" % (time)):
99
def getDetails(self, tree, startTime):
100
root = tree.getroot()
101
points = root.findall(".//point")
103
lastPoint = points[-1]
105
distance = lastPoint.get("distance")
109
time = lastPoint.get("time")
114
return float(distance), self.getDateTime(time)[0]-startTime[0]
116
def getSport(self, tree):
117
#return sport from file
118
root = tree.getroot()
119
sportElement = root.find(".//run")
121
sport = sportElement.get("sport")
122
sport = sport.capitalize()
127
def startTimeFromFile(self, tree):
128
root = tree.getroot()
130
pointElement = root.find(".//point")
131
if pointElement is not None:
132
#Try to get time from point
133
time = pointElement.get("time")
134
print "#TODO first time is different from time used by gpsbabel and has locale embedded: " + time
138
def getGPXFile(self, ID):
140
Generate GPX file based on activity ID
142
Returns (sport, GPX filename)
146
if ID == "0": #Only one activity in file
147
gpxFile = "%s/garmintools-%s.gpx" % (self.tmpdir, ID)
148
sport = self.getSport(self.xmldoc)
149
self.createGPXfile(gpxFile, self.xmldoc)
150
return sport, gpxFile
152
def createGPXfile(self, gpxfile, tree):
153
""" Function to transform a Garmintools dump file to a valid GPX+ file
155
xslt_doc = etree.parse(self.data_path+"/translate_garmintools.xsl")
156
transform = etree.XSLT(xslt_doc)
157
result_tree = transform(tree)
158
result_tree.write(gpxfile, xml_declaration=True)