From: <jb...@us...> - 2009-10-22 09:18:16
|
Revision: 373 http://pytrainer.svn.sourceforge.net/pytrainer/?rev=373&view=rev Author: jblance Date: 2009-10-22 09:18:04 +0000 (Thu, 22 Oct 2009) Log Message: ----------- Added some support for determining sport on import to plugins branch Modified Paths: -------------- pytrainer/branches/plugins-v2/plugins/garmin-gpx/conf.xml pytrainer/branches/plugins-v2/plugins/garmin-gpx/garmingpx.py pytrainer/branches/plugins-v2/plugins/garmin-hr/garminhr.py pytrainer/branches/plugins-v2/plugins/garmin-hr-file/conf.xml pytrainer/branches/plugins-v2/plugins/garmin-hr-file/garminhrfile.py pytrainer/branches/plugins-v2/plugins/garmin-tcxv2/conf.xml pytrainer/branches/plugins-v2/plugins/garmin-tcxv2/garmin-tcxv2.py pytrainer/branches/plugins-v2/pytrainer/lib/xmlUtils.py pytrainer/branches/plugins-v2/pytrainer/main.py pytrainer/branches/plugins-v2/pytrainer/record.py Modified: pytrainer/branches/plugins-v2/plugins/garmin-gpx/conf.xml =================================================================== --- pytrainer/branches/plugins-v2/plugins/garmin-gpx/conf.xml 2009-10-21 09:50:08 UTC (rev 372) +++ pytrainer/branches/plugins-v2/plugins/garmin-gpx/conf.xml 2009-10-22 09:18:04 UTC (rev 373) @@ -6,5 +6,5 @@ pluginbutton="Import from Garmin GPX file" executable="garmingpx" > -<!--<conf-values variable="device" value="/dev/ttyUSB0"/>--> +<conf-values variable="Force_sport_to" value=""/> </pytrainer-plugin> Modified: pytrainer/branches/plugins-v2/plugins/garmin-gpx/garmingpx.py =================================================================== --- pytrainer/branches/plugins-v2/plugins/garmin-gpx/garmingpx.py 2009-10-21 09:50:08 UTC (rev 372) +++ pytrainer/branches/plugins-v2/plugins/garmin-gpx/garmingpx.py 2009-10-22 09:18:04 UTC (rev 373) @@ -20,8 +20,10 @@ import logging import os +import shutil from gui.dialogs import fileChooserDialog, guiFlush import xml.etree.cElementTree +from lib.xmlUtils import XMLParser class garmingpx(): """ Plugin to import from a GPX file or files @@ -32,16 +34,35 @@ self.parent = parent self.tmpdir = self.parent.conf.getValue("tmpdir") self.validate = validate + self.data_path = os.path.dirname(__file__) + self.sport = self.getConfValue("Force_sport_to") + def getConfValue(self, confVar): + info = XMLParser(self.data_path+"/conf.xml") + code = info.getValue("pytrainer-plugin","plugincode") + plugindir = self.parent.conf.getValue("plugindir") + if not os.path.isfile(plugindir+"/"+code+"/conf.xml"): + value = None + else: + info = XMLParser(plugindir+"/"+code+"/conf.xml") + value = info.getValue("pytrainer-plugin",confVar) + return value + def run(self): logging.debug(">>") selectedFiles = fileChooserDialog(title="Choose a GPX file (or files) to import", multiple=True).getFiles() guiFlush() importfiles = [] + if not selectedFiles: + return importfiles for filename in selectedFiles: if self.valid_input_file(filename): if not self.inDatabase(filename): - importfiles.append(filename) + sport = self.getSport(filename) + gpxfile = "%s/garmin-gpx-%d.gpx" % (self.tmpdir, len(importfiles)) + shutil + shutil.copy(filename, gpxfile) + importfiles.append((gpxfile, sport)) else: logging.debug("%s already in database. Skipping import." % (filename) ) else: @@ -73,6 +94,13 @@ else: return False + def getSport(self, filename): + #return sport from overide if present or default to "import" + if self.sport: + return self.sport + sport = "import" + return sport + def detailsFromGPX(self, filename): """ Function to return the first time element from a GPX 1.1 file """ tree = xml.etree.cElementTree.ElementTree(file=filename) Modified: pytrainer/branches/plugins-v2/plugins/garmin-hr/garminhr.py =================================================================== --- pytrainer/branches/plugins-v2/plugins/garmin-hr/garminhr.py 2009-10-21 09:50:08 UTC (rev 372) +++ pytrainer/branches/plugins-v2/plugins/garmin-hr/garminhr.py 2009-10-22 09:18:04 UTC (rev 373) @@ -36,8 +36,20 @@ self.tmpdir = self.parent.conf.getValue("tmpdir") self.data_path = os.path.dirname(__file__) self.validate = validate - self.input_dev = None + self.input_dev = self.getConfValue("device") + self.sport = self.getConfValue("Force_sport_to") + def getConfValue(self, confVar): + info = XMLParser(self.data_path+"/conf.xml") + code = info.getValue("pytrainer-plugin","plugincode") + plugindir = self.parent.conf.getValue("plugindir") + if not os.path.isfile(plugindir+"/"+code+"/conf.xml"): + value = None + else: + info = XMLParser(plugindir+"/"+code+"/conf.xml") + value = info.getValue("pytrainer-plugin",confVar) + return value + def run(self): logging.debug(">>") importfiles = [] @@ -59,9 +71,10 @@ logging.debug("Found %d tracks in %s" % (len(tracks), gpsbabelOutputFile)) for track in tracks: #can be multiple tracks if self.shouldImport(track): + sport = self.getSport(track) #TODO need to fix this logic.... gpxfile = "%s/garminhrfile%d.gpx" % (self.tmpdir, len(importfiles)) self.createGPXfile(gpxfile, track) - importfiles.append(gpxfile) + importfiles.append((gpxfile, sport)) logging.debug("Importing %s of %s tracks" % (len(importfiles), len(tracks)) ) else: logging.info("File %s failed validation" % (gpsbabelOutputFile)) @@ -90,11 +103,6 @@ return False def garminDeviceExists(self): - info = XMLParser(self.data_path+"/conf.xml") - prefs = info.getAllValues("conf-values") - for (variable, value) in prefs: - if variable == "device": - self.input_dev = value try: outmod = commands.getstatusoutput('/sbin/lsmod | grep garmin_gps') if outmod[0]==256: #there is no garmin_gps module loaded @@ -142,6 +150,13 @@ else: return True + def getSport(self, track): + #TODO return sport + if self.sport: + return self.sport + else: + return "import" + def createGPXfile(self, gpxfile, track): """ Function to transform a Garmin Training Center v1 Track to a valid GPX+ file """ Modified: pytrainer/branches/plugins-v2/plugins/garmin-hr-file/conf.xml =================================================================== --- pytrainer/branches/plugins-v2/plugins/garmin-hr-file/conf.xml 2009-10-21 09:50:08 UTC (rev 372) +++ pytrainer/branches/plugins-v2/plugins/garmin-hr-file/conf.xml 2009-10-22 09:18:04 UTC (rev 373) @@ -1,9 +1,10 @@ <?xml version="1.0" ?> <pytrainer-plugin - name="Garmin Import with HR (from TCXv1file)" - description="Import records from a garmin gps device with heart rate support (from TCXv1 file)" + name="Garmin training center file import (version 1)" + description="Import records from a garmin gps device with heart rate support (from training center version 1 file)" plugincode="garminhrfile" - pluginbutton="Import from Garmin file (HR support)" + pluginbutton="Import from Garmin training center file (version 1)" executable="garminhrfile" > +<conf-values variable="Force_sport_to" value=""/> </pytrainer-plugin> Modified: pytrainer/branches/plugins-v2/plugins/garmin-hr-file/garminhrfile.py =================================================================== --- pytrainer/branches/plugins-v2/plugins/garmin-hr-file/garminhrfile.py 2009-10-21 09:50:08 UTC (rev 372) +++ pytrainer/branches/plugins-v2/plugins/garmin-hr-file/garminhrfile.py 2009-10-22 09:18:04 UTC (rev 373) @@ -22,6 +22,7 @@ import os import logging from lxml import etree +from lib.xmlUtils import XMLParser from gui.dialogs import fileChooserDialog, guiFlush @@ -39,7 +40,19 @@ self.tmpdir = self.parent.conf.getValue("tmpdir") self.data_path = os.path.dirname(__file__) self.validate = validate + self.sport = self.getConfValue("Force_sport_to") + def getConfValue(self, confVar): + info = XMLParser(self.data_path+"/conf.xml") + code = info.getValue("pytrainer-plugin","plugincode") + plugindir = self.parent.conf.getValue("plugindir") + if not os.path.isfile(plugindir+"/"+code+"/conf.xml"): + value = None + else: + info = XMLParser(plugindir+"/"+code+"/conf.xml") + value = info.getValue("pytrainer-plugin",confVar) + return value + def run(self): logging.debug(">>") selectedFiles = fileChooserDialog(title="Choose a Garmin Training Center file to import").getFiles() @@ -53,9 +66,10 @@ logging.debug("Found %d tracks in %s" % (len(tracks), filename)) for track in tracks: #can be multiple tracks if self.shouldImport(track): + sport = self.getSport(track) #TODO need to fix this logic.... gpxfile = "%s/garminhrfile%d.gpx" % (self.tmpdir, len(importfiles)) self.createGPXfile(gpxfile, track) - importfiles.append(gpxfile) + importfiles.append((gpxfile, sport)) logging.debug("Importing %s of %s tracks" % (len(importfiles), len(tracks)) ) else: logging.info("File %s failed validation" % (filename)) @@ -91,6 +105,13 @@ else: return True + def getSport(self, track): + #TODO return sport + if self.sport: + return self.sport + else: + return "import" + def getTracks(self, filename): """ Function to return all the tracks in a Garmin Training Center v1 file """ Modified: pytrainer/branches/plugins-v2/plugins/garmin-tcxv2/conf.xml =================================================================== --- pytrainer/branches/plugins-v2/plugins/garmin-tcxv2/conf.xml 2009-10-21 09:50:08 UTC (rev 372) +++ pytrainer/branches/plugins-v2/plugins/garmin-tcxv2/conf.xml 2009-10-22 09:18:04 UTC (rev 373) @@ -6,4 +6,5 @@ pluginbutton="Import from Garmin TCX file (version 2)" executable="garmin-tcxv2" > +<conf-values variable="Force_sport_to" value=""/> </pytrainer-plugin> Modified: pytrainer/branches/plugins-v2/plugins/garmin-tcxv2/garmin-tcxv2.py =================================================================== --- pytrainer/branches/plugins-v2/plugins/garmin-tcxv2/garmin-tcxv2.py 2009-10-21 09:50:08 UTC (rev 372) +++ pytrainer/branches/plugins-v2/plugins/garmin-tcxv2/garmin-tcxv2.py 2009-10-22 09:18:04 UTC (rev 373) @@ -20,6 +20,7 @@ import logging import os from lxml import etree +from lib.xmlUtils import XMLParser from gui.dialogs import fileChooserDialog, guiFlush @@ -29,7 +30,19 @@ self.tmpdir = self.parent.conf.getValue("tmpdir") self.data_path = os.path.dirname(__file__) self.validate = validate + self.sport = self.getConfValue("Force_sport_to") + def getConfValue(self, confVar): + info = XMLParser(self.data_path+"/conf.xml") + code = info.getValue("pytrainer-plugin","plugincode") + plugindir = self.parent.conf.getValue("plugindir") + if not os.path.isfile(plugindir+"/"+code+"/conf.xml"): + value = None + else: + info = XMLParser(plugindir+"/"+code+"/conf.xml") + value = info.getValue("pytrainer-plugin",confVar) + return value + def run(self): logging.debug(">>") # able to select multiple files.... @@ -41,9 +54,10 @@ for filename in selectedFiles: if self.valid_input_file(filename): if not self.inDatabase(filename): + sport = self.getSport(filename) gpxfile = "%s/garmin-tcxv2-%d.gpx" % (self.tmpdir, len(importfiles)) self.createGPXfile(gpxfile, filename) - importfiles.append(gpxfile) + importfiles.append((gpxfile, sport)) else: logging.debug("%s already in database. Skipping import." % (filename,) ) else: @@ -70,6 +84,19 @@ else: return False + def getSport(self, filename): + #return sport from file or overide if present + if self.sport: + return self.sport + tree = etree.ElementTree(file=filename) + root = tree.getroot() + sportElement = root.find(".//{http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2}Activity") + try: + sport = sportElement.get("Sport") + except: + sport = "import" + return sport + def detailsFromTCX(self, filename): tree = etree.ElementTree(file=filename) root = tree.getroot() Modified: pytrainer/branches/plugins-v2/pytrainer/lib/xmlUtils.py =================================================================== --- pytrainer/branches/plugins-v2/pytrainer/lib/xmlUtils.py 2009-10-21 09:50:08 UTC (rev 372) +++ pytrainer/branches/plugins-v2/pytrainer/lib/xmlUtils.py 2009-10-22 09:18:04 UTC (rev 373) @@ -75,8 +75,11 @@ def getValue(self,tagname,variable): self._load() - root = self.xmldoc.getElementsByTagName(tagname)[0] - value = root.attributes[variable].value + try: + root = self.xmldoc.getElementsByTagName(tagname)[0] + value = root.attributes[variable].value + except KeyError: + value = "" return value def getAllValues(self,tagname): Modified: pytrainer/branches/plugins-v2/pytrainer/main.py =================================================================== --- pytrainer/branches/plugins-v2/pytrainer/main.py 2009-10-21 09:50:08 UTC (rev 372) +++ pytrainer/branches/plugins-v2/pytrainer/main.py 2009-10-22 09:18:04 UTC (rev 373) @@ -172,14 +172,14 @@ def runPlugin(self,widget,pathPlugin): logging.debug('>>') self.pluginClass = self.plugins.importClass(pathPlugin) - pluginFiles = self.pluginClass.run() + pluginFiles = self.pluginClass.run() if pluginFiles is not None: - logging.debug("Plugin returned " +str(len(pluginFiles)) + " files: " +','.join(pluginFiles) ) + logging.debug("Plugin returned %d files" % (len(pluginFiles)) ) #process returned GPX files - for pluginFile in pluginFiles: + for (pluginFile, sport) in pluginFiles: if os.path.isfile(pluginFile): - logging.info('File exists. Size: '+ str(os.path.getsize(pluginFile))) - self.record.importFromGPX(pluginFile) + logging.info('File exists. Size: %d. Sport: %s' % (os.path.getsize(pluginFile), sport)) + self.record.importFromGPX(pluginFile, sport) else: logging.error('File '+pluginFile+' not valid') else: Modified: pytrainer/branches/plugins-v2/pytrainer/record.py =================================================================== --- pytrainer/branches/plugins-v2/pytrainer/record.py 2009-10-21 09:50:08 UTC (rev 372) +++ pytrainer/branches/plugins-v2/pytrainer/record.py 2009-10-22 09:18:04 UTC (rev 373) @@ -411,14 +411,15 @@ self.recordwindow.run() logging.debug('<<') - def importFromGPX(self, gpxFile): + def importFromGPX(self, gpxFile, sport): """ Add a record from a valid pytrainer type GPX file """ logging.debug('>>') logging.info('Retrieving data from '+gpxFile) - #create an entry, defaulting sport to 'import' - in future could get actual sport from gpxFile.... - entry = ["import",""] + if not sport: + sport = "import" + entry = [sport,""] entry_id = self.insertNewRecord(gpxFile, entry) if entry_id is None: logging.error("Entry not created for file %s" % gpxFile) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |