From 4d3d64ab4c7a677b599041f7450d5a0056a2d9a7 Mon Sep 17 00:00:00 2001 From: xqt Date: Thu, 21 Sep 2023 10:45:52 +0200 Subject: [PATCH] [cleanup] remove mw 1.14 and mw 1.22 Siteinfo fallback implementation Siteinfo._get_default was introduced to support defaults for 'restrictions' (introduced with mw 1.23) and 'fileextensions' (introduces with mw 1.15). These fallbacks can be dropped. This partly reverts 0f1c546 and f7a399e Change-Id: I961c25bf06d7608f51af98e50a94431f0b475075 --- pywikibot/site/_siteinfo.py | 40 +++++-------------------------------- tests/siteinfo_tests.py | 13 +++++------- tests/utils.py | 7 +++++-- 3 files changed, 15 insertions(+), 45 deletions(-) diff --git a/pywikibot/site/_siteinfo.py b/pywikibot/site/_siteinfo.py index 4aa01df66f..6585c3e58c 100644 --- a/pywikibot/site/_siteinfo.py +++ b/pywikibot/site/_siteinfo.py @@ -12,6 +12,7 @@ from typing import Any, Optional, Union import pywikibot +from pywikibot.backports import Dict, List from pywikibot.exceptions import APIError from pywikibot.tools.collections import EMPTY_DEFAULT @@ -58,7 +59,7 @@ class Siteinfo(Container): def __init__(self, site) -> None: """Initialise it with an empty cache.""" self._site = site - self._cache = {} + self._cache: Dict[str, Any] = {} def clear(self) -> None: """Remove all items from Siteinfo. @@ -67,37 +68,6 @@ def clear(self) -> None: """ self._cache.clear() - @staticmethod - def _get_default(key: str): - """ - Return the default value for different properties. - - If the property is 'restrictions' it returns a dictionary with: - - 'cascadinglevels': 'sysop' - - 'semiprotectedlevels': 'autoconfirmed' - - 'levels': '' (everybody), 'autoconfirmed', 'sysop' - - 'types': 'create', 'edit', 'move', 'upload' - Otherwise it returns :py:obj:`tools.EMPTY_DEFAULT`. - - :param key: The property name - :return: The default value - :rtype: dict or :py:obj:`tools.EmptyDefault` - """ - if key == 'restrictions': - # implemented in b73b5883d486db0e9278ef16733551f28d9e096d - return { - 'cascadinglevels': ['sysop'], - 'semiprotectedlevels': ['autoconfirmed'], - 'levels': ['', 'autoconfirmed', 'sysop'], - 'types': ['create', 'edit', 'move', 'upload'] - } - - if key == 'fileextensions': - # the default file extensions in MediaWiki - return [{'ext': ext} for ext in ['png', 'gif', 'jpg', 'jpeg']] - - return EMPTY_DEFAULT - @staticmethod def _post_process(prop, data) -> None: """Do some default handling of data. Directly modifies data.""" @@ -150,7 +120,7 @@ def warn_handler(mod, message) -> bool: if not props: raise ValueError('At least one property name must be provided.') - invalid_properties = [] + invalid_properties: List[str] = [] request = self._site._request( expiry=pywikibot.config.API_config_expiry if expiry is False else expiry, @@ -168,7 +138,7 @@ def warn_handler(mod, message) -> bool: if len(props) == 1: pywikibot.log( f"Unable to get siprop '{props[0]}'") - return {props[0]: (Siteinfo._get_default(props[0]), False)} + return {props[0]: (EMPTY_DEFAULT, False)} pywikibot.log('Unable to get siteinfo, because at least ' "one property is unknown: '{}'".format( "', '".join(props))) @@ -181,7 +151,7 @@ def warn_handler(mod, message) -> bool: result = {} if invalid_properties: for prop in invalid_properties: - result[prop] = (Siteinfo._get_default(prop), False) + result[prop] = (EMPTY_DEFAULT, False) pywikibot.log("Unable to get siprop(s) '{}'".format( "', '".join(invalid_properties))) if 'query' in data: diff --git a/tests/siteinfo_tests.py b/tests/siteinfo_tests.py index 020cf2e593..f39a6c5a28 100755 --- a/tests/siteinfo_tests.py +++ b/tests/siteinfo_tests.py @@ -53,20 +53,17 @@ def test_siteinfo_boolean(self): self.assertIsInstance(mysite.namespaces[0].subpages, bool) self.assertIsInstance(mysite.namespaces[0].content, bool) - def test_properties_with_defaults(self): - """Test the siteinfo properties with defaults.""" - # This does not test that the defaults work correct, - # unless the default site is a version needing these defaults + def test_properties(self): + """Test the siteinfo properties.""" # 'fileextensions' introduced in v1.15: - self.assertIsInstance(self.site.siteinfo.get('fileextensions'), list) self.assertIn('fileextensions', self.site.siteinfo) fileextensions = self.site.siteinfo.get('fileextensions') + self.assertIsInstance(fileextensions, list) self.assertIn({'ext': 'png'}, fileextensions) # 'restrictions' introduced in v1.23: - mysite = self.site - self.assertIsInstance(mysite.siteinfo.get('restrictions'), dict) - self.assertIn('restrictions', mysite.siteinfo) + self.assertIn('restrictions', self.site.siteinfo) restrictions = self.site.siteinfo.get('restrictions') + self.assertIsInstance(restrictions, dict) self.assertIn('cascadinglevels', restrictions) def test_no_cache(self): diff --git a/tests/utils.py b/tests/utils.py index 6d76c97fd2..8c0ce06a36 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -20,6 +20,7 @@ from pywikibot.data.api import Request as _original_Request from pywikibot.exceptions import APIError from pywikibot.login import LoginStatus +from pywikibot.tools.collections import EMPTY_DEFAULT from pywikibot.site import Namespace from pywikibot.tools import PYTHON_VERSION @@ -277,7 +278,7 @@ def get(self, key, get_default=True, cache=True, expiry=False): return loaded[0] if get_default: - default = pywikibot.site.Siteinfo._get_default(key) + default = EMPTY_DEFAULT if cache: self._cache[key] = (default, False) return default @@ -342,7 +343,9 @@ def __init__(self, code, fam, user): super().__init__(code, fam, user) self._userinfo = pywikibot.tools.collections.EMPTY_DEFAULT self._paraminfo = DryParamInfo() - self._siteinfo = DummySiteinfo({}) + # setup a default siteinfo used by dry tests + default_siteinfo = {'fileextensions': [{'ext': 'jpg'}]} + self._siteinfo = DummySiteinfo(default_siteinfo) self._siteinfo._cache['lang'] = (code, True) self._siteinfo._cache['case'] = ( 'case-sensitive' if self.family.name == 'wiktionary' else