Revision: 6461 Author: nicdumz Date: 2009-03-02 07:00:46 +0000 (Mon, 02 Mar 2009)
Log Message: ----------- Introducing a PageRelatedError abstract class, to use class level messages to have simpler Error creations, and unified Error message. For now only NoPage, IsRedirectPage, and IsNotRedirectPage are subclasses of that abstract Error: others could too, I'm just converting the Errors that we will keep _for sure_
Modified Paths: -------------- branches/rewrite/pywikibot/exceptions.py branches/rewrite/pywikibot/page.py branches/rewrite/pywikibot/site.py
Modified: branches/rewrite/pywikibot/exceptions.py =================================================================== --- branches/rewrite/pywikibot/exceptions.py 2009-03-01 09:19:24 UTC (rev 6460) +++ branches/rewrite/pywikibot/exceptions.py 2009-03-02 07:00:46 UTC (rev 6461) @@ -25,24 +25,47 @@ def __str__(self): return self.string
+class PageRelatedError(Error): + """Abstract Exception, used when the Exception concerns a particular + Page, and when a generic message can be written once for all""" + # Preformated UNICODE message where the page title will be inserted + # Override this in subclasses. + # u"Oh noes! Page %s is too funky, we should not delete it ;(" + message = None + def __init__(self, page): + """ + @param page + @type page: Page object + """ + if self.message is None: + raise Error("PageRelatedError is abstract. Can't instantiate it!") + super(PageRelatedError, self).__init__(self.message % page) + self._page = page + + def getPage(self): + return self._page + class NoUsername(Error): """Username is not in user-config.py"""
-class NoPage(Error): +class NoPage(PageRelatedError): """Page does not exist""" + message = u"Page %s doesn't exist."
class NoSuchSite(Error): """Site does not exist"""
-class IsRedirectPage(Error): +class IsRedirectPage(PageRelatedError): """Page is a redirect page""" + message = u"Page %s is a redirect page."
-class IsNotRedirectPage(Error): +class IsNotRedirectPage(PageRelatedError): """Page is not a redirect page""" + message = u"Page %s is not a redirect page."
class CircularRedirect(Error): """Page is a circular redirect - + Exception argument is the redirect target; this may be the same title as this page or a different title (in which case the target page directly or indirectly redirects back to this one)
Modified: branches/rewrite/pywikibot/page.py =================================================================== --- branches/rewrite/pywikibot/page.py 2009-03-01 09:19:24 UTC (rev 6460) +++ branches/rewrite/pywikibot/page.py 2009-03-02 07:00:46 UTC (rev 6461) @@ -283,8 +283,7 @@ exceptions that should be caught by the calling code:
- NoPage: The page does not exist - - IsRedirectPage: The page is a redirect. The argument of the - exception is the title of the page it redirects to. + - IsRedirectPage: The page is a redirect. - SectionError: The section does not exist on a page with a # link
@@ -304,7 +303,7 @@ else: # Make sure we re-raise an exception we got on an earlier attempt if hasattr(self, '_redirarg') and not get_redirect: - raise pywikibot.IsRedirectPage, self._redirarg + raise pywikibot.IsRedirectPage(self) elif hasattr(self, '_getexception'): raise self._getexception if force or not hasattr(self, "_revid") \
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2009-03-01 09:19:24 UTC (rev 6460) +++ branches/rewrite/pywikibot/site.py 2009-03-02 07:00:46 UTC (rev 6461) @@ -939,7 +939,7 @@ def page_restrictions(self, page): """Returns a dictionary reflecting page protections""" if not self.page_exists(page): - raise NoPage(u'No page %s.' % page) + raise NoPage(page) if not hasattr(page, "_protection"): self.loadpageinfo(page) return page._protection @@ -970,7 +970,7 @@ if not hasattr(page, "_redir"): self.loadpageinfo(page) if not page._redir: - raise pywikibot.IsNotRedirectPage(page.title()) + raise pywikibot.IsNotRedirectPage(page) title = page.title(withSection=False) query = api.Request(site=self, action="query", property="info", inprop="protection|talkid|subjectid", @@ -1348,8 +1348,7 @@ u"loadrevisions: Query on %s returned data on '%s'" % (page, pagedata['title'])) if "missing" in pagedata: - raise NoPage(u'Page %s does not exist' - % page.title(asLink=True)) + raise NoPage(page) else: page = Page(self, pagedata['title']) api.update_page(page, pagedata) @@ -2141,8 +2140,7 @@ except NoPage: lastrev = None if not recreate: - raise Error("Page %s does not exist on %s wiki." - % (page.title(withSection=False), self)) + raise token = self.token(page, "edit") self.lock_page(page) if lastrev is not None and page.latestRevision() != lastrev:
And yes, the new IsRedirectPage does not provide anymore in the Error message "the redirect target is ... ".
There are probably cons in doing this. But as I argued in the next commit, I dont't think that we always need to know what the redirect target is; and since that if we want to retrieve cleanly the redirect target (no wikitext parsing !!), an additional API query is needed, I'm saying: "users will call Page.getRedirectTarget if they need to know the redirect target ".
=)
2009/3/2 nicdumz@svn.wikimedia.org:
Revision: 6461 Author: nicdumz Date: 2009-03-02 07:00:46 +0000 (Mon, 02 Mar 2009)
Log Message:
Introducing a PageRelatedError abstract class, to use class level messages to have simpler Error creations, and unified Error message. For now only NoPage, IsRedirectPage, and IsNotRedirectPage are subclasses of that abstract Error: others could too, I'm just converting the Errors that we will keep _for sure_