def str2localized_duration(site, string): """ Localise a shorthand duration. 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"). """ mw_keys = { 'm': 'minutes', 'h': 'hours', 'd': 'days', 'w': 'weeks', 'M': 'months', 'y': 'years', } template = site.mediawiki_message(mw_keys[string[-1]]) 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)) else: return to_local_digits(string, site.code)
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"២៩៩៧៩២៤៥៨")
def test_to_local(self): """Test converting Arabic digits to local digits.""" 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"២៩៩៧៩២៤៥៨")
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"២៩៩៧៩២៤៥៨")
def test_to_local(self): """Test converting Arabic digits to local digits.""" 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"២៩៩៧៩២៤៥៨")
def get_params(self, timestamp, counter: int) -> dict: """Make params for archiving template.""" lang = self.site.lang return { 'counter': to_local_digits(counter, lang), 'year': to_local_digits(timestamp.year, lang), 'isoyear': to_local_digits(timestamp.isocalendar()[0], lang), 'isoweek': to_local_digits(timestamp.isocalendar()[1], lang), 'semester': to_local_digits(int(ceil(timestamp.month / 6)), lang), 'quarter': to_local_digits(int(ceil(timestamp.month / 3)), lang), 'month': to_local_digits(timestamp.month, lang), 'monthname': self.month_num2orig_names[timestamp.month]['long'], 'monthnameshort': self.month_num2orig_names[timestamp.month]['short'], 'week': to_local_digits(int(time.strftime('%W', timestamp.timetuple())), lang), }
def str2localized_duration(site, string: str) -> str: """ Localise a shorthand duration. Translates a duration written in the shorthand notation (ex. "24h", "7d") into an expression in the local wiki language ("24 hours", "7 days"). """ key, duration = checkstr(string) template = site.mediawiki_message(MW_KEYS[key]) if template: # replace plural variants exp = i18n.translate(site.code, template, {'$1': int(duration)}) return exp.replace('$1', to_local_digits(duration, site.code)) return to_local_digits(string, site.code)
def str2localized_duration(site, string): """ Localise a shorthand duration. 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"). """ key, duration = checkstr(string) template = site.mediawiki_message(MW_KEYS[key]) if template: # replace plural variants exp = i18n.translate(site.code, template, {'$1': int(duration)}) return exp.replace('$1', to_local_digits(duration, site.code)) else: return to_local_digits(string, site.code)
def str2localized_duration(site, string): """ Localise a shorthand duration. 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"). """ if string[-1] == 'd': template = site.mediawiki_message('Days') elif string[-1] == 'h': template = site.mediawiki_message('Hours') if template: exp = i18n.translate(site.code, template, int(string[:-1])) return to_local_digits(exp.replace('$1', string[:-1]), site.code) else: return to_local_digits(string, site.code)
def analyze_page(self): """Analyze DiscussionPage.""" max_arch_size = str2size(self.get_attr('maxarchivesize')) arch_counter = int(self.get_attr('counter', '1')) oldthreads = self.page.threads self.page.threads = [] whys = [] pywikibot.output(u'Processing %d threads' % len(oldthreads)) for t in oldthreads: if len(oldthreads) - self.archived_threads \ <= int(self.get_attr('minthreadsleft', 5)): self.page.threads.append(t) continue # Because there's too little threads left. # TODO: Make an option so that unstamped (unsigned) posts get # archived. why = t.should_be_archived(self) if why: archive = self.get_attr('archive') lang = self.site.lang params = { '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), 'semester': to_local_digits(int(ceil(float(t.timestamp.month) / 6)), 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': 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): arch_counter += 1 self.set_attr('counter', str(arch_counter)) whys.append(why) self.archived_threads += 1 else: self.page.threads.append(t) return set(whys)
def analyze_page(self): """Analyze DiscussionPage.""" max_arch_size = str2size(self.get_attr('maxarchivesize')) arch_counter = int(self.get_attr('counter', '1')) oldthreads = self.page.threads self.page.threads = [] whys = [] pywikibot.output(u'Processing %d threads' % len(oldthreads)) for t in oldthreads: if len(oldthreads) - self.archived_threads \ <= int(self.get_attr('minthreadsleft', 5)): self.page.threads.append(t) continue # Because there's too little threads left. # TODO: Make an option so that unstamped (unsigned) posts get # archived. why = t.should_be_archived(self) if why: archive = self.get_attr('archive') lang = self.site.lang params = { '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': 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): arch_counter += 1 self.set_attr('counter', str(arch_counter)) whys.append(why) self.archived_threads += 1 else: self.page.threads.append(t) return set(whys)
def test_to_local(self): self.assertEqual(textlib.to_local_digits(299792458, "en"), 299792458) self.assertEqual(textlib.to_local_digits(299792458, "fa"), "۲۹۹۷۹۲۴۵۸") self.assertEqual(textlib.to_local_digits("299792458 flash", "fa"), "۲۹۹۷۹۲۴۵۸ flash") self.assertEqual(textlib.to_local_digits("299792458", "km"), "២៩៩៧៩២៤៥៨")
def analyze_page(self): """Analyze DiscussionPage.""" max_arch_size = str2size(self.get_attr('maxarchivesize')) arch_counter = int(self.get_attr('counter', '1')) oldthreads = self.page.threads self.page.threads = [] whys = set() pywikibot.output('Processing {} threads'.format(len(oldthreads))) for t in oldthreads: if len(oldthreads) - self.archived_threads \ <= int(self.get_attr('minthreadsleft', 5)): self.page.threads.append(t) continue # Because there's too little threads left. # TODO: Make an option so that unstamped (unsigned) posts get # archived. why = t.should_be_archived(self) if why: archive_pattern = self.get_attr('archive') lang = self.site.lang params = { '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), 'semester': to_local_digits(int(ceil(float(t.timestamp.month) / 6)), 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': to_local_digits( int(time.strftime('%W', t.timestamp.timetuple())), lang), } archive_title = archive_pattern % params archive_page = pywikibot.Page(self.site, archive_title) archive = self.get_archive(archive_page, params) new_params = dict(params) new_params.update({ 'counter': to_local_digits(arch_counter + 1, lang), }) new_archive_title = archive_pattern % new_params counter_matters = (new_archive_title != archive_title) if (counter_matters and arch_counter > 1 and not archive.exists()): # the above may happen because a new year/month etc. # or simply because of the increment new_params.update({ 'counter': to_local_digits(1, lang), }) archive_page = pywikibot.Page(self.site, archive_pattern % new_params) new_archive = self.get_archive(archive_page, new_params) if not new_archive.exists(): # reset counter and update vars arch_counter = 1 archive = new_archive params = new_params if self.feed_archive(archive, t, max_arch_size, params): if counter_matters: arch_counter += 1 whys.add(why) self.archived_threads += 1 else: self.page.threads.append(t) self.set_attr('counter', str(arch_counter)) return whys