Parcourir la source

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
tags/v0.1^2
Ben Kurtovic il y a 13 ans
Parent
révision
76113a3f6a
4 fichiers modifiés avec 46 ajouts et 14 suppressions
  1. +3
    -3
      wiki/tools/category.py
  2. +16
    -7
      wiki/tools/page.py
  3. +3
    -3
      wiki/tools/site.py
  4. +24
    -1
      wiki/tools/user.py

+ 3
- 3
wiki/tools/category.py Voir le fichier

@@ -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]

+ 16
- 7
wiki/tools/page.py Voir le fichier

@@ -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

+ 3
- 3
wiki/tools/site.py Voir le fichier

@@ -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):


+ 24
- 1
wiki/tools/user.py Voir le fichier

@@ -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

Chargement…
Annuler
Enregistrer