From 53f32961e27322d0d687d1be5aeb342219a8d6e4 Mon Sep 17 00:00:00 2001 From: Florian Schweikert Date: Thu, 19 Jan 2012 21:51:24 +0100 Subject: [PATCH] added missing py files --- gotovienna/config.py | 40 ++++++++++++++++++++++++++++++++++++++++ gotovienna/update.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 gotovienna/config.py create mode 100644 gotovienna/update.py diff --git a/gotovienna/config.py b/gotovienna/config.py new file mode 100644 index 0000000..a13872d --- /dev/null +++ b/gotovienna/config.py @@ -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 index 0000000..00f8c23 --- /dev/null +++ b/gotovienna/update.py @@ -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 -- 1.7.9.5