--- /dev/null
+from os import path
+try:
+ import json
+except ImportError:
+ import simplejson as json
+
+class JsonConfig:
+ def __init__(self):
+ self.configfile = path.expanduser('~/.gotovienna/config.json')
+ self.conf = {}
+ if path.exists(self.configfile):
+ with open(self.configfile, 'r') as f:
+ try:
+ self.conf = json.load(f)
+ except ValueError:
+ print "Corrupt config"
+
+ def save(self):
+ with open(self.configfile, 'w') as f:
+ json.dump(self.conf, f)
+
+ def getGpsEnabled(self):
+ if self.conf.has_key('gps_enabled'):
+ return self.conf['gps_enabled']
+ return True
+
+ def setGpsEnabled(self, value):
+ self.conf['gps_enabled'] = value
+ self.save()
+
+ def getLastStationsUpdate(self):
+ if self.conf.has_key('last_stations_update'):
+ return self.conf['last_stations_update']
+ return 'Unknown'
+
+ def setLastStationsUpdate(self, date):
+ self.conf['last_stations_update'] = date
+ self.save()
+
+config = JsonConfig()
--- /dev/null
+from urllib2 import urlopen
+from hashlib import md5
+from datetime import datetime
+import os
+from os import path
+from config import config
+
+CONFIGDIR = path.expanduser('~/.gotovienna')
+STATIONFILENAME = path.join(CONFIGDIR, 'stations.db')
+URLPREFIX = 'http://www.logic.at/people/kelvan/python/gotovienna/downloads/%s'
+REMOTEFILE = 'stations.db'
+REMOTEHASH = 'stations.md5'
+
+def compare_hash(md5sum, fn):
+ localmd5 = md5()
+ md5sum = md5sum.split()[0]
+
+ if not path.exists(fn):
+ # File doesn't exist
+ return False
+
+ with open(fn,'rb') as f:
+ for chunk in iter(lambda: f.read(8192), ''):
+ localmd5.update(chunk)
+ return localmd5.hexdigest() == md5sum
+
+def check_stations_update():
+ """ Check for new version of stations.db
+ return True if new version available
+ return False if not
+ raise exception if unable to check for new version
+ """
+ remote_hash = urlopen(URLPREFIX % REMOTEHASH).read()
+ return not compare_hash(remote_hash, STATIONFILENAME)
+
+def update_stations():
+ if not path.exists(CONFIGDIR):
+ os.mkdir(CONFIGDIR)
+ remote = urlopen(URLPREFIX % REMOTEFILE)
+ with open(STATIONFILENAME, 'wb') as f:
+ f.write(remote.read())
+ config.setLastStationsUpdate(datetime.now().strftime('%c'))
+ return True