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
25
from pytrainer.lib.xmlUtils import XMLParser
26
from pytrainer.gui.dialogs import fileChooserDialog, guiFlush
29
def __init__(self, parent = None, validate=False):
31
self.tmpdir = self.parent.conf.getValue("tmpdir")
32
self.data_path = os.path.dirname(__file__)
33
self.validate = validate
34
self.sport = self.getConfValue("Force_sport_to")
36
def getConfValue(self, confVar):
37
info = XMLParser(self.data_path+"/conf.xml")
38
code = info.getValue("pytrainer-plugin","plugincode")
39
plugindir = self.parent.conf.getValue("plugindir")
40
if not os.path.isfile(plugindir+"/"+code+"/conf.xml"):
43
info = XMLParser(plugindir+"/"+code+"/conf.xml")
44
value = info.getValue("pytrainer-plugin",confVar)
49
# able to select multiple files....
50
selectedFiles = fileChooserDialog(title="Choose a garmintools dump file (or files) to import", multiple=True).getFiles()
53
if not selectedFiles: #Nothing selected
55
for filename in selectedFiles:
56
if self.valid_input_file(filename):
57
#Garmin dump files are not valid xml - need to load into a xmltree
58
#read file into string
59
with open(filename, 'r') as f:
61
fileString = StringIO.StringIO("<root>"+xmlString+"</root>")
63
tree = etree.parse(fileString)
64
if not self.inDatabase(tree):
65
sport = self.getSport(tree)
66
gpxfile = "%s/garmintools-%d.gpx" % (self.tmpdir, len(importfiles))
67
self.createGPXfile(gpxfile, tree)
68
importfiles.append((gpxfile, sport))
70
logging.debug("%s already in database. Skipping import." % (filename,) )
72
logging.info("File %s failed validation" % (filename))
76
def valid_input_file(self, filename):
77
""" Function to validate input file if requested"""
78
if not self.validate: #not asked to validate
79
logging.debug("Not validating %s" % (filename) )
82
print "Cannot validate garmintools dump files yet"
83
logging.debug("Cannot validate garmintools dump files yet")
85
'''xslfile = os.path.realpath(self.parent.parent.data_path)+ "/schemas/GarminTrainingCenterDatabase_v2.xsd"
86
from lib.xmlValidation import xmlValidator
87
validator = xmlValidator()
88
return validator.validateXSL(filename, xslfile)'''
90
def inDatabase(self, tree):
91
#comparing date and start time (sport may have been changed in DB after import)
92
time = self.detailsFromFile(tree)
93
if self.parent.parent.ddbb.select("records","*","date_time_utc=\"%s\"" % (time)):
98
def getSport(self, tree):
99
#return sport from file or overide if present
102
root = tree.getroot()
103
sportElement = root.find(".//run")
105
sport = sportElement.get("sport")
106
sport = sport.capitalize()
111
def detailsFromFile(self, tree):
112
root = tree.getroot()
114
pointElement = root.find(".//point")
115
if pointElement is not None:
116
#Try to get time from point
117
time = pointElement.get("time")
118
print "#TODO first time is different from time used by gpsbabel and has locale embedded: " + time
122
def createGPXfile(self, gpxfile, tree):
123
""" Function to transform a Garmintools dump file to a valid GPX+ file
125
xslt_doc = etree.parse(self.data_path+"/translate.xsl")
126
transform = etree.XSLT(xslt_doc)
127
result_tree = transform(tree)
128
result_tree.write(gpxfile, xml_declaration=True)