jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/650158 )
Change subject: [IMPR] use functools.cache to cache version.getversiondict()
......................................................................
[IMPR] use functools.cache to cache version.getversiondict()
- use backports cache for version.getversiondict()
- update dry_site_tests.TestDrySite
- update http_tests.UserAgentTestCase and remove test_version method
- update documentation in http._UserAgentFormatter
- simplify http.user_agent() function
Change-Id: Id12e6f8c32ce920f417aaa4d8c88b2a57de46344
---
M pywikibot/comms/http.py
M pywikibot/version.py
M tests/dry_site_tests.py
M tests/http_tests.py
4 files changed, 13 insertions(+), 38 deletions(-)
Approvals:
Matěj Suchánek: Looks good to me, but someone else must approve
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index 3e2748f..8e3a0be 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -96,7 +96,7 @@
def get_value(self, key, args, kwargs):
"""Get field as usual except for version and revision."""
- # This is the Pywikibot revision; also map it to {version} at present.
+ # This is the Pywikibot version; also map it to {revision} at present.
if key == 'version' or key == 'revision':
return pywikibot.version.getversiondict()['rev']
return super().get_value(key, args, kwargs)
@@ -143,16 +143,10 @@
@return: The formatted user agent
"""
values = USER_AGENT_PRODUCTS.copy()
-
- script_name = pywikibot.bot.calledModuleName()
-
- values['script'] = script_name
-
- # TODO: script_product should add the script version, if known
- values['script_product'] = script_name
+ values.update(dict.fromkeys(['script', 'script_product'],
+ pywikibot.bot.calledModuleName()))
script_comments = []
- username = ''
if config.user_agent_description:
script_comments.append(config.user_agent_description)
@@ -161,6 +155,7 @@
values['lang'] = '' # TODO: use site.lang, if known
values['site'] = ''
+ username = ''
if site:
script_comments.append(str(site))
diff --git a/pywikibot/version.py b/pywikibot/version.py
index cdf0d6d..d821a56 100644
--- a/pywikibot/version.py
+++ b/pywikibot/version.py
@@ -1,6 +1,6 @@
"""Module to determine the pywikibot version (tag, revision and date)."""
#
-# (C) Pywikibot team, 2007-2020
+# (C) Pywikibot team, 2007-2021
#
# Distributed under the terms of the MIT license.
#
@@ -23,11 +23,12 @@
import pywikibot
+from pywikibot.backports import cache
from pywikibot.comms.http import fetch
from pywikibot import config2 as config
from pywikibot.tools import deprecated
-cache = None
+
_logger = 'version'
@@ -63,7 +64,7 @@
'master': 'branches/master',
'stable': 'branches/stable',
}
- data = dict(getversiondict()) # copy dict to prevent changes in 'cache'
+ data = getversiondict()
data['cmp_ver'] = 'n/a'
local_hsh = data.get('hsh', '')
hsh = {}
@@ -82,6 +83,7 @@
return '{tag} ({hsh}, {rev}, {date}, {cmp_ver})'.format_map(data)
+@cache
def getversiondict():
"""Get version info for the package.
@@ -92,10 +94,6 @@
- hash (git hash for the current revision)
@rtype: C{dict} of four C{str}
"""
- global cache
- if cache:
- return cache
-
_program_dir = _get_program_dir()
exceptions = {}
@@ -132,8 +130,7 @@
warn('Unable to detect package date', UserWarning)
datestring = '-2 (unknown)'
- cache = {'tag': tag, 'rev': rev, 'date': datestring, 'hsh': hsh}
- return cache
+ return {'tag': tag, 'rev': rev, 'date': datestring, 'hsh': hsh}
def svn_rev_info(path):
diff --git a/tests/dry_site_tests.py b/tests/dry_site_tests.py
index 9cc91a4..3b2ba4d 100644
--- a/tests/dry_site_tests.py
+++ b/tests/dry_site_tests.py
@@ -84,13 +84,12 @@
self.assertEqual('foo_bar', user_agent(x, format_string='{username}'))
- old_config = '{script}/{version} Pywikibot/2.0 (User:{username})'
+ old_config = '{script}/{version} Pywikibot/6.0 (User:{username})'
- pywikibot.version.getversiondict()
script_value = (pywikibot.calledModuleName() + '/'
- + pywikibot.version.cache['rev'])
+ + pywikibot.version.getversiondict()['rev'])
- self.assertEqual(script_value + ' Pywikibot/2.0 (User:foo_bar)',
+ self.assertEqual(script_value + ' Pywikibot/6.0 (User:foo_bar)',
user_agent(x, format_string=old_config))
x._userinfo = {'name': '⁂'}
diff --git a/tests/http_tests.py b/tests/http_tests.py
index c0a4d63..7f5014a 100644
--- a/tests/http_tests.py
+++ b/tests/http_tests.py
@@ -209,24 +209,8 @@
self.assertEqual('.', http.user_agent_username('.'))
self.assertEqual("'", http.user_agent_username("'"))
self.assertEqual('foo_bar', http.user_agent_username('foo bar'))
-
self.assertEqual('%E2%81%82', http.user_agent_username('⁂'))
- def test_version(self):
- """Test http.user_agent {version}."""
- old_cache = pywikibot.version.cache
- try:
- pywikibot.version.cache = None
- http.user_agent(format_string='version does not appear')
- self.assertIsNone(pywikibot.version.cache)
- pywikibot.version.cache = {'rev': 'dummy'}
- self.assertEqual(
- http.user_agent(format_string='{version} does appear'),
- 'dummy does appear')
- self.assertIsNotNone(pywikibot.version.cache)
- finally:
- pywikibot.version.cache = old_cache
-
class DefaultUserAgentTestCase(TestCase):
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/650158
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Id12e6f8c32ce920f417aaa4d8c88b2a57de46344
Gerrit-Change-Number: 650158
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Matěj Suchánek <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits