jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1235310?usp=email )

Change subject: UA: Cleanup UA format string
......................................................................

UA: Cleanup UA format string

Replace the first occurrence of "family", "code", or "lang" with "site".
The "lang" variable never worked properly. All of these can be replaced
with "site", which is recognized by Wikimedia traffic management.
Also replace "script_product" by "script" and "version" by "revision".
Replace {script_product} with {username}/{script} in user_agent_format.
Update tests accordingly.

Bug: T414201
Change-Id: I65dda068b0b22ff76cfa27cb5e2610f31f2e2f9f
---
M pywikibot/comms/http.py
M pywikibot/config.py
M tests/dry_site_tests.py
M tests/http_tests.py
4 files changed, 86 insertions(+), 29 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index 4740e03..d248334 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -56,7 +56,7 @@
     ServerError,
 )
 from pywikibot.logging import critical, debug, error, log, warning
-from pywikibot.tools import file_mode_checker
+from pywikibot.tools import file_mode_checker, issue_deprecation_warning


 try:
@@ -160,10 +160,31 @@
     """User-agent formatter to load version/revision only if necessary."""

     def get_value(self, key, args, kwargs):
-        """Get field as usual except for version and revision."""
-        # This is the Pywikibot version; also map it to {revision} at present.
+        """Lazy load revision key. Also replace deprecated variables."""
+        replacements = {
+            'script_product': 'script',
+            'version': 'revision',
+        }
+        replacements.update(dict.fromkeys(['code', 'lang', 'family'], 'site'))
+
+        revision: str = ''
         if key in ('version', 'revision'):
-            return pywikibot.version.getversiondict()['rev']
+            # lazy load the revision
+            revision = pywikibot.version.getversiondict()['rev']
+            if key == 'revision':
+                return revision
+
+        if key in ('code', 'lang', 'family', 'script_product', 'version'):
+            repl = replacements[key]
+            issue_deprecation_warning(
+                f'{{{key}}} value for user_agent',
+                f'{{{repl}}}',
+                depth=7,
+                since='11.0.0'
+            )
+            if key == 'version':
+                return revision
+            return super().get_value(repl, args, kwargs)
         return super().get_value(key, args, kwargs)


