~ubuntu-branches/ubuntu/natty/pytrainer/natty-proposed

« back to all changes in this revision

Viewing changes to plugins/garmintools/garmintools.py

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2010-02-04 06:07:11 UTC
  • mfrom: (4.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100204060711-25n5aw66w5egeiph
Tags: 1.7.1-1ubuntu1
* Merge from debian testing, remaining changes:
  - debian/control:
    + Replace Depends on iceweasel with firefox | abrowser.
    + Bump python-dev,debhelper build-dependencies.
    - Drop dependency on python-glade2 (libglade -> gtkbuilder transition).
  - debian/rules:
    + Append --install-laoyut=deb to setup.py install to prevent a build
      failure with Python 2.6.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: iso-8859-1 -*-
 
2
 
 
3
#Copyright (C) Fiz Vazquez vud1@sindominio.net
 
4
# Modified by dgranda
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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.
 
19
 
 
20
import logging
 
21
import os
 
22
import StringIO
 
23
from lxml import etree
 
24
 
 
25
from pytrainer.lib.xmlUtils import XMLParser
 
26
from pytrainer.gui.dialogs import fileChooserDialog, guiFlush
 
27
 
 
28
class garmintools():
 
29
        def __init__(self, parent = None, validate=False):
 
30
                self.parent = parent
 
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")
 
35
 
 
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"):
 
41
                        value = None
 
42
                else:
 
43
                        info = XMLParser(plugindir+"/"+code+"/conf.xml")
 
44
                        value = info.getValue("pytrainer-plugin",confVar)
 
45
                return value
 
46
 
 
47
        def run(self):
 
48
                logging.debug(">>")
 
49
                # able to select multiple files....
 
50
                selectedFiles = fileChooserDialog(title="Choose a garmintools dump file (or files) to import", multiple=True).getFiles()
 
51
                guiFlush()
 
52
                importfiles = []
 
53
                if not selectedFiles: #Nothing selected
 
54
                        return importfiles
 
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:
 
60
                                        xmlString = f.read()
 
61
                                fileString = StringIO.StringIO("<root>"+xmlString+"</root>")
 
62
                                #parse string as xml
 
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))                                    
 
69
                                else:
 
70
                                        logging.debug("%s already in database. Skipping import." % (filename,) )
 
71
                        else:
 
72
                                logging.info("File %s failed validation" % (filename))
 
73
                logging.debug("<<")
 
74
                return importfiles
 
75
 
 
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) )
 
80
                        return True
 
81
                else:
 
82
                        print "Cannot validate garmintools dump files yet"
 
83
                        logging.debug("Cannot validate garmintools dump files yet")
 
84
                        return True
 
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)'''
 
89
 
 
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)):
 
94
                        return True
 
95
                else:
 
96
                        return False
 
97
 
 
98
        def getSport(self, tree):
 
99
                #return sport from file or overide if present
 
100
                if self.sport:
 
101
                        return self.sport
 
102
                root = tree.getroot()
 
103
                sportElement = root.find(".//run")
 
104
                try:
 
105
                        sport = sportElement.get("sport")
 
106
                        sport = sport.capitalize()
 
107
                except:
 
108
                        sport = "import"
 
109
                return sport
 
110
 
 
111
        def detailsFromFile(self, tree):
 
112
                root = tree.getroot()
 
113
                #Find first point
 
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
 
119
                        return time
 
120
                return None
 
121
 
 
122
        def createGPXfile(self, gpxfile, tree):
 
123
                """ Function to transform a Garmintools dump file to a valid GPX+ file
 
124
                """
 
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)
 
129