jenkins-bot submitted this change.

View Change


Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[bugfix] Use site.lang instead of site.code in http.user_agent()

- if lang is already cached in site.siteinfo, use it for values in
http.user_agent(). Otherwise use site.code delimited in brackets
instead.
- simplify code to update values
- update DummySiteinfo
- update documentation and type hints

Bug: T228322
Change-Id: I54403e06a7304d036c3c8587a751d050e4e3b9e6
---
M tests/utils.py
M pywikibot/comms/http.py
2 files changed, 40 insertions(+), 14 deletions(-)

diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index 5ef5f3a..f682298 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -188,29 +188,26 @@
return username


-def user_agent(site=None, format_string: str = None) -> str:
- """
- Generate the user agent string for a given site and format.
+def user_agent(site: Optional['pywikibot.site.BaseSite'] = None,
+ format_string: str = '') -> str:
+ """Generate the user agent string for a given site and format.

- :param site: The site for which this user agent is intended. May be None.
- :type site: BaseSite
- :param format_string: The string to which the values will be added using
- str.format. Is using config.user_agent_format when it is None.
+ :param site: The site for which this user agent is intended. May be
+ None.
+ :param format_string: The string to which the values will be added
+ using str.format. Is using config.user_agent_format when it is
+ empty.
:return: The formatted user agent
"""
values = USER_AGENT_PRODUCTS.copy()
values.update(dict.fromkeys(['script', 'script_product'],
pywikibot.bot.calledModuleName()))
+ values.update(dict.fromkeys(['family', 'code', 'lang', 'site'], ''))

script_comments = []
if config.user_agent_description:
script_comments.append(config.user_agent_description)

- values['family'] = ''
- values['code'] = ''
- values['lang'] = '' # TODO: use site.lang, if known
- values['site'] = ''
-
username = ''
if site:
script_comments.append(str(site))
@@ -224,7 +221,8 @@
values.update({
'family': site.family.name,
'code': site.code,
- 'lang': site.code, # TODO: use site.lang, if known
+ 'lang': (site.lang if site.siteinfo.is_cached('lang')
+ else f'({site.code})'),
'site': str(site),
})

diff --git a/tests/utils.py b/tests/utils.py
index c2e483d..5f93270 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -244,7 +244,11 @@

class DummySiteinfo:

- """Dummy class to use instead of :py:obj:`pywikibot.site.Siteinfo`."""
+ """Dummy Siteinfo class.
+
+ To be used instead of :class:`pywikibot.site.Siteinfo
+ <pywikibot.site._siteinfo.Siteinfo>`.
+ """

def __init__(self, cache):
"""Initializer."""
@@ -282,6 +286,13 @@
"""Return False."""
return False

+ def is_cached(self, key: str) -> bool:
+ """Return whether the key is cached.
+
+ .. versionadded:: 8.3
+ """
+ return key in self._cache
+
def is_recognised(self, key):
"""Return None."""
return None

To view, visit change 935801. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I54403e06a7304d036c3c8587a751d050e4e3b9e6
Gerrit-Change-Number: 935801
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Mpaa <mpaa.wiki@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: Zhuyifei1999 <zhuyifei1999@gmail.com>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged