Revision: 5416 Author: nicdumz Date: 2008-05-20 21:34:18 +0000 (Tue, 20 May 2008)
Log Message: ----------- * Fixing #1967922 "cosmetic_change fails to get title ?"
* Introducing InvalidTitle to be used instead of Error. The problem, if we raise wikipedia.Error, and catch wikipedia.Error, is that we don't really know what we are catching : Error is the baseclass for all our wikipedia defined exceptions, and "except Error:" catches PageNotSaved, NoPage, or whatever other exceptions. It's poor, we don't want to do this :)
Modified Paths: -------------- trunk/pywikipedia/cosmetic_changes.py trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/cosmetic_changes.py =================================================================== --- trunk/pywikipedia/cosmetic_changes.py 2008-05-20 20:22:19 UTC (rev 5415) +++ trunk/pywikipedia/cosmetic_changes.py 2008-05-20 21:34:18 UTC (rev 5416) @@ -181,7 +181,10 @@ # [[page_title|link_text]]trailing_chars # We only work on namespace 0 because pipes and linktrails work # differently for images and categories. - page = wikipedia.Page(self.site, titleWithSection) + try: + page = wikipedia.Page(self.site, titleWithSection) + except wikipedia.InvalidTitle: + return match.group() if page.namespace() == 0: # Replace underlines by spaces, also multiple underlines titleWithSection = re.sub('_+', ' ', titleWithSection)
Modified: trunk/pywikipedia/wikipedia.py =================================================================== --- trunk/pywikipedia/wikipedia.py 2008-05-20 20:22:19 UTC (rev 5415) +++ trunk/pywikipedia/wikipedia.py 2008-05-20 21:34:18 UTC (rev 5416) @@ -167,6 +167,9 @@ class IsNotRedirectPage(Error): """Page is not a redirect page"""
+class InvalidTitle(Error): + """Invalid page title""" + class LockedPage(Error): """Page is locked"""
@@ -328,7 +331,7 @@ try: t = url2unicode(t, site = insite, site2 = site) except UnicodeDecodeError: - raise Error(u'Bad page title : %s' % t) + raise InvalidTitle(u'Bad page title : %s' % t)
# Normalize unicode string to a NFC (composed) format to allow # proper string comparisons. According to @@ -354,7 +357,7 @@ self._namespace = defaultNamespace
if not t: - raise Error(u"Invalid title '%s'" % title ) + raise InvalidTitle(u"Invalid title '%s'" % title )
self._namespace = defaultNamespace # @@ -1069,7 +1072,7 @@ output(u"DBG> invalid <li> item in Whatlinkshere: %s" % link) try: p = Page(self.site(), title) - except Error: + except InvalidTitle: output(u"DBG> Whatlinkshere:%s contains invalid link to %s" % (self.title(), title)) continue @@ -1564,7 +1567,7 @@ output( u"ERROR: link from %s to [[%s:%s]] contains invalid unicode reference?!" % (self.aslink(), newSite, newTitle)) - except Error: + except InvalidTitle: output( u"ERROR: link from %s to [[%s:%s]] is improperly formatted?" % (self.aslink(), newSite, newTitle)) @@ -1633,7 +1636,7 @@ if not self.site().isInterwikiLink(title): try: page = Page(self.site(), title) - except Error: + except InvalidTitle: output(u"Page %s contains invalid link to [[%s]]." % (self.title(), title)) continue @@ -1740,7 +1743,7 @@ continue try: name = Page(self.site(), name).title() - except Error: + except InvalidTitle: if name.strip(): output( u"Page %s contains invalid template name {{%s}}." @@ -2613,7 +2616,7 @@ for match in lineR.finditer(titleList): try: yield Page(self.site(), match.group('title')) - except Error: + except InvalidTitle: output( u"Image description page %s contains invalid reference to [[%s]]." % (self.title(), match.group('title'))) @@ -3247,7 +3250,7 @@ site = insite.getSite(code = lang) try: result[site] = Page(site, pagetitle, insite = insite) - except Error: + except InvalidTitle: output( u"[getLanguageLinks] Text contains invalid interwiki link [[%s:%s]]." % (lang, pagetitle))