Xqt has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1328301?usp=email )
Change subject: archivebot: Fix locale and timezone options
......................................................................
archivebot: Fix locale and timezone options
Pass command-line option values directly to locale.setlocale and the TZ
environment variable. Encoding the locale produced unsupported bytes,
while accessing a timezone attribute on the parsed string made both
options fail before the bot could start.
Change-Id: Ia1abc28d8788039c556d827ef38f1959f8ed9acd
---
M scripts/archivebot.py
M tests/archivebot_tests.py
2 files changed, 31 insertions(+), 3 deletions(-)
Approvals:
Xqt: Verified; Looks good to me, approved
diff --git a/scripts/archivebot.py b/scripts/archivebot.py
index 350a1dd..f5f6dbe 100755
--- a/scripts/archivebot.py
+++ b/scripts/archivebot.py
@@ -157,6 +157,8 @@
-locale:LOCALE Switch to locale LOCALE.
+-timezone:ZONE Switch to timezone ZONE.
+
-namespace:NS Only archive pages from the given namespace.
-page:PAGE Archive a single PAGE. Default namespace is a user talk
@@ -1064,9 +1066,9 @@
filename = value
elif option == 'locale':
# Required for english month names
- locale.setlocale(locale.LC_TIME, value.encode('utf8'))
+ locale.setlocale(locale.LC_TIME, value)
elif option == 'timezone':
- os.environ['TZ'] = value.timezone
+ os.environ['TZ'] = value
# Or use the preset value
if hasattr(time, 'tzset'):
time.tzset()
diff --git a/tests/archivebot_tests.py b/tests/archivebot_tests.py
index f3840f7..d04cf18 100755
--- a/tests/archivebot_tests.py
+++ b/tests/archivebot_tests.py
@@ -11,7 +11,7 @@
from contextlib import suppress
from datetime import datetime
from types import SimpleNamespace
-from unittest.mock import Mock
+from unittest.mock import Mock, patch
import pywikibot
from pywikibot.exceptions import Error
@@ -83,6 +83,32 @@
net = False
+ def _run_main(self, option: str) -> None:
+ """Run main with site-dependent work bypassed."""
+ with patch.object(archivebot.pywikibot, 'handle_args',
+ return_value=[option]):
+ with patch.object(archivebot.pywikibot, 'Site'):
+ with patch.object(archivebot, 'show_md5_key',
+ return_value=True):
+ archivebot.main(option)
+
+ def test_locale_option(self) -> None:
+ """Test that the locale option passes a string to setlocale."""
+ with patch.object(archivebot.locale, 'setlocale') as setlocale:
+ self._run_main('-locale:C')
+
+ setlocale.assert_called_once_with(archivebot.locale.LC_TIME, 'C')
+
+ def test_timezone_option(self) -> None:
+ """Test that the timezone option sets the TZ environment variable."""
+ environ = {}
+ with patch.object(archivebot.os, 'environ', environ):
+ with patch.object(archivebot.time, 'tzset', create=True) as tzset:
+ self._run_main('-timezone:UTC')
+
+ self.assertEqual(environ['TZ'], 'UTC')
+ tzset.assert_called_once_with()
+
def test_get_params_reuses_isocalendar(self) -> None:
"""Test that the ISO calendar is calculated once."""
date = datetime(2024, 12, 30)
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1328301?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: Ia1abc28d8788039c556d827ef38f1959f8ed9acd
Gerrit-Change-Number: 1328301
Gerrit-PatchSet: 3
Gerrit-Owner: Mahveotm <[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]