Skip to content

Commit

Permalink
[cleanup] remove mw 1.14 and mw 1.22 Siteinfo fallback implementation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
xqt committed Sep 29, 2023
1 parent ad4ac3e commit 4d3d64a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 45 deletions.
40 changes: 5 additions & 35 deletions pywikibot/site/_siteinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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."""
Expand Down Expand Up @@ -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,
Expand All @@ -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)))
Expand All @@ -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:
Expand Down
13 changes: 5 additions & 8 deletions tests/siteinfo_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 5 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4d3d64a

Please sign in to comment.