added missing py files
authorFlorian Schweikert <kelvan@logic.at>
Thu, 19 Jan 2012 20:51:24 +0000 (21:51 +0100)
committerFlorian Schweikert <kelvan@logic.at>
Thu, 19 Jan 2012 20:51:24 +0000 (21:51 +0100)
gotovienna/config.py [new file with mode: 0644]
gotovienna/update.py [new file with mode: 0644]

diff --git a/gotovienna/config.py b/gotovienna/config.py
new file mode 100644 (file)
index 0000000..a13872d
--- /dev/null
@@ -0,0 +1,40 @@
+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()
diff --git a/gotovienna/update.py b/gotovienna/update.py
new file mode 100644 (file)
index 0000000..00f8c23
--- /dev/null
@@ -0,0 +1,43 @@
+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