jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/339938 )
Change subject: archivebot.py:Fix string checking in all functions ......................................................................
archivebot.py:Fix string checking in all functions
Bug: T157742 Change-Id: Id3a668755ae03cdb444e22eb249201e59e5dac1c --- M scripts/archivebot.py M tests/archivebot_tests.py 2 files changed, 37 insertions(+), 10 deletions(-)
Approvals: jenkins-bot: Verified Xqt: Looks good to me, approved
diff --git a/scripts/archivebot.py b/scripts/archivebot.py index e664952..baefcef 100755 --- a/scripts/archivebot.py +++ b/scripts/archivebot.py @@ -168,9 +168,9 @@ Translates a duration written in the shorthand notation (ex. "24h", "7d") into an expression in the local language of the wiki ("24 hours", "7 days"). """ - template = site.mediawiki_message(MW_KEYS[string[-1]]) + key, duration = checkstr(string) + template = site.mediawiki_message(MW_KEYS[key]) if template: - duration = string[:-1] # replace plural variants exp = i18n.translate(site.code, template, {'$1': int(duration)}) return exp.replace('$1', to_local_digits(duration, site.code)) @@ -195,14 +195,7 @@ @return: the corresponding timedelta object @rtype: datetime.timedelta """ - key = string[-1] - if string.isdigit(): - key = 's' - duration = string - issue_deprecation_warning('Time period without qualifier', - string + key, 1, UserWarning) - else: - duration = string[:-1] + key, duration = checkstr(string)
if duration.isdigit(): duration = int(duration) @@ -226,6 +219,31 @@ return datetime.timedelta(days=days)
+def checkstr(string): + """ + Return the key and duration extracted from the string. + + @param string: a string defining a time period: + 300s - 300 seconds + 36h - 36 hours + 7d - 7 days + 2w - 2 weeks (14 days) + 1y - 1 year + @type string: str + @return: key and duration extracted form the string + @rtype: (str, str) + """ + key = string[-1] + if string.isdigit(): + key = 's' + duration = string + issue_deprecation_warning('Time period without qualifier', + string + key, 1, UserWarning) + else: + duration = string[:-1] + return key, duration + + def str2size(string): """ Return a size for a shorthand size. diff --git a/tests/archivebot_tests.py b/tests/archivebot_tests.py index 6f56259..dc0e890 100644 --- a/tests/archivebot_tests.py +++ b/tests/archivebot_tests.py @@ -97,6 +97,15 @@ self.assertRaises(archivebot.MalformedConfigError, archivebot.str2time, '4000@') self.assertRaises(archivebot.MalformedConfigError, archivebot.str2time, '$1')
+ def test_checkstr(self): + """Test for extracting key and duration from shorthand notation of durations.""" + self.assertEqual(archivebot.checkstr('400s'), ('s', '400')) + self.assertEqual(archivebot.checkstr('3000'), ('s', '3000')) + self.assertEqual(archivebot.checkstr('7d'), ('d', '7')) + self.assertEqual(archivebot.checkstr('3y'), ('y', '3')) + # Should pass, because the key is verified in str2time + self.assertEqual(archivebot.checkstr('4000@'), ('@', '4000')) + def test_str2size(self): """Test for parsing the shorthand notation of sizes.""" self.assertEqual(archivebot.str2size('0'), (0, 'B'))
pywikibot-commits@lists.wikimedia.org