jenkins-bot has submitted this change and it was merged.
Change subject: Archivebot to handle non latin counters. ......................................................................
Archivebot to handle non latin counters.
Adding a function to the textlib to handle non-latin digits. And the function is used in archivebot.py
Bug: 69551 Change-Id: I342b1f96b2b77511815665b09e7ce53853a7beb7 --- M pywikibot/textlib.py M scripts/archivebot.py M tests/textlib_tests.py 3 files changed, 50 insertions(+), 8 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py index 4b3934b..837ab39 100644 --- a/pywikibot/textlib.py +++ b/pywikibot/textlib.py @@ -39,14 +39,34 @@ NON_LATIN_DIGITS = { 'ckb': u'٠١٢٣٤٥٦٧٨٩', 'fa': u'۰۱۲۳۴۵۶۷۸۹', + 'km': u'០១២៣៤៥៦៧៨៩', 'kn': u'೦೧೨೩೪೫೬೭೮೯', - 'hi': u'०१२३४५६७८९', 'bn': u'০১২৩৪৫৬৭৮৯', 'gu': u'૦૧૨૩૪૫૬૭૮૯', 'or': u'୦୧୨୩୪୫୬୭୮୯', }
+def to_local_digits(phrase, lang): + """ + Change Latin digits based on language to localized version. + + Be aware that this function only returns for several language + And doesn't touch the input if other languages are asked. + @param phrase: The phrase to convert to localized numerical + @param lang: language code + @return: The localized version + @rtype: unicode + """ + digits = NON_LATIN_DIGITS.get(lang) + if not digits: + return phrase + phrase = u"%s" % phrase + for i in range(10): + phrase = phrase.replace(str(i), digits[i]) + return phrase + + def unescape(s): """Replace escaped HTML-special characters by their originals.""" if '&' not in s: diff --git a/scripts/archivebot.py b/scripts/archivebot.py index 015f924..1961649 100644 --- a/scripts/archivebot.py +++ b/scripts/archivebot.py @@ -105,6 +105,7 @@ import pywikibot from pywikibot import i18n from pywikibot.textlib import TimeStripper +from pywikibot.textlib import to_local_digits
ZERO = datetime.timedelta(0)
@@ -466,16 +467,19 @@ why = t.should_be_archived(self) if why: archive = self.get_attr('archive') + lang = self.site.lang params = { - 'counter': arch_counter, - 'year': t.timestamp.year, - 'isoyear': t.timestamp.isocalendar()[0], - 'isoweek': t.timestamp.isocalendar()[1], - 'quarter': int(ceil(float(t.timestamp.month) / 3)), - 'month': t.timestamp.month, + 'counter': to_local_digits(arch_counter, lang), + 'year': to_local_digits(t.timestamp.year, lang), + 'isoyear': to_local_digits(t.timestamp.isocalendar()[0], lang), + 'isoweek': to_local_digits(t.timestamp.isocalendar()[1], lang), + 'quarter': to_local_digits( + int(ceil(float(t.timestamp.month) / 3)), lang), + 'month': to_local_digits(t.timestamp.month, lang), 'monthname': self.month_num2orig_names[t.timestamp.month]['long'], 'monthnameshort': self.month_num2orig_names[t.timestamp.month]['short'], - 'week': int(time.strftime('%W', t.timestamp.timetuple())), + 'week': to_local_digits( + int(time.strftime('%W', t.timestamp.timetuple())), lang), } archive = pywikibot.Page(self.site, archive % params) if self.feed_archive(archive, t, max_arch_size, params): diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py index 49d5e5f..4292645 100644 --- a/tests/textlib_tests.py +++ b/tests/textlib_tests.py @@ -210,6 +210,24 @@ self.assertRaises(pywikibot.InvalidTitle, textlib.getCategoryLinks, '[[Category:nasty{{{!}}]]', self.site)
+ +class TestLocalDigits(TestCase): + + """Test to verify that local digits are correctly being handled.""" + + net = False + + def test_to_local(self): + self.assertEqual(textlib.to_local_digits(299792458, 'en'), 299792458) + self.assertEqual( + textlib.to_local_digits(299792458, 'fa'), u"۲۹۹۷۹۲۴۵۸") + self.assertEqual( + textlib.to_local_digits( + u"299792458 flash", 'fa'), u"۲۹۹۷۹۲۴۵۸ flash") + self.assertEqual( + textlib.to_local_digits( + "299792458", 'km'), u"២៩៩៧៩២៤៥៨") + if __name__ == '__main__': try: unittest.main()
pywikibot-commits@lists.wikimedia.org