From d2f7228653eeb4b5ced20b34f6ed147a0f66740f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Cort=C3=A9z?= Date: Mon, 30 Mar 2015 10:55:56 -0600 Subject: [PATCH] Config validation, invisible interface fix --- src/config.py | 7 +--- src/config_utils.py | 58 +++++++--------------------- src/controller/mainController.py | 12 ++++-- src/sessionmanager/session.py | 4 +- src/sessionmanager/sessionManager.py | 4 +- 5 files changed, 27 insertions(+), 58 deletions(-) diff --git a/src/config.py b/src/config.py index f945b816..9742510c 100644 --- a/src/config.py +++ b/src/config.py @@ -1,5 +1,5 @@ # -*- coding: cp1252 -*- -from config_utils import Configuration, ConfigurationResetException +import config_utils import paths import logging @@ -13,7 +13,4 @@ app = None def setup (): global app log.debug("Loading global app settings...") - try: - app = Configuration(paths.config_path(MAINFILE), paths.app_path(MAINSPEC)) - except ConfigurationResetException: - pass + app = config_utils.load_config(paths.config_path(MAINFILE), paths.app_path(MAINSPEC)) diff --git a/src/config_utils.py b/src/config_utils.py index 7e636148..165d82c0 100644 --- a/src/config_utils.py +++ b/src/config_utils.py @@ -1,50 +1,18 @@ # -*- coding: utf-8 -*- -from UserDict import UserDict from configobj import ConfigObj, ParseError -from validate import Validator, VdtValueError +from validate import Validator, ValidateError import os -"""We're using the configobj python package -from http://www.voidspace.org.uk/python/configobj.html """ +class ConfigLoadError(Exception): pass -class ConfigurationResetException(Exception): - pass - - -class Configuration (UserDict): - - def __init__ (self, file=None, spec=None, *args, **kwargs): - self.file = file - self.spec = spec - self.validator = Validator() - self.setup_config(file=file, spec=spec) - self.validated = self.config.validate(self.validator, copy=True) - if self.validated: - self.write() - UserDict.__init__(self, self.config) - - def setup_config (self, file, spec): - #The default way -- load from a file - spec = ConfigObj(spec, list_values=False, encoding="utf-8") - try: - self.config = ConfigObj(infile=file, configspec=spec, create_empty=True, stringify=True, encoding="utf-8") - except ParseError: - os.remove(file) - self.config = ConfigObj(infile=file, configspec=spec, create_empty=True, stringify=True) - raise ConfigurationResetException - def __getitem__ (self, *args, **kwargs): - return dict(self.config).__getitem__(*args, **kwargs) - - def __setitem__ (self, *args, **kwargs): - self.config.__setitem__(*args, **kwargs) - UserDict.__setitem__(self, *args, **kwargs) - - def write (self): - if hasattr(self.config, 'write'): - self.config.write() - -class SessionConfiguration (Configuration): - def setup_config (self, file, spec): - #No infile required. - spec = ConfigObj(spec, list_values=False) - self.config = ConfigObj(configspec=spec, stringify=True) +def load_config(config_path, configspec_path=None, *args, **kwargs): + spec = ConfigObj(configspec_path, encoding='UTF8', list_values=False, _inspec=True) + try: + config = ConfigObj(infile=config_path, configspec=spec, create_empty=True, encoding='UTF8', *args, **kwargs) + except ParseError: + raise ConfigLoadError("Unable to load %r" % config_path) + validator = Validator() + validated = config.validate(validator, copy=True) + if validated == True: + config.write() + return config diff --git a/src/controller/mainController.py b/src/controller/mainController.py index cf6dba63..f3e426e2 100644 --- a/src/controller/mainController.py +++ b/src/controller/mainController.py @@ -69,7 +69,7 @@ class Controller(object): buffer = self.search_buffer("home_timeline", view_buffer.account) else: buffer = self.search_buffer(view_buffer.name, view_buffer.account) - return buffer + if buffer != None: return buffer def get_first_buffer(self, account): """ Gets the first valid buffer for an account. @@ -785,9 +785,13 @@ class Controller(object): self.view.check_menuitem("autoread", autoread) def fix_wrong_buffer(self): - for i in self.accounts: - buffer = self.view.search("home_timeline", i) - if buffer != None: break + buf = self.get_best_buffer() + if buf == None: + for i in self.accounts: + buffer = self.view.search("home_timeline", i) + if buffer != None: break + else: + buffer = self.view.search("home_timeline", buf.session.db["user_name"]) self.view.change_buffer(buffer) def up(self, *args, **kwargs): diff --git a/src/sessionmanager/session.py b/src/sessionmanager/session.py index 051d1df9..7cd6a145 100644 --- a/src/sessionmanager/session.py +++ b/src/sessionmanager/session.py @@ -11,7 +11,7 @@ import sound import logging from twitter import utils from twython import TwythonError, TwythonRateLimitError, TwythonAuthError -from config_utils import Configuration, ConfigurationResetException +import config_utils from mysc.thread_utils import stream_threaded from pubsub import pub log = logging.getLogger("sessionmanager.session") @@ -106,7 +106,7 @@ class Session(object): file_ = "%s/session.conf" % (self.session_id,) # try: log.debug("Creating config file %s" % (file_,)) - self.settings = Configuration(paths.config_path(file_), paths.app_path("Conf.defaults")) + self.settings = config_utils.load_config(paths.config_path(file_), paths.app_path("Conf.defaults")) self.init_sound() # except: # log.exception("The session configuration has failed.") diff --git a/src/sessionmanager/sessionManager.py b/src/sessionmanager/sessionManager.py index 6b11b36c..8a8e1419 100644 --- a/src/sessionmanager/sessionManager.py +++ b/src/sessionmanager/sessionManager.py @@ -8,7 +8,7 @@ import os import logging import session import manager -from config_utils import Configuration +import config_utils import config log = logging.getLogger("sessionmanager.sessionManager") @@ -32,7 +32,7 @@ class sessionManagerController(object): if os.path.isdir(paths.config_path(i)): log.debug("Adding session %s" % (i,)) strconfig = "%s/session.conf" % (paths.config_path(i)) - config_test = Configuration(strconfig) + config_test = config_utils.load_config(strconfig) name = config_test["twitter"]["user_name"] if config_test["twitter"]["user_key"] != "" and config_test["twitter"]["user_secret"] != "": sessionsList.append(name)