Config validation, invisible interface fix

This commit is contained in:
Manuel Cortez 2015-03-30 10:55:56 -06:00
parent a513303a9a
commit d2f7228653
5 changed files with 27 additions and 58 deletions

View File

@ -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))

View File

@ -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")
def load_config(config_path, configspec_path=None, *args, **kwargs):
spec = ConfigObj(configspec_path, encoding='UTF8', list_values=False, _inspec=True)
try:
self.config = ConfigObj(infile=file, configspec=spec, create_empty=True, stringify=True, encoding="utf-8")
config = ConfigObj(infile=config_path, configspec=spec, create_empty=True, encoding='UTF8', *args, **kwargs)
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)
raise ConfigLoadError("Unable to load %r" % config_path)
validator = Validator()
validated = config.validate(validator, copy=True)
if validated == True:
config.write()
return config

View File

@ -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):
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):

View File

@ -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.")

View File

@ -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)