소스 검색

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.
tags/v0.1^2
Ben Kurtovic 13 년 전
부모
커밋
6aa2370900
3개의 변경된 파일30개의 추가작업 그리고 17개의 파일을 삭제
  1. +0
    -8
      wiki/tools/exceptions.py
  2. +29
    -8
      wiki/tools/functions.py
  3. +1
    -1
      wiki/tools/user.py

+ 0
- 8
wiki/tools/exceptions.py 파일 보기

@@ -9,17 +9,9 @@ This module contains all exceptions used by the wiki.tools package.
class WikiToolsetError(Exception): class WikiToolsetError(Exception):
"""Base exception class for errors in the Wiki Toolset.""" """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): class SiteNotFoundError(WikiToolsetError):
"""A site matching the args given to get_site() could not be found in the """A site matching the args given to get_site() could not be found in the
config file.""" config file."""


class UserNotFoundError(WikiToolsetError): class UserNotFoundError(WikiToolsetError):
"""Attempting to get information about a user that does not exist.""" """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)

+ 29
- 8
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. automatically available from wiki.tools.
""" """


from getpass import getpass

from core import config from core import config
from wiki.tools.exceptions import ConfigError, SiteNotFoundError
from wiki.tools.exceptions import SiteNotFoundError
from wiki.tools.site import Site from wiki.tools.site import Site


__all__ = ["get_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): def _get_site_object_from_dict(name, d):
"""Return a Site object based on the contents of a dict, probably acquired """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"] project = d["project"]
lang = d["lang"] lang = d["lang"]
try: 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 then `project` and `lang`. If, with any number of args, a site cannot be
found in the config, SiteNotFoundError is raised. 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): 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." e = "Keyword arguments 'lang' and 'project' must be specified together."
raise TypeError(e) 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 try: # ...so use the default site
default = config.wiki["defaultSite"] default = config.wiki["defaultSite"]
except KeyError: except KeyError:
@@ -75,7 +94,8 @@ def get_site(name=None, project=None, lang=None):
raise SiteNotFoundError(e) raise SiteNotFoundError(e)
return _get_site_object_from_dict(default, site) 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: try:
site = config.wiki["sites"][name] site = config.wiki["sites"][name]
except KeyError: except KeyError:
@@ -90,7 +110,8 @@ def get_site(name=None, project=None, lang=None):
else: else:
return _get_site_object_from_dict(name, site) 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: if site["project"] == project and site["lang"] == lang:
return _get_site_object_from_dict(sitename, site) return _get_site_object_from_dict(sitename, site)
e = "Site '{0}:{1}' not found in config.".format(project, lang) e = "Site '{0}:{1}' not found in config.".format(project, lang)


+ 1
- 1
wiki/tools/user.py 파일 보기

@@ -34,7 +34,7 @@ class User(object):
if self._exists is None or force: if self._exists is None or force:
self._load_attributes_from_api() self._load_attributes_from_api()
if self._exists is False: if self._exists is False:
raise UserNotFoundError(self.name)
raise UserNotFoundError("User '{0}' does not exist.".format(self.name))
return getattr(self, attr) return getattr(self, attr)


def _load_attributes_from_api(self): def _load_attributes_from_api(self):


불러오는 중...
취소
저장