From b290582dbf5a8e420f88cf9660fa45937fdd3b8c Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 24 Jul 2011 22:34:54 -0400 Subject: [PATCH] added a bunch of new methods to User in wikitools; added one user-related exception to wikitools; moved .get_rights() call in IRC command !rights to .get_groups(), because get_rights() now returns actual rights (thanks to the API) --- irc/commands/rights.py | 2 +- wiki/tools/exceptions.py | 7 +++ wiki/tools/user.py | 135 ++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 131 insertions(+), 13 deletions(-) diff --git a/irc/commands/rights.py b/irc/commands/rights.py index 2715d60..6c44227 100644 --- a/irc/commands/rights.py +++ b/irc/commands/rights.py @@ -27,7 +27,7 @@ class Rights(BaseCommand): username = ' '.join(data.args) site = tools.get_site() user = site.get_user(username) - rights = user.get_rights() + rights = user.get_groups() if rights: try: rights.remove("*") # remove the implicit '*' group given to everyone diff --git a/wiki/tools/exceptions.py b/wiki/tools/exceptions.py index b515c45..3dc463d 100644 --- a/wiki/tools/exceptions.py +++ b/wiki/tools/exceptions.py @@ -16,3 +16,10 @@ class ConfigError(WikiToolsetError): 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/user.py b/wiki/tools/user.py index 4fb69b7..c12e234 100644 --- a/wiki/tools/user.py +++ b/wiki/tools/user.py @@ -1,32 +1,143 @@ # -*- coding: utf-8 -*- +from wiki.tools.exceptions import UserNotFoundError +from wiki.tools.page import Page + class User(object): """ EarwigBot's Wiki Toolset: User Class """ - def __init__(self, site, username): + def __init__(self, site, name): """ Docstring needed """ - self.site = site - self.username = username + # Public attributes + self.site = site # Site instance, for doing API queries, etc + self.name = name # our username + + # Attributes filled in by an API query + self._exists = None + self._userid = None + self._blockinfo = None + self._groups = None + self._rights = None + self._editcount = None + self._registration = None + self._emailable = None + self._gender = None - def exists(self): + def _get_attribute_from_api(self, attr, force): """ Docstring needed """ - pass + if self._exists is None or force: + self._load_attributes_from_api() + if self._exists is False: + raise UserNotFoundError(self.name) + return getattr(self, attr) - def get_rights(self): + def _load_attributes_from_api(self): """ Docstring needed """ - params = {"action": "query", "list": "users", "usprop": "groups", - "ususers": self.username} + params = {"action": "query", "list": "users", "ususers": self.name, + "usprop": "blockinfo|groups|rights|editcount|registration|emailable|gender"} result = self.site.api_query(params) + + # normalize our username in case it was entered oddly + self.name = result["query"]["users"][0]["name"] + + try: + self._userid = result["query"]["users"][0]["userid"] + except KeyError: # userid is missing, so user does not exist + self._exists = False + return + + self._exists = True + res = result['query']['users'][0] + + self._groups = res["groups"] + self._rights = res["rights"] + self._editcount = res["editcount"] + self._registration = res["registration"] + self._gender = res["gender"] + + try: + res["emailable"] + except KeyError: + self._emailable = False + else: + self._emailable = True + try: - rights = res['query']['users'][0]['groups'] - except KeyError: # 'groups' not found, meaning the user does not exist - return None - return rights + self._blockinfo = {"by": res["blockedby"], + "reason": res["blockreason"], "expiry": res["blockexpiry"]} + except KeyError: + self._blockinfo = False + + def exists(self, force=False): + """ + Docstring needed + """ + return self._get_attribute_from_api("_exists", force) + + def get_userid(self, force=False): + """ + Docstring needed + """ + return self._get_attribute_from_api("_userid", force) + + def get_blockinfo(self, force=False): + """ + Docstring needed + """ + return self._get_attribute_from_api("_blockinfo", force) + + def get_groups(self, force=False): + """ + Docstring needed + """ + return self._get_attribute_from_api("_groups", force) + + def get_rights(self, force=False): + """ + Docstring needed + """ + return self._get_attribute_from_api("_rights", force) + + def get_editcount(self, force=False): + """ + Docstring needed + """ + return self._get_attribute_from_api("_editcount", force) + + def get_registration(self, force=False): + """ + Docstring needed + """ + return self._get_attribute_from_api("_registration", force) + + def get_emailable(self, force=False): + """ + Docstring needed + """ + return self._get_attribute_from_api("_emailable", force) + + def get_gender(self, force=False): + """ + Docstring needed + """ + return self._get_attribute_from_api("_gender", force) + + def get_userpage(self): + """ + Docstring needed + """ + return Page(self.site, "User:" + self.name) # Namespace checking! + + def get_talkpage(self): + """ + Docstring needed + """ + return Page(self.site, "User talk:" + self.name) # Namespace checking!