From f4219ffad031592344e2a8508bf3daa8e17de268 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 24 Jul 2011 12:03:20 -0400 Subject: [PATCH] More work on the early stages of wikitools: tools.get_site() returns an actual Site object, thanks to tools.functions._get_site_object_from_dict(). Site objects now have a working (but primitive) .api_query(), .get_page(), .get_category(), and .get_user(). Page objects now have a working .get(), for getting page content from the API. Category is now a subclass of Page, and has its own .get_members(), which returns a list of titles. Still need to implement proper namespace logic in pages. --- wiki/tools/category.py | 15 ++++++++++++--- wiki/tools/functions.py | 34 ++++++++++++++++++++++++++++------ wiki/tools/page.py | 19 +++++++++++++++++-- wiki/tools/site.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 103 insertions(+), 13 deletions(-) diff --git a/wiki/tools/category.py b/wiki/tools/category.py index 3df8477..1dfdba3 100644 --- a/wiki/tools/category.py +++ b/wiki/tools/category.py @@ -1,9 +1,18 @@ # -*- coding: utf-8 -*- -class Category(object): +from wiki.tools.page import Page + +class Category(Page): """ EarwigBot's Wiki Toolset: Category Class """ - def __init__(self): - pass + def get_members(limit=50): + """ + Docstring needed + """ + params = {"action": "query", "list": "categorymembers", "cmlimit": limit} + params["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/functions.py b/wiki/tools/functions.py index 220e4f3..195400c 100644 --- a/wiki/tools/functions.py +++ b/wiki/tools/functions.py @@ -16,6 +16,25 @@ from wiki.tools.site import Site __all__ = ["get_site"] +def _get_site_object_from_dict(name, d): + """Return a Site object based on the contents of a dict, probably acquired + through our config file, and a separate name.""" + project = d["project"] + lang = d["lang"] + try: + api = d["apiURL"] + except KeyError: + api = None + try: + sql_server = d["sqlServer"] + except KeyError: + sql_server = None + try: + sql_db = d["sqlDB"] + except KeyError: + sql_db = None + return Site(name, project, lang, api, (sql_server, sql_db)) + def get_site(name=None, project=None, lang=None): """Returns a Site instance based on information from our config file. @@ -50,26 +69,29 @@ def get_site(name=None, project=None, lang=None): e = "Default site is not specified in config." raise SiteNotFoundError(e) try: - return config.wiki["sites"][default] + site = config.wiki["sites"][default] except KeyError: e = "Default site specified by config is not in the config's sites list." raise SiteNotFoundError(e) + return _get_site_object_from_dict(default, site) if name is not None: # name arg given, but don't look at others yet try: - return config.wiki["sites"][name] + site = config.wiki["sites"][name] except KeyError: if project is None: # implies lang is None, i.e., only name was given e = "Site '{0}' not found in config.".format(name) raise SiteNotFoundError(e) - for site in config.wiki["sites"].values(): + for sitename, site in config.wiki["sites"].items(): if site["project"] == project and site["lang"] == lang: - return site + return _get_site_object_from_dict(sitename, site) e = "Neither site '{0}' nor site '{1}:{2}' found in config.".format(name, project, lang) raise SiteNotFoundError(e) + else: + return _get_site_object_from_dict(name, site) - for site in config.wiki["sites"].values(): # implied lang and proj are not None + for sitename, site in config.wiki["sites"].items(): # implied lang and proj are not None if site["project"] == project and site["lang"] == lang: - return site + return _get_site_object_from_dict(sitename, site) e = "Site '{0}:{1}' not found in config.".format(project, lang) raise SiteNotFoundError(e) diff --git a/wiki/tools/page.py b/wiki/tools/page.py index 3a30a70..49a89ee 100644 --- a/wiki/tools/page.py +++ b/wiki/tools/page.py @@ -5,5 +5,20 @@ class Page(object): EarwigBot's Wiki Toolset: Page Class """ - def __init__(self): - pass + def __init__(self, site, title): + """ + Docstring needed + """ + self.site = site + self.title = title + + def get(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 diff --git a/wiki/tools/site.py b/wiki/tools/site.py index caba1e4..34df709 100644 --- a/wiki/tools/site.py +++ b/wiki/tools/site.py @@ -1,9 +1,53 @@ # -*- coding: utf-8 -*- +from json import loads +from urllib import urlencode +from urllib2 import urlopen + +from wiki.tools.category import Category +from wiki.tools.page import Page +from wiki.tools.user import User + class Site(object): """ EarwigBot's Wiki Toolset: Site Class """ - def __init__(self): - pass + def __init__(self, name, project, lang, api=None, sql=(None, None)): + """ + Docstring needed + """ + self.name = name + self.project = project + self.lang = lang + self.__api = api + self.__sql = sql + + def api_query(self, params): + """ + Docstring needed + """ + params["format"] = "json" + data = urlencode(params) + result = urlopen(self.__api, data).read() + return loads(result) + + def get_page(self, pagename): + """ + Docstring needed + """ + if pagename.startswith("Category:"): # proper namespace checking! + return get_category(pagename[9:]) + return Page(self, pagename) + + def get_category(self, catname): + """ + Docstring needed + """ + return Category(self, "Category:" + catname) # namespace checking! + + def get_user(self, username): + """ + Docstring needed + """ + return User(self, username)