From 6aa2370900785c606464fac3da3aa600b2ef588e Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Wed, 27 Jul 2011 02:22:18 -0400 Subject: [PATCH] Exception and function cleanup in wikitools: * Got rid of ConfigError from exceptions.py. * Try to load config ourselves if it isn't already, via the new _load_config() method of Site. It uses getpass if passwords are encrypted, as done by earwigbot.py. * Cleaned up UserNotFoundError in user.py and exceptions.py. --- wiki/tools/exceptions.py | 8 -------- wiki/tools/functions.py | 37 +++++++++++++++++++++++++++++-------- wiki/tools/user.py | 2 +- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/wiki/tools/exceptions.py b/wiki/tools/exceptions.py index 3dc463d..d628d0d 100644 --- a/wiki/tools/exceptions.py +++ b/wiki/tools/exceptions.py @@ -9,17 +9,9 @@ This module contains all exceptions used by the wiki.tools package. class WikiToolsetError(Exception): """Base exception class for errors in the Wiki Toolset.""" -class ConfigError(WikiToolsetError): - """An error occured when trying to do something involving our config - file. Maybe it hasn't been loaded?""" - class SiteNotFoundError(WikiToolsetError): """A site matching the args given to get_site() could not be found in the config file.""" class UserNotFoundError(WikiToolsetError): """Attempting to get information about a user that does not exist.""" - def __init__(self, name): - self.name = name - def __str__(self): - return "User '{0}' does not exist.".format(self.name) diff --git a/wiki/tools/functions.py b/wiki/tools/functions.py index 195400c..d1cc020 100644 --- a/wiki/tools/functions.py +++ b/wiki/tools/functions.py @@ -10,15 +10,31 @@ There's no need to import this module explicitly. All functions here are automatically available from wiki.tools. """ +from getpass import getpass + from core import config -from wiki.tools.exceptions import ConfigError, SiteNotFoundError +from wiki.tools.exceptions import SiteNotFoundError from wiki.tools.site import Site __all__ = ["get_site"] +def _load_config(): + """Called by a config-requiring function, such as get_site(), when config + has not been loaded. This will usually happen only if we're running code + directly from Python's interpreter and not the bot itself, because + earwigbot.py or core/main.py will already call these functions. + """ + is_encrypted = config.verify_config() + if is_encrypted: # passwords in the config file are encrypted + key = getpass("Enter key to unencrypt bot passwords: ") + config.parse_config(key) + else: + config.parse_config(None) + def _get_site_object_from_dict(name, d): """Return a Site object based on the contents of a dict, probably acquired - through our config file, and a separate name.""" + through our config file, and a separate name. + """ project = d["project"] lang = d["lang"] try: @@ -54,15 +70,18 @@ def get_site(name=None, project=None, lang=None): then `project` and `lang`. If, with any number of args, a site cannot be found in the config, SiteNotFoundError is raised. """ - if config._config is None: - e = "Config file has not been loaded: use config.verify_config() and then config.parse_config() to do so." - raise ConfigError(e) + # check if config has been loaded, and load it if it hasn't + if not config.is_config_loaded(): + _load_config() + # someone specified a project without a lang (or a lang without a project)! if (project is None and lang is not None) or (project is not None and lang is None): e = "Keyword arguments 'lang' and 'project' must be specified together." raise TypeError(e) - if name is None and project is None: # no args given (project is None implies lang is None) + # no args given, so return our default site (project is None implies lang + # is None, so we don't need to add that in) + if name is None and project is None: try: # ...so use the default site default = config.wiki["defaultSite"] except KeyError: @@ -75,7 +94,8 @@ def get_site(name=None, project=None, lang=None): raise SiteNotFoundError(e) return _get_site_object_from_dict(default, site) - if name is not None: # name arg given, but don't look at others yet + # name arg given, but don't look at others unless `name` isn't found + if name is not None: try: site = config.wiki["sites"][name] except KeyError: @@ -90,7 +110,8 @@ def get_site(name=None, project=None, lang=None): else: return _get_site_object_from_dict(name, site) - for sitename, site in config.wiki["sites"].items(): # implied lang and proj are not None + # if we end up here, then project and lang are both not None + for sitename, site in config.wiki["sites"].items(): if site["project"] == project and site["lang"] == lang: return _get_site_object_from_dict(sitename, site) e = "Site '{0}:{1}' not found in config.".format(project, lang) diff --git a/wiki/tools/user.py b/wiki/tools/user.py index c12e234..ae19bfe 100644 --- a/wiki/tools/user.py +++ b/wiki/tools/user.py @@ -34,7 +34,7 @@ class User(object): if self._exists is None or force: self._load_attributes_from_api() if self._exists is False: - raise UserNotFoundError(self.name) + raise UserNotFoundError("User '{0}' does not exist.".format(self.name)) return getattr(self, attr) def _load_attributes_from_api(self):