From 8fe1e1b39d9c4581b8c1a0911ea49c28fd701fb1 Mon Sep 17 00:00:00 2001 From: Peter Dahlberg Date: Thu, 5 Feb 2015 18:38:28 +0100 Subject: [PATCH] Fix handling of persistant storage if no persistant storage exists yet --- common.py | 12 ++++++++---- plugins.py | 7 ++++--- urlbot.py | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/common.py b/common.py index 16a1e34..8d5a055 100644 --- a/common.py +++ b/common.py @@ -5,7 +5,7 @@ if '__main__' == __name__: print('''this is a library file, which is not meant to be executed''') exit(-1) -import sys, time, pickle +import sys, time, pickle, os from local_config import conf RATE_GLOBAL = 0x01 @@ -36,9 +36,13 @@ def conf_save(obj): return pickle.dump(obj, fd) def conf_load(): - with open(conf('persistent_storage'), 'rb') as fd: - fd.seek(0) - return pickle.load(fd) + path = conf('persistent_storage') + if os.path.isfile(path): + with open(path, 'rb') as fd: + fd.seek(0) + return pickle.load(fd) + else: + return {} def get_version_git(): import subprocess diff --git a/plugins.py b/plugins.py index 7483895..68a01ce 100644 --- a/plugins.py +++ b/plugins.py @@ -440,13 +440,14 @@ def command_usersetting(argv, **args): set_conf('persistent_locked', True) blob = conf_load() + + if 'user_pref' not in blob: + blob['user_pref'] = {} if not arg_user in blob['user_pref']: blob['user_pref'][arg_user] = {} - blob['user_pref'][arg_user][arg_key] = ( - True if 'on' == arg_val else False - ) + blob['user_pref'][arg_user][arg_key] = 'on' == arg_val conf_save(blob) set_conf('persistent_locked', False) diff --git a/urlbot.py b/urlbot.py index 21b7f7d..f3eeb00 100755 --- a/urlbot.py +++ b/urlbot.py @@ -229,7 +229,7 @@ def handle_msg(msg_obj): return arg_user = msg_obj['mucnick'] - blob_userpref = conf_load()['user_pref'] + blob_userpref = conf_load().get('user_pref',[]) nospoiler = False if arg_user in blob_userpref: