http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10024
Revision: 10024 Author: valhallasw Date: 2012-03-18 17:08:41 +0000 (Sun, 18 Mar 2012) Log Message: ----------- Split off section recognition functionality to textlib.py.
See also: pywikipediabot-Bugs-2776438
Modified Paths: -------------- trunk/pywikipedia/pywikibot/textlib.py trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/pywikibot/textlib.py =================================================================== --- trunk/pywikipedia/pywikibot/textlib.py 2012-03-17 18:03:32 UTC (rev 10023) +++ trunk/pywikipedia/pywikibot/textlib.py 2012-03-18 17:08:41 UTC (rev 10024) @@ -931,3 +931,14 @@ text += u'|%s=%s\n' % (item, params[item])
return u'{{%s\n%s}}' % (template, text) + +#---------------------------------- +# Page parsing functionality +#---------------------------------- + +def does_text_contain_section(pagetext, section): + """ Determines whether the page text contains the given + section title. + """ + m = re.search("=+[ ']*%s[ ']*=+" % re.escape(section), pagetext) + return bool(m)
Modified: trunk/pywikipedia/wikipedia.py =================================================================== --- trunk/pywikipedia/wikipedia.py 2012-03-17 18:03:32 UTC (rev 10023) +++ trunk/pywikipedia/wikipedia.py 2012-03-18 17:08:41 UTC (rev 10024) @@ -823,14 +823,13 @@ self._redirarg = redirtarget else: raise IsRedirectPage(redirtarget) - if self.section(): - m = re.search("=+[ ']*%s[ ']*=+" % re.escape(self.section()), - pageInfo['revisions'][0]['*']) - if not m: - try: - self._getexception - except AttributeError: - raise SectionError # Page has no section by this name + + if self.section() and \ + not textlib.does_text_contain_section(pagetext, self.section()): + try: + self._getexception + except AttributeError: + raise SectionError # Page has no section by this name return pagetext
def _getEditPageOld(self, get_redirect=False, throttle=True, sysop=False, @@ -965,15 +964,14 @@ self._redirarg = redirtarget else: raise IsRedirectPage(redirtarget) - if self.section(): - m = re.search("=+[ ']*%s[ ']*=+" % re.escape(self.section()), - text) - if not m: - try: - self._getexception - except AttributeError: - raise SectionError # Page has no section by this name
+ if self.section() and \ + not textlib.does_text_contain_section(text, self.section()): + try: + self._getexception + except AttributeError: + raise SectionError # Page has no section by this name + return pagetext
def getOldVersion(self, oldid, force=False, get_redirect=False,
pywikipedia-svn@lists.wikimedia.org