jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/904743?usp=email )
Change subject: [IMPR] Improve api._generators and avoid similar code ......................................................................
[IMPR] Improve api._generators and avoid similar code
Change-Id: I5959bfe9686d918774529b98d45a3ac084848366 --- M pywikibot/data/api/_generators.py 1 file changed, 39 insertions(+), 34 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/data/api/_generators.py b/pywikibot/data/api/_generators.py index e32ed2e..de6f011 100644 --- a/pywikibot/data/api/_generators.py +++ b/pywikibot/data/api/_generators.py @@ -19,7 +19,7 @@
import pywikibot from pywikibot import config -from pywikibot.backports import Callable +from pywikibot.backports import Callable, Iterable from pywikibot.exceptions import Error, InvalidTitleError, UnsupportedPageError from pywikibot.site import Namespace from pywikibot.tools import deprecated @@ -963,25 +963,42 @@ page._coords = coords
-def update_page(page, pagedict: dict, props=None): - """Update attributes of Page object page, based on query data in pagedict. +def update_page(page: pywikibot.Page, + pagedict: dict[str, Any], + props: Iterable[str] | None = None) -> None: + """ + Update attributes of Page object *page*, based on query data in *pagedict*.
:param page: object to be updated - :type page: pywikibot.page.Page - :param pagedict: the contents of a "page" element of a query response - :param props: the property names which resulted in pagedict. If a missing - value in pagedict can indicate both 'false' and 'not present' the - property which would make the value present must be in the props - parameter. - :type props: iterable of string - :raises pywikibot.exceptions.InvalidTitleError: Page title is invalid - :raises pywikibot.exceptions.UnsupportedPageError: Page with namespace < 0 - is not supported yet + :param pagedict: the contents of a *page* element of a query + response + :param props: the property names which resulted in *pagedict*. If a + missing value in *pagedict* can indicate both 'false' and + 'not present' the property which would make the value present + must be in the *props* parameter. + :raises InvalidTitleError: Page title is invalid + :raises UnsupportedPageError: Page with namespace < 0 is not + supported yet """ _update_pageid(page, pagedict) _update_contentmodel(page, pagedict)
props = props or [] + + # test for pagedict content only and call updater function + for element in ('coordinates', 'revisions'): + if element in pagedict: + updater = globals()['_update_' + element] + updater(page, pagedict[element]) + + # test for pagedict and props contents, call updater or set attribute + for element in ('categories', 'langlinks', 'templates'): + if element in pagedict: + updater = globals()['_update_' + element] + updater(page, pagedict[element]) + elif element in props: + setattr(page, '_' + element, set()) + if 'info' in props: page._isredir = 'redirect' in pagedict
@@ -991,9 +1008,6 @@ if 'protection' in pagedict: _update_protection(page, pagedict)
- if 'revisions' in pagedict: - _update_revisions(page, pagedict['revisions']) - if 'lastrevid' in pagedict: page.latest_revision_id = pagedict['lastrevid']
@@ -1006,24 +1020,6 @@ if 'categoryinfo' in pagedict: page._catinfo = pagedict['categoryinfo']
- if 'templates' in pagedict: - _update_templates(page, pagedict['templates']) - elif 'templates' in props: - page._templates = set() - - if 'categories' in pagedict: - _update_categories(page, pagedict['categories']) - elif 'categories' in props: - page._categories = set() - - if 'langlinks' in pagedict: - _update_langlinks(page, pagedict['langlinks']) - elif 'langlinks' in props: - page._langlinks = set() - - if 'coordinates' in pagedict: - _update_coordinates(page, pagedict['coordinates']) - if 'pageimage' in pagedict: page._pageimage = pywikibot.FilePage(page.site, pagedict['pageimage'])
pywikibot-commits@lists.wikimedia.org