Source

doko / doko / __init__.py

Diff from to

doko/__init__.py

 import time
 import optparse
 from optparse import OptionValueError
-import time
 from collections import namedtuple
 from collections import OrderedDict
 import webbrowser
 import BeautifulSoup
 
 
+DEFAULT_TIMEOUT = 3
+DEFAULT_RETRIES = 10
+
+LOCATION_STRATEGIES = OrderedDict()
+
+CACHE_FILE = os.path.expanduser("~/.doko_cache")
+
+
 class Location(namedtuple('Location', 'latitude longitude')):
     precision = None
 
     def __repr__(self):
         return "%s,%s" % (self.safe_latitude(), self.safe_longitude())
 
-DEFAULT_TIMEOUT = 3
-DEFAULT_RETRIES = 10
-
-LOCATION_STRATEGIES = OrderedDict()
-
-CACHE_FILE = os.path.expanduser("~/.doko_cache")
-
 
 # Important, define strategies in default resolution order
 def location_strategy(name):
     def _(fn):
         LOCATION_STRATEGIES[name] = fn
+        fn._strategy_name = name
     return _
 
 
 class LocationServiceException(Exception):
     pass
 
+
 def write_to_cache(location):
     with open(CACHE_FILE, 'w') as fh:
-        fh.write("%f %s" % (time.time(), location))
+        fh.write(str(location))
+
 
 @location_strategy("cache")
 def cache_location(timeout=DEFAULT_TIMEOUT):
     """
-    Fetch and return current location from a filebacked cache, stored in ~/.doko_cache
+    Fetch and return current location from a filebacked cache, stored in
+    ~/.doko_cache
 
-    Cache is considered value for up to 30 minutes, but refreshed each time it is queried
+    Cache is considered value for up to 30 minutes, but refreshed each time
+    it is queried
     """
     thirty_mins = (60 * 30)
+    if not os.path.exists(CACHE_FILE):
+        return
+
+    last_updated = os.stat(CACHE_FILE).st_mtime
+    if last_updated + thirty_mins < time.time():
+        return
+
     try:
         cache = open(CACHE_FILE).read().strip()
-        timestamp, loc = cache.split(" ")
-        if float(timestamp) + thirty_mins > time.time():
-            lat, lon = loc.split(",")
-            return Location(lat, lon)
-    except IOError:
-        # No cache file, but raising would fail without --force
-        return None
+        lat, lon = map(float, cache.split(","))
+        return Location(lat, lon)
     except ValueError:
         # Invalid content in cache file. Nuke it and start over
-        os.unlink(cache_file)
-        return None
+        os.unlink(CACHE_FILE)
+        return
+
 
 if CoreLocation:
     @location_strategy("corelocation")
 
         if not m.locationServicesEnabled():
             raise LocationServiceException(
-                    'location services not enabled -- check privacy settings in System Preferences'  # nopep8
+                    'location services not enabled -- check privacy settings in System Preferences'  # noqa
                 )
 
         if not m.locationServicesAvailable():
         last_error = e.message
 
     if not l:
-        for _, strategy in LOCATION_STRATEGIES:
+        for strategy_f in remaining_strategies.itervalues():
             try:
-                l = strategy()
+                l = strategy_f()
             except LocationServiceException, e:
                 last_error = e.message
 
     if not l:
         raise LocationServiceException(last_error)
 
-    write_to_cache(l.raw())
+    # success!
+    strategy_name = strategy_f._strategy_name
+
+    if strategy_name != 'cache':
+        write_to_cache(l.raw())
+
+    return l, strategy_name
 
-    return l
 
 def _create_option_parser():
     usage = \
             help='Store geodata with <precision> significant digits')
     parser.add_option('--cache', action='store_true', dest='cache',
             help='Consult a filebacked cache for up to 30 mins')
+    parser.add_option('--show-strategy', action='store_true',
+            help='Include the strategy which succeeded in the output')
 
     return parser
 
         del LOCATION_STRATEGIES['cache']
 
     try:
-        l = location(options.strategy, timeout=options.timeout,
+        l, s = location(options.strategy, timeout=options.timeout,
                 force=options.force)
     except LocationServiceException, e:
         if not options.quiet:
             print >> sys.stderr, e.message
         sys.exit(1)
 
-    print l
+    if options.show_strategy:
+        print l, '(%s)' % s
+    else:
+        print l
 
     if options.show:
         webbrowser.open(
                 'https://maps.google.com/?q=%s' % str(l)
             )
-main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.