jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/458831 )
Change subject: [IMPR] Improvements for makecat.py ......................................................................
[IMPR] Improvements for makecat.py
- use "tocheck" DequeGenerator as MakeCatBot attribute - implement "summary" as as MakeCatBot attribute and change methods arguments accordingly - implement "generator" as as MakeCatBot attribute - implement init_page() previously done in asktoadd() to add redirect targets to check list - implement skip_page() to skip not existent page previously skipped in last main for statement - rename asktoadd to treat_page and assign current_page to pl - let the bot run by in behaviour
7th step detached from Id7ca3461d for easier reviewing
Bug: T192706 Change-Id: Ib3249fd2b6b19c974e0ff3be1e0ed960396612f7 --- M scripts/makecat.py 1 file changed, 45 insertions(+), 36 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/makecat.py b/scripts/makecat.py index 7f8be08..b447193 100755 --- a/scripts/makecat.py +++ b/scripts/makecat.py @@ -75,6 +75,7 @@ 'forward': False, 'keepparent': False, 'nodate': False, + 'summary': None, }) super().__init__(**kwargs) self.skipdates = self.opt.nodate @@ -82,6 +83,7 @@ self.checkbroken = not (self.opt.forward and self.opt.exist) self.removeparent = not self.opt.keepparent self.main = not self.opt.all + self.tocheck = DequeGenerator()
self.workingcatname = self.opt.catnames self._setup_menubar() @@ -98,6 +100,11 @@ cls.option_bar = {'e': extended, 'r': small} cls.treat_options = cls.option_bar['r']
+ @property + def generator(self): + """Generator property used by run().""" + return pagegenerators.DequePreloadingGenerator(self.tocheck) + @staticmethod def highlight_title(page, condition=True): """Highlight a page title if conditon is True.""" @@ -119,19 +126,19 @@ or page in checked or self.skipdates and page.autoFormat()[0] is not None)
- def change_category(self, page, categories, summary): + def change_category(self, page, categories): """Change the category of page.""" global workingcat, parentcats for category in categories: if self.removeparent and category in parentcats: - page.change_category(workingcat, summary=summary) + page.change_category(workingcat, summary=self.opt.summary) return True return False
- def include(self, page, checklinks=True, realinclude=True, linkterm=None, - summary=''): + def include(self, page, checklinks=True, realinclude=True, linkterm=None): """Include the current page to the working category.""" global workingcat, parentcats + global checked actualworkingcat = workingcat if linkterm: actualworkingcat.sortKey = linkterm @@ -141,18 +148,18 @@ else: cats = list(page.categories()) if workingcat not in cats \ - and not self.change_category(page, cats, summary): + and not self.change_category(page, cats): newtext = textlib.replaceCategoryLinks( page.text, cats + [actualworkingcat], site=page.site) - page.put(newtext, summary=summary) + page.put(newtext, summary=self.opt.summary)
if checklinks: self.checklinks(page)
def checklinks(self, page): """Check whether the page has to be added to the tocheck deque.""" - global checked, tocheck + global checked pywikibot.output('\nChecking links for "{}"...' .format(page.title()), newline=False) generators = [page.linkedPages()] @@ -161,26 +168,32 @@ for i, linked_page in enumerate(chain(*generators)): self.print_dot(not i % 25) if self.needcheck(linked_page): - tocheck.append(linked_page) + self.tocheck.append(linked_page) checked.add(linked_page)
+ def init_page(self, page): + """Add redirect targets to check list.""" + global checked + super(MakeCatBot, self).init_page(page) + if page.isRedirectPage(): + newpage = page.getRedirectTarget() + if self.needcheck(newpage): + self.tocheck.append(newpage) + checked.add(newpage) + def skip_page(self, page): """Check whether the page is to be skipped.""" - pass + if not self.checkbroken and not page.exists(): + pywikibot.warning('Page {page} does not exist on {page.site}. ' + 'Skipping.'.format(page=page)) + return True + return super(MakeCatBot, self).skip_page(page)
- def asktoadd(self, pl, summary): + def treat_page(self): """Work on current page and ask to add article to category.""" - global checked, tocheck + global checked global excludefile - mysite = self.site - if pl.site != mysite: - return - if pl.isRedirectPage(): - pl2 = pl.getRedirectTarget() - if self.needcheck(pl2): - tocheck.append(pl2) - checked.add(pl2) - return + pl = self.current_page ctoshow = 500 pywikibot.output('') pywikibot.output('== {} =='.format(pl.title())) @@ -189,7 +202,7 @@ 'Add to category {}?'.format(self.workingcatname), self.treat_options, default='i') if answer == 'y': - self.include(pl, summary=summary) + self.include(pl) break if answer == 'c': self.include(pl, realinclude=False) @@ -198,9 +211,9 @@ if pl.exists() and not pl.isRedirectPage(): linkterm = pywikibot.input( 'In what manner should it be alphabetized?') - self.include(pl, linkterm=linkterm, summary=summary) + self.include(pl, linkterm=linkterm) break - self.include(pl, summary=summary) + self.include(pl) break elif answer == 'n': excludefile.write('%s\n' % pl.title()) @@ -228,7 +241,7 @@ pagetitle = pywikibot.input('Specify page to add:') page = pywikibot.Page(pywikibot.Site(), pagetitle) if page not in checked: - self.include(page, summary=summary) + self.include(page) elif answer == 's': if not pl.exists(): pywikibot.output('Page does not exist; not added.') @@ -237,16 +250,17 @@ 'Redirect page. Will be included normally.') self.include(pl, realinclude=False) else: - self.include(pl, checklinks=False, summary=summary) + self.include(pl, checklinks=False) break elif answer == 'l': - length = len(tocheck) + length = len(self.tocheck) pywikibot.output('Number of pages still to check: {}' .format(length)) if length: pywikibot.output('Pages to be checked:') pywikibot.output( - fill(' - '.join(page.title() for page in tocheck))) + fill(' - '.join(page.title() + for page in self.tocheck))) self.highlight_title(page) elif answer == 'm': self.highlight_title(pl, ctoshow > 500) @@ -267,11 +281,10 @@ @type args: str """ global workingcat, parentcats - global checked, tocheck + global checked global excludefile
checked = set() - tocheck = DequeGenerator()
workingcatname = ''
@@ -296,7 +309,7 @@ summary = i18n.twtranslate(mysite, 'makecat-create', {'cat': workingcatname})
- bot = MakeCatBot(site=mysite, **options) + bot = MakeCatBot(site=mysite, summary=summary, **options)
workingcat = pywikibot.Category(mysite, '{0}{1}' .format(mysite.namespaces.CATEGORY, @@ -352,13 +365,9 @@
for pl in articles: checked.add(pl) - bot.include(pl, summary=summary) + bot.include(pl)
- gen = pagegenerators.DequePreloadingGenerator(tocheck) - - for page in gen: - if bot.checkbroken or page.exists(): - bot.asktoadd(page, summary) + bot.run()
if __name__ == '__main__':