@@ -208,6 +229,11 @@
                format_string: str | None = '') -> str:
     """Generate the user agent string for a given site and format.
 
+    .. versionchanged:: 11.0
+       ``code``, ``lang`` and ``family`` variables within format string
+       are no longer supported. They will be replaced  by ``site``
+       during deprecation period.
+
     :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
@@ -216,30 +242,22 @@
     :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'], ''))
+    values['script'] = pywikibot.bot.calledModuleName()
+    values['site'] = ''

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

     username = user_agent_username(site.username() if site else None)
+
     if site:
-        script_comments.append(str(site))
+        values['site'] = site.sitename
+        script_comments.append(site.sitename)

         if username:
-            username = user_agent_username(site.username())
             script_comments.append('User:' + username)

-        values.update({
-            'family': site.family.name,
-            'code': site.code,
-            'lang': (site.lang if site.siteinfo.is_cached('lang')
-                     else f'({site.code})'),
-            'site': str(site),
-        })
-
     values['username'] = username
     values['script_comments'] = '; '.join(script_comments)

diff --git a/pywikibot/config.py b/pywikibot/config.py
index 7adee70..16572d4 100644
--- a/pywikibot/config.py
+++ b/pywikibot/config.py
@@ -43,6 +43,7 @@
 import copy
 import os
 import platform
+import re
 import stat
 import sys
 import types
@@ -134,8 +135,8 @@
 # User agent format.
 # For the meaning and more help in customization see:
 # https://www.mediawiki.org/wiki/Manual:Pywikibot/User-agent
-user_agent_format = ('{script_product} ({script_comments}) {pwb} ({revision}) '
-                     '{http_backend} {python}')
+user_agent_format = ('{username}/{script} ({script_comments}) {pwb} '
+                     '({revision}) {python} {http_backend}')

 # User agent description
 # This is a free-form string that can be user to describe specific bot/tool,
@@ -1034,6 +1035,30 @@
 _modified = {_key for _key in _public_globals
              if _exec_globals[_key] != globals()[_key]}

+# UA variables deprecated since Pywikibot 11
+if 'user_agent_format' in _modified:
+    _right_user_agent_format = _exec_globals['user_agent_format']
+    _right_user_agent_format, _number_of_subs_made = re.subn(
+        r'\{(lang|code|family)\}', '{site}', _right_user_agent_format, count=1)
+    if _number_of_subs_made:
+        _right_user_agent_format = re.sub(
+            r'\{(lang|code|family)\}', '', _right_user_agent_format)
+    _right_user_agent_format = re.sub(
+        r'\{script_product\}', '{script}', _right_user_agent_format)
+    _right_user_agent_format = re.sub(
+        r'\{version\}', '{revision}', _right_user_agent_format)
+    msg = """
+Deprecated placeholders in "user_agent_format" detected since Pywikibot 11.0:
+- The first occurrence of "{lang}", "{code}" or "{family}" is replaced with
+  "{site}", others are removed.
+- All occurrences of "{script_product}" replaced with "{script}".
+- All occurrences of "{version}" replaced with "{revision}".
+"""
+    if _right_user_agent_format != _exec_globals['user_agent_format']:
+        warn(msg, _ConfigurationDeprecationWarning, stacklevel=2)
+        _exec_globals['user_agent_format'] = _right_user_agent_format
+    del _right_user_agent_format, _number_of_subs_made
+
 for _key in _modified:
     globals()[_key] = _exec_globals[_key]

diff --git a/tests/dry_site_tests.py b/tests/dry_site_tests.py
index c8e492e..8b5e39e 100755
--- a/tests/dry_site_tests.py
+++ b/tests/dry_site_tests.py
@@ -11,6 +11,7 @@

 import pywikibot
 from pywikibot.comms.http import user_agent, user_agent_username
+from pywikibot.tools import suppress_warnings
 from tests.aspects import DefaultDrySiteTestCase


@@ -65,12 +66,13 @@
         self.assertEqual('Pywikibot/' + pywikibot.__version__,
                          user_agent(x, format_string='{pwb}'))

-        self.assertEqual(x.family.name,
-                         user_agent(x, format_string='{family}'))
-        self.assertEqual(x.code,
-                         user_agent(x, format_string='{code}'))
-        self.assertEqual(x.family.name + ' ' + x.code,
-                         user_agent(x, format_string='{family} {code}'))
+        # {family} {lang} and {code} are replaced with {site}
+        # since Pywikibot 11.0
+        for format_string in ('{family}', '{code}', '{lang}'):
+            with suppress_warnings(f'{format_string} value for user_agent',
+                                   category=FutureWarning):
+                self.assertEqual(x.sitename,
+                                 user_agent(x, format_string=format_string))

         self.assertEqual(x.username(),
                          user_agent(x, format_string='{username}'))
@@ -90,8 +92,11 @@
         script_value = (pywikibot.calledModuleName() + '/'
                         + pywikibot.version.getversiondict()['rev'])

-        self.assertEqual(script_value + ' Pywikibot/6.0 (User:foo_bar)',
-                         user_agent(x, format_string=old_config))
+        # {version} is replaced with {revision} since Pywikibot 11.0
+        with suppress_warnings('{version} value for user_agent',
+                               category=FutureWarning):
+            self.assertEqual(script_value + ' Pywikibot/6.0 (User:foo_bar)',
+                             user_agent(x, format_string=old_config))

         x._userinfo = {'name': '⁂'}
         x._username = '⁂'
diff --git a/tests/http_tests.py b/tests/http_tests.py
index 1a7e3bf..e49743e 100755
--- a/tests/http_tests.py
+++ b/tests/http_tests.py
@@ -23,7 +23,12 @@
 from pywikibot.exceptions import FatalServerError, Server504Error
 from pywikibot.tools import PYTHON_VERSION, THREADING_FREE, suppress_warnings
 from tests import join_images_path, utils
-from tests.aspects import HttpbinTestCase, TestCase, require_modules
+from tests.aspects import (
+    DeprecationTestCase,
+    HttpbinTestCase,
+    TestCase,
+    require_modules,
+)


 class HttpTestCase(TestCase):
@@ -176,7 +181,7 @@
                          'https://community.fandom.com/wiki/Community_Central')


-class UserAgentTestCase(TestCase):
+class UserAgentTestCase(DeprecationTestCase):

     """User agent formatting tests using a format string."""

@@ -192,7 +197,11 @@
         self.assertEqual(http.user_agent_username(),
                          http.user_agent(format_string='{username}'))
         self.assertEqual('', http.user_agent(format_string='{family}'))
+        self.assertOneDeprecationParts(
+            '{family} value for user_agent', '{site}')
         self.assertEqual('', http.user_agent(format_string='{lang}'))
+        self.assertOneDeprecationParts('{lang} value for user_agent', '{site}')
+        self.assertEqual('', http.user_agent(format_string='{site}'))

         self.assertEqual('Pywikibot/' + pywikibot.__version__,
                          http.user_agent(format_string='{pwb}'))

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1235310?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I65dda068b0b22ff76cfa27cb5e2610f31f2e2f9f
Gerrit-Change-Number: 1235310
Gerrit-PatchSet: 6
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to