John Vandenberg has uploaded a new change for review. https://gerrit.wikimedia.org/r/179439
Change subject: Move templatesWithParams from BasePage into Page ...................................................................... Move templatesWithParams from BasePage into Page templatesWithParams has no suitable implementation for a WikibasePage, and causes exceptions when mwparserfromhell is not available and textlib.extract_templates_and_params_regex is used as a fallback. Bug: T71664 Change-Id: Ia225ba19426c8819a23aa3c9a803d58302bf796e --- M pywikibot/page.py M tests/wikibase_tests.py 2 files changed, 53 insertions(+), 58 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/39/179439/1 diff --git a/pywikibot/page.py b/pywikibot/page.py index 192d3e1..08eddc3 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -1282,57 +1282,6 @@ return self.site.pageimages(self, step=step, total=total, content=content) - @deprecate_arg("get_redirect", None) - def templatesWithParams(self): - """Iterate templates used on this Page. - - @return: a generator that yields a tuple for each use of a template - in the page, with the template Page as the first entry and a list of - parameters as the second entry. - - """ - # WARNING: may not return all templates used in particularly - # intricate cases such as template substitution - titles = list(t.title() for t in self.templates()) - templates = textlib.extract_templates_and_params(self.text) - # backwards-compatibility: convert the dict returned as the second - # element into a list in the format used by old scripts - result = [] - for template in templates: - link = pywikibot.Link(template[0], self.site, - defaultNamespace=10) - try: - if link.canonical_title() not in titles: - continue - except pywikibot.Error: - # this is a parser function or magic word, not template name - continue - args = template[1] - intkeys = {} - named = {} - positional = [] - for key in sorted(args): - try: - intkeys[int(key)] = args[key] - except ValueError: - named[key] = args[key] - for i in range(1, len(intkeys) + 1): - # only those args with consecutive integer keys can be - # treated as positional; an integer could also be used - # (out of order) as the key for a named argument - # example: {{tmp|one|two|5=five|three}} - if i in intkeys: - positional.append(intkeys[i]) - else: - for k in intkeys: - if k < 1 or k >= i: - named[str(k)] = intkeys[k] - break - for name in named: - positional.append("%s=%s" % (name, named[name])) - result.append((pywikibot.Page(link, self.site), positional)) - return result - @deprecated_args(nofollow_redirects=None, get_redirect=None) def categories(self, withSortKey=False, step=None, total=None, content=False): @@ -1845,6 +1794,57 @@ 'if source is a Site.') super(Page, self).__init__(source, title, ns) + @deprecate_arg("get_redirect", None) + def templatesWithParams(self): + """Iterate templates used on this Page. + + @return: a generator that yields a tuple for each use of a template + in the page, with the template Page as the first entry and a list of + parameters as the second entry. + + """ + # WARNING: may not return all templates used in particularly + # intricate cases such as template substitution + titles = list(t.title() for t in self.templates()) + templates = textlib.extract_templates_and_params(self.text) + # backwards-compatibility: convert the dict returned as the second + # element into a list in the format used by old scripts + result = [] + for template in templates: + link = pywikibot.Link(template[0], self.site, + defaultNamespace=10) + try: + if link.canonical_title() not in titles: + continue + except pywikibot.Error: + # this is a parser function or magic word, not template name + continue + args = template[1] + intkeys = {} + named = {} + positional = [] + for key in sorted(args): + try: + intkeys[int(key)] = args[key] + except ValueError: + named[key] = args[key] + for i in range(1, len(intkeys) + 1): + # only those args with consecutive integer keys can be + # treated as positional; an integer could also be used + # (out of order) as the key for a named argument + # example: {{tmp|one|two|5=five|three}} + if i in intkeys: + positional.append(intkeys[i]) + else: + for k in intkeys: + if k < 1 or k >= i: + named[str(k)] = intkeys[k] + break + for name in named: + positional.append("%s=%s" % (name, named[name])) + result.append((pywikibot.Page(link, self.site), positional)) + return result + class FilePage(Page): diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py index ca49782..0a39680 100644 --- a/tests/wikibase_tests.py +++ b/tests/wikibase_tests.py @@ -538,20 +538,15 @@ class TestPageMethods(WikidataTestCase): - """Test cases to test methods of Page() behave correctly with Wikibase.""" + """Test behaviour of WikibasePage methods inherited from BasePage.""" def test_page_methods(self): - """Test ItemPage methods inherited from superclass Page.""" + """Test ItemPage methods inherited from superclass BasePage.""" self.wdp = pywikibot.ItemPage(self.get_repo(), 'Q60') self.wdp.previousRevision() self.assertEqual(self.wdp.langlinks(), []) self.assertEqual(self.wdp.templates(), []) self.assertFalse(self.wdp.isCategoryRedirect()) - - def test_item_templates(self): - """Test templatesWithParams.""" - self.wdp = pywikibot.ItemPage(self.get_repo(), 'Q60') - self.wdp.templatesWithParams() def test_item_bot_may_edit(self): """Test botMayEdit.""" -- To view, visit https://gerrit.wikimedia.org/r/179439 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ia225ba19426c8819a23aa3c9a803d58302bf796e Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: John Vandenberg <jay...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits