Debian Codegraph @

extensions/gpx2garmin/gpx2garmin.py

raw
1 from gi.repository import Gtk
2 import subprocess
3 import re
4
5 class gpx2garmin:
6 def __init__(self, parent = None, pytrainer_main = None, conf_dir = None, options = None):
7 self.limit = options["gpx2garminmaxpoints"]
8 self.device = options["gpx2garmindevice"]
9 self.conf_dir = conf_dir
10 self.gpxfile = None
11 self.tmpgpxfile = "/tmp/_gpx2garmin.gpx"
12 self.pytrainer_main = pytrainer_main
13
14 def prepareGPX(self):
15 ' create an output file and insert a name into the <trk> stanza '
16 f = open(self.tmpgpxfile, 'w')
17 name = 'default'
18 for line in open(self.gpxfile):
19 if re.search('^<name>.+</name>$', line):
20 name = line
21 elif re.search('^<trk>$', line):
22 line = '%s%s' % (line, name)
23 f.write(line)
24 f.close()
25
26 def exportGPX(self):
27 ' export the GPX file using gpsbabel '
28 cmd = ["gpsbabel", "-t", "-i", "gpx", "-f", self.tmpgpxfile, "-o", "garmin"]
29 if self.limit is not None:
30 cmd = cmd + ["-x", "simplify,count=%s" % self.limit]
31 cmd = cmd + ["-F", self.device]
32 return subprocess.call(cmd)
33
34 def run(self, id, activity=None):
35 ' main extension method '
36 self.gpxfile = "%s/gpx/%s.gpx" % (self.conf_dir, id)
37 self.log = "Export "
38 try:
39 self.prepareGPX()
40 if self.exportGPX() == 1:
41 # assume no device present?
42 self.log = self.log + "failed - no device present?"
43 else:
44 self.log = self.log + "succeeded!"
45 except:
46 self.log = self.log + "failed!"
47 md = Gtk.MessageDialog(self.pytrainer_main.windowmain.window1, Gtk.DialogFlags.DESTROY_WITH_PARENT, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, self.log)
48 md.set_title(_("gpx2garmin Extension"))
49 md.set_modal(False)
50 md.run()
51 md.destroy()