浏览代码

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)

tags/v0.1^2
Ben Kurtovic 13 年前
父节点
当前提交
b290582dbf
共有 3 个文件被更改,包括 131 次插入13 次删除
  1. +1
    -1
      irc/commands/rights.py
  2. +7
    -0
      wiki/tools/exceptions.py
  3. +123
    -12
      wiki/tools/user.py

+ 1
- 1
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


+ 7
- 0
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)

+ 123
- 12
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!

正在加载...
取消
保存