jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/904829 )
Change subject: [IMPR] Improvements for archivebot.py ......................................................................
[IMPR] Improvements for archivebot.py
- add preload_pages() to PageArchiver to avoid deeply nested control flow - add show_md5_key() function to show the MD5 key only
Change-Id: Iaa84ebd3b73b9f513eeae7ce34d71af0f0fef1b7 --- M scripts/archivebot.py 1 file changed, 42 insertions(+), 19 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/archivebot.py b/scripts/archivebot.py index 07f4771..a530410 100755 --- a/scripts/archivebot.py +++ b/scripts/archivebot.py @@ -635,6 +635,16 @@ params['monthnameshort'] = monthnames['short'] return params
+ def preload_pages(self, counter: int, thread, pattern) -> None: + """Preload pages if counter matters.""" + if counter < 25: + return + + for c in range(counter): + params = self.get_params(thread.timestamp, c + 1) + self.get_archive_page(pattern % params, params) + list(self.site.preloadpages(self.archives.values())) + def analyze_page(self) -> Set[ShouldArchive]: """Analyze DiscussionPage.""" max_size = self.get_attr('maxarchivesize') @@ -712,13 +722,7 @@
if counter_matters:
- # preload pages - if counter >= 25: - for c in range(counter): - params = self.get_params(thread.timestamp, c + 1) - self.get_archive_page(pattern % params, params) - list(self.site.preloadpages(self.archives.values())) - + self.preload_pages(counter, thread, pattern) while not counter_found and counter > 1 \ and not archive.exists(): # This may happen when either: @@ -847,6 +851,24 @@ return True
+def show_md5_key(calc, salt, site) -> bool: + """Show calculated MD5 hexdigest.""" + if not calc: + return False + + if not salt: + pywikibot.bot.suggest_help(missing_parameters=['-salt']) + else: + page = pywikibot.Page(site, calc) + if page.exists(): + calc = page.title() + else: + pywikibot.info( + f'NOTE: the specified page "{calc}" does not (yet) exist.') + pywikibot.info(f'key = {calc_md5_hexdigest(calc, salt)}') + return True + + def main(*args: str) -> None: """ Process command line arguments and invoke bot. @@ -902,18 +924,7 @@
site = pywikibot.Site()
- if calc: - if not salt: - pywikibot.bot.suggest_help(missing_parameters=['-salt']) - return - page = pywikibot.Page(site, calc) - if page.exists(): - calc = page.title() - else: - pywikibot.info( - 'NOTE: the specified page "{}" does not (yet) exist.' - .format(calc)) - pywikibot.info(f'key = {calc_md5_hexdigest(calc, salt)}') + if show_md5_key(calc, salt, site): return
if not templates: