Преглед на файлове

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.
tags/v0.1^2
Ben Kurtovic преди 13 години
родител
ревизия
f4219ffad0
променени са 4 файла, в които са добавени 103 реда и са изтрити 13 реда
  1. +12
    -3
      wiki/tools/category.py
  2. +28
    -6
      wiki/tools/functions.py
  3. +17
    -2
      wiki/tools/page.py
  4. +46
    -2
      wiki/tools/site.py

+ 12
- 3
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]

+ 28
- 6
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)

+ 17
- 2
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

+ 46
- 2
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)

Зареждане…
Отказ
Запис