From 3572ff3f090220021894ed02a7b577b5d286a05e Mon Sep 17 00:00:00 2001 From: Fred Wright Date: Sat, 16 Apr 2016 00:01:13 -0700 Subject: Fixes contrib/webgps.py for Python 3. Since the import fallback hack doesn't work properly in Python 3, this gets rid of it and adds a symlink to make the 'gps' package directory directly available here (similar to what was done in devtools). It then makes a few minor fixes for Python 3. The default Python 3 pickle protocol 3 doesn't work in Python 2, but protocol 2 doesn't work correctly in this case either, so there doesn't seem to be any way to make the tracks file backward-compatible. Thus, it just uses the default protocol and punts at load time if the load fails. This change also fixes the reversed skyview, for consistency with pretty much everything else. There was one case where it crashed with a "dictionary changed size" error that appeared to be due to the delete_stale method's deleting a dictionary entry while scanning it, which is a no no. This has also been fixed. Also adds the result files to .gitignore. TESTED: Ran with both Python 2 and Python 3. This included one run of over three hours without any more crashes. --- contrib/.gitignore | 3 +++ contrib/gps | 1 + contrib/webgps.py | 36 ++++++++++++++++++++++-------------- 3 files changed, 26 insertions(+), 14 deletions(-) create mode 120000 contrib/gps diff --git a/contrib/.gitignore b/contrib/.gitignore index 1ba658af..9a7d311e 100644 --- a/contrib/.gitignore +++ b/contrib/.gitignore @@ -3,3 +3,6 @@ binlog binreplay lla2ecef motosend +gpsd-*.html +gpsd-*.js +tracks.p diff --git a/contrib/gps b/contrib/gps new file mode 120000 index 00000000..3673ce63 --- /dev/null +++ b/contrib/gps @@ -0,0 +1 @@ +../gps \ No newline at end of file diff --git a/contrib/webgps.py b/contrib/webgps.py index 6350f4bc..0a801b5e 100755 --- a/contrib/webgps.py +++ b/contrib/webgps.py @@ -36,12 +36,11 @@ If this file is present on start of webgps.py, it is loaded. This allows to restart webgps.py without losing accumulated satellite tracks. """ +from __future__ import absolute_import, print_function, division + import time, math, sys, os, pickle -try: - from gps import * -except ImportError: - sys.path.append('..') - from gps import * + +from gps import * TRACKMAX = 1024 STALECOUNT = 10 @@ -52,7 +51,8 @@ def polartocart(el, az): radius = DIAMETER * (1 - el / 90.0) # * math.cos(Deg2Rad(float(el))) theta = Deg2Rad(float(az - 90)) return ( - -int(radius * math.cos(theta) + 0.5), # switch sides for a skyview! + # Changed this back to normal orientation - fw + int(radius * math.cos(theta) + 0.5), int(radius * math.sin(theta) + 0.5) ) @@ -83,7 +83,7 @@ class SatTracks(gps): '''gpsd client writing HTML5 and output.''' def __init__(self): - gps.__init__(self) + super(SatTracks, self).__init__() self.sattrack = {} # maps PRNs to Tracks self.state = None self.statetimer = time.time() @@ -112,7 +112,7 @@ class SatTracks(gps): """ % jsfile) sats = self.satellites[:] - sats.sort(lambda a, b: a.PRN - b.PRN) + sats.sort(key=lambda x: x.PRN) for s in sats: fh.write("\t\t\t\t\t%d%d%d%d%s\n" % ( s.PRN, s.elevation, s.azimuth, s.ss, s.used and 'Y' or 'N' @@ -203,8 +203,8 @@ function draw_satview() { ctx.beginPath(); ctx.strokeText('N', -4, -202); - ctx.strokeText('E', -210, 4); - ctx.strokeText('W', 202, 4); + ctx.strokeText('W', -210, 4); + ctx.strokeText('E', 202, 4); ctx.strokeText('S', -4, 210); ctx.strokeStyle = 'grey'; @@ -271,10 +271,13 @@ function draw_satview() { t.stale -= 1 def delete_stale(self): + stales = [] for prn in self.sattrack.keys(): if self.sattrack[prn].stale == 0: - del self.sattrack[prn] + stales.append(prn) self.needsupdate = 1 + for prn in stales: + del self.sattrack[prn] def insert_sat(self, prn, x, y): try: @@ -342,15 +345,20 @@ def main(): # restore the tracks pfile = 'tracks.p' if os.path.isfile(pfile): - p = open(pfile) - sat.sattrack = pickle.load(p) + p = open(pfile, 'rb') + try: + sat.sattrack = pickle.load(p) + except ValueError: + print("Ignoring incompatible tracks file.", file=sys.stderr) p.close() try: sat.run(prefix, period) except KeyboardInterrupt: # save the tracks - p = open(pfile, 'w') + p = open(pfile, 'wb') + # No protocol is backward-compatible from Python 3 to Python 2, + # so we just use the default and punt at load time if needed. pickle.dump(sat.sattrack, p) p.close() -- cgit v1.2.1