def saveSettings(self, promptOnFail=False): try: if not os.path.exists(mh.getPath('')): os.makedirs(mh.getPath('')) with outFile("settings.ini") as f: f.write(mh.formatINI(self.settings)) with outFile("shortcuts.ini") as f: for action, shortcut in self.shortcuts.iteritems(): f.write('%d %d %s\n' % (shortcut[0], shortcut[1], action)) with outFile("mouse.ini") as f: for mouseAction, method in self.mouseActions.iteritems(): f.write('%d %d %s\n' % (mouseAction[0], mouseAction[1], method.__name__)) if self.dialog is not None: self.helpIds.update(self.dialog.helpIds) with outFile("help.ini") as f: for helpId in self.helpIds: f.write('%s\n' % helpId) except: log.error('Failed to save settings file', exc_info=True) if promptOnFail: self.prompt('Error', 'Could not save settings file.', 'OK')
def download(self, url): filename = os.path.basename(url) if os.path.exists(os.path.join(self.path, filename)): etag, modified = self.cache.get(filename, (None, None)) else: etag, modified = None, None try: downloaded, etag, modified, data = self.__downloadConditionally(url, etag, modified) except urllib.error.HTTPError as e: log.notice('Could not download %s: %s', url, e) return False, e.code if downloaded: with open(os.path.join(self.path, filename), 'wb') as f: f.write(data) self.cache[filename] = (etag, modified) cachePath = os.path.join(self.path, 'cache.ini') with open(cachePath, 'w') as f: f.write(mh.formatINI(self.cache)) return True, (200 if downloaded else 304)
class DownloadCache(): class NotModifiedHandler(urllib2.BaseHandler): def http_error_304(self, req, fp, code, message, headers): addinfourl = urllib2.addinfourl(fp, headers, req.get_full_url()) addinfourl.code = code return addinfourl def __init__(self, path): self.path = path cachePath = os.path.join(self.path, 'cache.ini') self.cache = {} if os.path.exists(cachePath): try: with open(cachePath, 'r') as f: self.cache = mh.parseINI(f.read()) except ValueError: os.remove(cachePath) def download(self, url): filename = os.path.basename(url) if os.path.exists(os.path.join(self.path, filename)): etag, modified = self.cache.get(filename, (None, None)) else: etag, modified = None, None try: downloaded, etag, modified, data = self.__downloadConditionally( url, etag, modified) except urllib2.HTTPError, e: log.notice('Could not download %s: %s', url, e) return False, e.code if downloaded: with open(os.path.join(self.path, filename), 'wb') as f: f.write(data) self.cache[filename] = (etag, modified) cachePath = os.path.join(self.path, 'cache.ini') with open(cachePath, 'w') as f: f.write(mh.formatINI(self.cache)) return True, (200 if downloaded else 304)