From 76113a3f6ac2f649607757a2b14bcc5846c25da2 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 24 Jul 2011 18:01:06 -0400 Subject: [PATCH] Cleaning up slightly and adding some more methods: User: added .get_rights() (working) and .exists() (skeleton). Page: added .exists() (skeleton); store text as ._content; get() has a force_reload argument. Category: fixed missing self in .get_members(). Site: self.__api -> self._api; self.__sql -> self._sql --- wiki/tools/category.py | 6 +++--- wiki/tools/page.py | 23 ++++++++++++++++------- wiki/tools/site.py | 6 +++--- wiki/tools/user.py | 25 ++++++++++++++++++++++++- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/wiki/tools/category.py b/wiki/tools/category.py index 1dfdba3..01a3179 100644 --- a/wiki/tools/category.py +++ b/wiki/tools/category.py @@ -7,12 +7,12 @@ class Category(Page): EarwigBot's Wiki Toolset: Category Class """ - def get_members(limit=50): + def get_members(self, limit=50): """ Docstring needed """ - params = {"action": "query", "list": "categorymembers", "cmlimit": limit} - params["cmtitle"] = self.title + params = {"action": "query", "list": "categorymembers", + "cmlimit": limit, "cmtitle": self.title} result = self.site.api_query(params) members = result['query']['categorymembers'] return [member["title"] for member in members] diff --git a/wiki/tools/page.py b/wiki/tools/page.py index 49a89ee..9dbb7ab 100644 --- a/wiki/tools/page.py +++ b/wiki/tools/page.py @@ -11,14 +11,23 @@ class Page(object): """ self.site = site self.title = title + self._content = None - def get(self): + def exists(self): """ Docstring needed """ - params = {'action': 'query', 'prop': 'revisions', 'rvprop':'content', 'rvlimit':'1'} - params["titles"] = self.title - result = self.site.api_query(params) - pageid = result['query']['pages'].keys()[0] - content = result['query']['pages'][pageid]['revisions'][0]['*'] - return content + pass + + def get(self, force_reload=False): + """ + Docstring needed + """ + if content is None or force_reload: + params = {"action": "query", "prop": "revisions", + "rvprop": "content", "rvlimit": 1, "titles": self.title} + result = self.site.api_query(params) + content = result["query"]["pages"].values()[0]["revisions"][0]["*"] + self._content = content + return content + return self._content diff --git a/wiki/tools/site.py b/wiki/tools/site.py index 34df709..ea62c77 100644 --- a/wiki/tools/site.py +++ b/wiki/tools/site.py @@ -20,8 +20,8 @@ class Site(object): self.name = name self.project = project self.lang = lang - self.__api = api - self.__sql = sql + self._api = api + self._sql = sql def api_query(self, params): """ @@ -29,7 +29,7 @@ class Site(object): """ params["format"] = "json" data = urlencode(params) - result = urlopen(self.__api, data).read() + result = urlopen(self._api, data).read() return loads(result) def get_page(self, pagename): diff --git a/wiki/tools/user.py b/wiki/tools/user.py index 5044e50..4fb69b7 100644 --- a/wiki/tools/user.py +++ b/wiki/tools/user.py @@ -5,5 +5,28 @@ class User(object): EarwigBot's Wiki Toolset: User Class """ - def __init__(self): + def __init__(self, site, username): + """ + Docstring needed + """ + self.site = site + self.username = username + + def exists(self): + """ + Docstring needed + """ pass + + def get_rights(self): + """ + Docstring needed + """ + params = {"action": "query", "list": "users", "usprop": "groups", + "ususers": self.username} + result = self.site.api_query(params) + try: + rights = res['query']['users'][0]['groups'] + except KeyError: # 'groups' not found, meaning the user does not exist + return None + return rights