Revision: 5279 Author: nicdumz Date: 2008-04-27 09:37:06 +0000 (Sun, 27 Apr 2008)
Log Message: ----------- * Normalizing page titles (API is returning info on Namespace:Title and not on namespace:title) * Cleaning a bit Site Error raises : we cannot use standard RuntimeError or ValueError to raise exceptions, because unicode strings are needed for title rendering, and sadly http://bugs.python.org/issue2517 'Error when printing an exception containing a Unicode string'. More cleaning is needed. * implementing Site.page_restrictions, Site.page_can_be_edited
Modified Paths: -------------- branches/rewrite/pywikibot/exceptions.py branches/rewrite/pywikibot/login.py branches/rewrite/pywikibot/page.py branches/rewrite/pywikibot/site.py
Modified: branches/rewrite/pywikibot/exceptions.py =================================================================== --- branches/rewrite/pywikibot/exceptions.py 2008-04-26 19:39:00 UTC (rev 5278) +++ branches/rewrite/pywikibot/exceptions.py 2008-04-27 09:37:06 UTC (rev 5279) @@ -10,11 +10,17 @@ __version__ = '$Id: $'
+import sys + # TODO: These are copied from wikipedia.py; not certain that all of them # will be needed in the rewrite.
class Error(Exception): """Wikipedia error""" + def __init__(self, arg): + self.string = arg.encode(sys.stdout.encoding) + def __str__(self): + return self.string
class NoUsername(Error): """Username is not in user-config.py"""
Modified: branches/rewrite/pywikibot/login.py =================================================================== --- branches/rewrite/pywikibot/login.py 2008-04-26 19:39:00 UTC (rev 5278) +++ branches/rewrite/pywikibot/login.py 2008-04-27 09:37:06 UTC (rev 5279) @@ -72,7 +72,7 @@ try: self.username = config.sysopnames[self.site.family().name][self.site.language()] except: - raise NoUsername(u'ERROR: Sysop username for %s:%s is undefined.\nIf you have a sysop account for that site, please add such a line to user-config.py:\n\nsysopnames['%s']['%s'] = 'myUsername'' % (self.site.family.name, self.site.lang, self.site.family.name, self.site.lang)) + raise NoUsername(u'ERROR: Sysop username for %s:%s is undefined.\nIf you have a sysop account for that site, please add such a line to user-config.py:\n\nsysopnames['%s']['%s'] = 'myUsername'' % (self.site.family().name, self.site.language(), self.site.family().name, self.site.language())) else: try: self.username = config.usernames[self.site.family().name][self.site.language()]
Modified: branches/rewrite/pywikibot/page.py =================================================================== --- branches/rewrite/pywikibot/page.py 2008-04-26 19:39:00 UTC (rev 5278) +++ branches/rewrite/pywikibot/page.py 2008-04-27 09:37:06 UTC (rev 5279) @@ -75,12 +75,15 @@ "Invalid namespace '%i' for site %s." % (ns, source.sitename())) self._ns = ns + title = title[0].upper() + title[1:] if ns and not title.startswith(source.namespace(ns)+u":"): title = source.namespace(ns) + u":" + title elif not ns and u":" in title: - nsindex = source.getNamespaceIndex(title[ :title.index(u":")]) + pos = title.index(u':') + nsindex = source.getNamespaceIndex(title[ :pos]) if nsindex: self._ns = nsindex + title = title[:pos+1] + title[pos+1].upper() + title[pos+2:] if u"#" in title: title, self._section = title.split(u"#", 1) else: @@ -89,7 +92,7 @@ raise pywikibot.Error( "Page object cannot be created from Site without title.") self._title = title - elif isinstance(source, Page): + elif isinstance(source, Page): # copy all of source's attributes to this object self.__dict__ = source.__dict__ elif isinstance(source, Link):
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2008-04-26 19:39:00 UTC (rev 5278) +++ branches/rewrite/pywikibot/site.py 2008-04-27 09:37:06 UTC (rev 5279) @@ -13,6 +13,7 @@ import pywikibot from pywikibot.throttle import Throttle from pywikibot.data import api +from exceptions import * import config
import logging @@ -505,8 +506,8 @@ titles=title.encode(self.encoding())) for pageitem in query: if pageitem['title'] != title: - raise RuntimeError( - "getpageinfo: Query on %s returned data on '%s'" + raise Error( + u"getpageinfo: Query on %s returned data on '%s'" % (page, pageitem['title'])) api.update_page(page, pageitem)
@@ -515,7 +516,29 @@ if not hasattr(page, "_pageid"): self.getpageinfo(page) return page._pageid > 0 + + def page_restrictions(self, page): + """Returns a dictionary reflecting page protections""" + if not self.page_exists(page): + raise NoPage(u'No page %s.' % page) + # page_exists called getpageinfo which set protection levels + return page._protection
+ def page_can_be_edited(self, page): + """ + Returns True if and only if: + - page is unprotected, and bot has an account for this site, or + - page is protected, and bot has a sysop account for this site. + + """ + rest = self.page_restrictions(page) + sysop_protected = rest.has_key('edit') and rest['edit'][0] == 'sysop' + try: + api.LoginManager(site=self, sysop=sysop_protected) + except NoUsername: + return False + return True + def page_isredirect(self, page): """Return True if and only if page is a redirect.""" if not hasattr(page, "_redir"): @@ -672,7 +695,7 @@ """ if category.namespace() != 14: raise ValueError( - "Cannot get category members of non-Category page '%s'" + u"Cannot get category members of non-Category page '%s'" % category.title()) cmtitle = category.title(withSection=False).encode(self.encoding()) cmgen = api.PageGenerator(u"categorymembers", gcmtitle=cmtitle, @@ -790,8 +813,8 @@ for pagedata in rvgen: if page is not None: if pagedata['title'] != page.title(withSection=False): - raise RuntimeError( - "getrevisions: Query on %s returned data on '%s'" + raise Error( + u"getrevisions: Query on %s returned data on '%s'" % (page, pagedata['title'])) else: page = Page(self, pagedata['title']) @@ -823,8 +846,8 @@ ) for pageitem in llquery: if pageitem['title'] != lltitle: - raise RuntimeError( - "getlanglinks: Query on %s returned data on '%s'" + raise Error( + u"getlanglinks: Query on %s returned data on '%s'" % (page, pageitem['title'])) for linkdata in pageitem['langlinks']: yield pywikibot.Link(linkdata['*'], @@ -2036,7 +2059,7 @@ return catlib.Category(self, self.namespace(14)+':'+self.family().disambcatname[self.language()]) except KeyError: - raise NoPage + raise NoPage(u'No page %s.' % page)
def getToken(self, getalways = True, getagain = False, sysop = False): index = self._userIndex(sysop)