jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/812248 )
Change subject: [IMPR] Move tzoneFixedOffset class to time module ......................................................................
[IMPR] Move tzoneFixedOffset class to time module
tzoneFixedOffset was originally implemented in archivebot script by Mpaa and moved to textlib in an early state of Pywikibot 2.0
- rename tzoneFixedOffset to TZoneFixedOffset - use single underscore variables instead of double underscores because name mangling is not necessary here. (Similar to class timezone(tzinfo) https://github.com/python/cpython/blob/3.10/Lib/datetime.py) - deprecate textlib.tzoneFixedOffset
Change-Id: I72ef48caa747d39a5b8bfcf521fb3d80ef53fc7c --- M pywikibot/textlib.py M pywikibot/time.py M tests/timestripper_tests.py 3 files changed, 52 insertions(+), 43 deletions(-)
Approvals: Mpaa: Looks good to me, but someone else must approve Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py index ee70af9..cc94eaf 100644 --- a/pywikibot/textlib.py +++ b/pywikibot/textlib.py @@ -10,7 +10,6 @@ # # Distributed under the terms of the MIT license. # -import datetime import re from collections import OrderedDict, namedtuple from collections.abc import Sequence @@ -25,7 +24,8 @@ from pywikibot.backports import Tuple from pywikibot.exceptions import InvalidTitleError, SiteDefinitionError from pywikibot.family import Family -from pywikibot.tools import deprecated +from pywikibot.time import TZoneFixedOffset +from pywikibot.tools import deprecated, ModuleDeprecationWrapper from pywikibot.userinterfaces.transliteration import NON_LATIN_DIGITS
@@ -1829,41 +1829,6 @@ # Time parsing functionality (Archivebot) # ---------------------------------------
-class tzoneFixedOffset(datetime.tzinfo): - - """ - Class building tzinfo objects for fixed-offset time zones. - - :param offset: a number indicating fixed offset in minutes east from UTC - :param name: a string with name of the timezone - """ - - def __init__(self, offset: int, name: str) -> None: - """Initializer.""" - self.__offset = datetime.timedelta(minutes=offset) - self.__name = name - - def utcoffset(self, dt): - """Return the offset to UTC.""" - return self.__offset - - def tzname(self, dt): - """Return the name of the timezone.""" - return self.__name - - def dst(self, dt): - """Return no daylight savings time.""" - return datetime.timedelta(0) - - def __repr__(self) -> str: - """Return the internal representation of the timezone.""" - return '{}({}, {})'.format( - self.__class__.__name__, - self.__offset.days * 86400 + self.__offset.seconds, - self.__name - ) - - class TimeStripper:
"""Find timestamp in page and return it as pywikibot.Timestamp object.""" @@ -1929,7 +1894,7 @@ self._wikilink_pat = re.compile( r'[[(?P<link>[^]|]*?)(?P<anchor>|[^]]*)?]]')
- self.tzinfo = tzoneFixedOffset(self.site.siteinfo['timeoffset'], + self.tzinfo = TZoneFixedOffset(self.site.siteinfo['timeoffset'], self.site.siteinfo['timezone'])
@staticmethod @@ -2108,3 +2073,10 @@ timestamp = None
return timestamp + + +wrapper = ModuleDeprecationWrapper(__name__) +wrapper.add_deprecated_attr( + 'tzoneFixedOffset', + replacement_name='pywikibot.time.TZoneFixedOffset', + since='7.5.0') diff --git a/pywikibot/time.py b/pywikibot/time.py index 0e14332..b5240a9 100644 --- a/pywikibot/time.py +++ b/pywikibot/time.py @@ -22,6 +22,7 @@ 'str2timedelta', 'MW_KEYS', 'Timestamp', + 'TZoneFixedOffset' )
#: ..versionadded:: 7.5 @@ -309,6 +310,41 @@ return newdt
+class TZoneFixedOffset(datetime.tzinfo): + + """ + Class building tzinfo objects for fixed-offset time zones. + + :param offset: a number indicating fixed offset in minutes east from UTC + :param name: a string with name of the timezone + """ + + def __init__(self, offset: int, name: str) -> None: + """Initializer.""" + self._offset = datetime.timedelta(minutes=offset) + self._name = name + + def utcoffset(self, dt): + """Return the offset to UTC.""" + return self._offset + + def tzname(self, dt): + """Return the name of the timezone.""" + return self._name + + def dst(self, dt): + """Return no daylight savings time.""" + return datetime.timedelta(0) + + def __repr__(self) -> str: + """Return the internal representation of the timezone.""" + return '{}({}, {})'.format( + self.__class__.__name__, + self._offset.days * 86400 + self._offset.seconds, + self._name + ) + + def str2timedelta(string: str, timestamp=None) -> datetime.timedelta: """ Return a timedelta for a shorthand duration. diff --git a/tests/timestripper_tests.py b/tests/timestripper_tests.py index d2ef6d9..b178bac 100755 --- a/tests/timestripper_tests.py +++ b/tests/timestripper_tests.py @@ -9,7 +9,8 @@ import re from contextlib import suppress
-from pywikibot.textlib import TimeStripper, tzoneFixedOffset +from pywikibot.textlib import TimeStripper +from pywikibot.time import TZoneFixedOffset from tests.aspects import TestCase, unittest
@@ -152,7 +153,7 @@ self.ts.timestripper( '2000 people will attend. --12:12, 14 December 2015 (UTC)'), datetime.datetime( - 2015, 12, 14, 12, 12, tzinfo=tzoneFixedOffset(0, 'UTC'))) + 2015, 12, 14, 12, 12, tzinfo=TZoneFixedOffset(0, 'UTC')))
class TestTimeStripperLanguage(TestCase): @@ -224,7 +225,7 @@ """Test that correct date is matched.""" self.ts = TimeStripper(self.get_site(key))
- tzone = tzoneFixedOffset(self.ts.site.siteinfo['timeoffset'], + tzone = TZoneFixedOffset(self.ts.site.siteinfo['timeoffset'], self.ts.site.siteinfo['timezone'])
txt_match = self.sites[key]['match'] @@ -275,7 +276,7 @@
date = '06:57 06 June 2015 (UTC)' fake_date = '05:57 06 June 2015 (UTC)' - tzone = tzoneFixedOffset(0, 'UTC') + tzone = TZoneFixedOffset(0, 'UTC') expected_date = datetime.datetime(2015, 6, 6, 6, 57, tzinfo=tzone)
def test_timestripper_match_comment(self): @@ -391,7 +392,7 @@ username = '[[User:DoNotArchiveUntil]]' date = '06:57 06 June 2015 (UTC)' user_and_date = username + ' ' + date - tzone = tzoneFixedOffset(0, 'UTC') + tzone = TZoneFixedOffset(0, 'UTC')
def test_timestripper_match(self): """Test that dates in comments are correctly recognised."""
pywikibot-commits@lists.wikimedia.org