jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/458506 )
Change subject: [IMPR] Improvements for makecat.py
......................................................................
[IMPR] Improvements for makecat.py
- declare needcheck, include and asktoadd as classmethods
- global variables are kept and the script is mainly unchanged
- add needed doc string and enable D103 PEP checking
1st step detached from Id7ca3461d for easier reviewing
Change-Id:…
[View More] I27d7da12c97d98ba5cd72e192ef3bbce968d1f63
---
M scripts/makecat.py
M tox.ini
2 files changed, 143 insertions(+), 127 deletions(-)
Approvals:
Dalba: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/makecat.py b/scripts/makecat.py
index 25331a9..fd73b7c 100755
--- a/scripts/makecat.py
+++ b/scripts/makecat.py
@@ -48,140 +48,157 @@
import pywikibot
+from pywikibot.bot import NoRedirectPageBot, SingleSiteBot
from pywikibot import pagegenerators, i18n, textlib
from pywikibot.tools import DequeGenerator
-def needcheck(pl):
- if main:
- if pl.namespace() != 0:
- return False
- if pl in checked:
- return False
- if skipdates:
- if pl.autoFormat()[0] is not None:
- return False
- return True
+class MakeCatBot(SingleSiteBot, NoRedirectPageBot):
+ """Bot tries to find new articles for a given category."""
-def include(pl, checklinks=True, realinclude=True, linkterm=None, summary=''):
- cl = checklinks
- if linkterm:
- actualworkingcat = pywikibot.Category(mysite, workingcat.title(),
- sort_key=linkterm)
- else:
- actualworkingcat = workingcat
- if realinclude:
- try:
- text = pl.get()
- except pywikibot.NoPage:
- pass
- except pywikibot.IsRedirectPage:
- cl = True
+ @classmethod
+ def needcheck(cls, pl):
+ """Verify whether the current page may be processed."""
+ if main:
+ if pl.namespace() != 0:
+ return False
+ if pl in checked:
+ return False
+ if skipdates:
+ if pl.autoFormat()[0] is not None:
+ return False
+ return True
+
+ def change_category(self, page, catlist):
+ """Change the category of page."""
+ pass
+
+ @classmethod
+ def include(cls, pl, checklinks=True, realinclude=True, linkterm=None,
+ summary=''):
+ """Include the current page to the working category."""
+ cl = checklinks
+ if linkterm:
+ actualworkingcat = pywikibot.Category(mysite, workingcat.title(),
+ sort_key=linkterm)
else:
- cats = [x for x in pl.categories()]
- if workingcat not in cats:
- cats = [x for x in pl.categories()]
- for c in cats:
- if c in parentcats:
- if removeparent:
- pl.change_category(actualworkingcat,
- summary=summary)
- break
- else:
- pl.put(textlib.replaceCategoryLinks(
- text, cats + [actualworkingcat], site=pl.site),
- summary=summary)
- if cl:
- if checkforward:
- for page2 in pl.linkedPages():
- if needcheck(page2):
- tocheck.append(page2)
- checked[page2] = page2
- if checkbackward:
- for ref_page in pl.getReferences():
- if needcheck(ref_page):
- tocheck.append(ref_page)
- checked[ref_page] = ref_page
-
-
-def asktoadd(pl, summary):
- if pl.site != mysite:
- return
- if pl.isRedirectPage():
- pl2 = pl.getRedirectTarget()
- if needcheck(pl2):
- tocheck.append(pl2)
- checked[pl2] = pl2
- return
- ctoshow = 500
- pywikibot.output('')
- pywikibot.output('== {} =='.format(pl.title()))
- while True:
- # TODO: Use pywikibot.inputChoice?
- # (needs the support for 'other options')
- answer = pywikibot.input('[y]es/[n]o/[i]gnore/[o]ther options?')
- if answer == 'y':
- include(pl, summary=summary)
- break
- if answer == 'c':
- include(pl, realinclude=False)
- break
- if answer == 'z':
- if pl.exists():
- if not pl.isRedirectPage():
- linkterm = pywikibot.input(
- 'In what manner should it be alphabetized?')
- include(pl, linkterm=linkterm, summary=summary)
- break
- include(pl, summary=summary)
- break
- elif answer == 'n':
- excludefile.write('%s\n' % pl.title())
- break
- elif answer == 'i':
- break
- elif answer == 'o':
- pywikibot.output('t: Give the beginning of the text of the page')
- pywikibot.output(
- 'z: Add under another title (as [[Category|Title]])')
- pywikibot.output(
- 'x: Add the page, but do not check links to and from it')
- pywikibot.output('c: Do not add the page, but do check links')
- pywikibot.output('a: Add another page')
- pywikibot.output('l: Give a list of the pages to check')
- elif answer == 'a':
- pagetitle = pywikibot.input('Specify page to add:')
- page = pywikibot.Page(pywikibot.Site(), pagetitle)
- if page not in checked.keys():
- include(page, summary=summary)
- elif answer == 'x':
- if pl.exists():
- if pl.isRedirectPage():
- pywikibot.output(
- 'Redirect page. Will be included normally.')
- include(pl, realinclude=False)
- else:
- include(pl, checklinks=False, summary=summary)
- else:
- pywikibot.output('Page does not exist; not added.')
- break
- elif answer == 'l':
- pywikibot.output('Number of pages still to check: {}'
- .format(len(tocheck)))
- pywikibot.output('Pages to be checked:')
- pywikibot.output(' - '.join(page.title() for page in tocheck))
- pywikibot.output('== {} =='.format(pl.title()))
- elif answer == 't':
- pywikibot.output('== {} =='.format(pl.title()))
+ actualworkingcat = workingcat
+ if realinclude:
try:
- pywikibot.output('' + pl.get(get_redirect=True)[0:ctoshow])
+ text = pl.get()
except pywikibot.NoPage:
- pywikibot.output('Page does not exist.')
- ctoshow += 500
- else:
- pywikibot.output('Not understood.')
+ pass
+ except pywikibot.IsRedirectPage:
+ cl = True
+ else:
+ cats = [x for x in pl.categories()]
+ if workingcat not in cats:
+ cats = [x for x in pl.categories()]
+ for c in cats:
+ if c in parentcats:
+ if removeparent:
+ pl.change_category(actualworkingcat,
+ summary=summary)
+ break
+ else:
+ pl.put(textlib.replaceCategoryLinks(
+ text, cats + [actualworkingcat], site=pl.site),
+ summary=summary)
+ if cl:
+ if checkforward:
+ for page2 in pl.linkedPages():
+ if cls.needcheck(page2):
+ tocheck.append(page2)
+ checked[page2] = page2
+ if checkbackward:
+ for ref_page in pl.getReferences():
+ if cls.needcheck(ref_page):
+ tocheck.append(ref_page)
+ checked[ref_page] = ref_page
+
+ def skip_page(self, page):
+ """Check whether the page is to be skipped."""
+ pass
+
+ @classmethod
+ def asktoadd(cls, pl, summary):
+ """Work on current page and ask to add article to category."""
+ if pl.site != mysite:
+ return
+ if pl.isRedirectPage():
+ pl2 = pl.getRedirectTarget()
+ if cls.needcheck(pl2):
+ tocheck.append(pl2)
+ checked[pl2] = pl2
+ return
+ ctoshow = 500
+ pywikibot.output('')
+ pywikibot.output('== {} =='.format(pl.title()))
+ while True:
+ answer = pywikibot.input('[y]es/[n]o/[i]gnore/[o]ther options?')
+ if answer == 'y':
+ cls.include(pl, summary=summary)
+ break
+ if answer == 'c':
+ cls.include(pl, realinclude=False)
+ break
+ if answer == 'z':
+ if pl.exists():
+ if not pl.isRedirectPage():
+ linkterm = pywikibot.input(
+ 'In what manner should it be alphabetized?')
+ cls.include(pl, linkterm=linkterm, summary=summary)
+ break
+ cls.include(pl, summary=summary)
+ break
+ elif answer == 'n':
+ excludefile.write('%s\n' % pl.title())
+ break
+ elif answer == 'i':
+ break
+ elif answer == 'o':
+ pywikibot.output(
+ 't: Give the beginning of the text of the page')
+ pywikibot.output(
+ 'z: Add under another title (as [[Category|Title]])')
+ pywikibot.output(
+ 'x: Add the page, but do not check links to and from it')
+ pywikibot.output('c: Do not add the page, but do check links')
+ pywikibot.output('a: Add another page')
+ pywikibot.output('l: Give a list of the pages to check')
+ elif answer == 'a':
+ pagetitle = pywikibot.input('Specify page to add:')
+ page = pywikibot.Page(pywikibot.Site(), pagetitle)
+ if page not in checked.keys():
+ cls.include(page, summary=summary)
+ elif answer == 'x':
+ if pl.exists():
+ if pl.isRedirectPage():
+ pywikibot.output(
+ 'Redirect page. Will be included normally.')
+ cls.include(pl, realinclude=False)
+ else:
+ cls.include(pl, checklinks=False, summary=summary)
+ else:
+ pywikibot.output('Page does not exist; not added.')
+ break
+ elif answer == 'l':
+ pywikibot.output('Number of pages still to check: {}'
+ .format(len(tocheck)))
+ pywikibot.output('Pages to be checked:')
+ pywikibot.output(' - '.join(page.title() for page in tocheck))
+ pywikibot.output('== {} =='.format(pl.title()))
+ elif answer == 't':
+ pywikibot.output('== {} =='.format(pl.title()))
+ try:
+ pywikibot.output('' + pl.get(get_redirect=True)[0:ctoshow])
+ except pywikibot.NoPage:
+ pywikibot.output('Page does not exist.')
+ ctoshow += 500
+ else:
+ pywikibot.output('Not understood.')
try:
@@ -271,13 +288,13 @@
for pl in articles:
checked[pl] = pl
- include(pl, summary=summary)
+ MakeCatBot.include(pl, summary=summary)
gen = pagegenerators.DequePreloadingGenerator(tocheck)
for page in gen:
if checkbroken or page.exists():
- asktoadd(page, summary)
+ MakeCatBot.asktoadd(page, summary)
finally:
try:
diff --git a/tox.ini b/tox.ini
index ead11e1..7847107 100644
--- a/tox.ini
+++ b/tox.ini
@@ -227,7 +227,6 @@
scripts/interwiki.py : N803, N806, N802
scripts/isbn.py : N803, N806, N802
scripts/maintenance/* : T001
- scripts/makecat.py : D103
scripts/match_images.py : N803, N806
scripts/misspelling.py : N803, N806, N802
scripts/movepages.py : N803, N806, N802
--
To view, visit https://gerrit.wikimedia.org/r/458506
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I27d7da12c97d98ba5cd72e192ef3bbce968d1f63
Gerrit-Change-Number: 458506
Gerrit-PatchSet: 5
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: jenkins-bot (75)
[View Less]
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/458491 )
Change subject: [cleanup] cleanup script code
......................................................................
[cleanup] cleanup script code
- remove preleading "u" from strings
- use single quotes for string constants
- use str.format() instead of modulo operator for changed lines
- keep lines beneath 80 chars
Detached from Icd4c6a5625
Change-Id: …
[View More]I39f9d4d15d5bc0bf7e84575337826a2ef801832f
---
M scripts/makecat.py
1 file changed, 27 insertions(+), 27 deletions(-)
Approvals:
Dalba: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/makecat.py b/scripts/makecat.py
index 743a67b..25331a9 100755
--- a/scripts/makecat.py
+++ b/scripts/makecat.py
@@ -116,12 +116,12 @@
checked[pl2] = pl2
return
ctoshow = 500
- pywikibot.output(u'')
- pywikibot.output(u"==%s==" % pl.title())
+ pywikibot.output('')
+ pywikibot.output('== {} =='.format(pl.title()))
while True:
# TODO: Use pywikibot.inputChoice?
# (needs the support for 'other options')
- answer = pywikibot.input("[y]es/[n]o/[i]gnore/[o]ther options?")
+ answer = pywikibot.input('[y]es/[n]o/[i]gnore/[o]ther options?')
if answer == 'y':
include(pl, summary=summary)
break
@@ -132,7 +132,7 @@
if pl.exists():
if not pl.isRedirectPage():
linkterm = pywikibot.input(
- u"In what manner should it be alphabetized?")
+ 'In what manner should it be alphabetized?')
include(pl, linkterm=linkterm, summary=summary)
break
include(pl, summary=summary)
@@ -143,16 +143,16 @@
elif answer == 'i':
break
elif answer == 'o':
- pywikibot.output(u"t: Give the beginning of the text of the page")
+ pywikibot.output('t: Give the beginning of the text of the page')
pywikibot.output(
- u"z: Add under another title (as [[Category|Title]])")
+ 'z: Add under another title (as [[Category|Title]])')
pywikibot.output(
- u"x: Add the page, but do not check links to and from it")
- pywikibot.output(u"c: Do not add the page, but do check links")
- pywikibot.output(u"a: Add another page")
- pywikibot.output(u"l: Give a list of the pages to check")
+ 'x: Add the page, but do not check links to and from it')
+ pywikibot.output('c: Do not add the page, but do check links')
+ pywikibot.output('a: Add another page')
+ pywikibot.output('l: Give a list of the pages to check')
elif answer == 'a':
- pagetitle = pywikibot.input("Specify page to add:")
+ pagetitle = pywikibot.input('Specify page to add:')
page = pywikibot.Page(pywikibot.Site(), pagetitle)
if page not in checked.keys():
include(page, summary=summary)
@@ -160,28 +160,28 @@
if pl.exists():
if pl.isRedirectPage():
pywikibot.output(
- u"Redirect page. Will be included normally.")
+ 'Redirect page. Will be included normally.')
include(pl, realinclude=False)
else:
include(pl, checklinks=False, summary=summary)
else:
- pywikibot.output(u"Page does not exist; not added.")
+ pywikibot.output('Page does not exist; not added.')
break
elif answer == 'l':
- pywikibot.output(u"Number of pages still to check: %s"
- % len(tocheck))
- pywikibot.output(u"Pages to be checked:")
- pywikibot.output(u" - ".join(page.title() for page in tocheck))
- pywikibot.output(u"==%s==" % pl.title())
+ pywikibot.output('Number of pages still to check: {}'
+ .format(len(tocheck)))
+ pywikibot.output('Pages to be checked:')
+ pywikibot.output(' - '.join(page.title() for page in tocheck))
+ pywikibot.output('== {} =='.format(pl.title()))
elif answer == 't':
- pywikibot.output(u"==%s==" % pl.title())
+ pywikibot.output('== {} =='.format(pl.title()))
try:
- pywikibot.output(u'' + pl.get(get_redirect=True)[0:ctoshow])
+ pywikibot.output('' + pl.get(get_redirect=True)[0:ctoshow])
except pywikibot.NoPage:
- pywikibot.output(u"Page does not exist.")
+ pywikibot.output('Page does not exist.')
ctoshow += 500
else:
- pywikibot.output(u"Not understood.")
+ pywikibot.output('Not understood.')
try:
@@ -259,13 +259,13 @@
# in a diferent category.
articles = list(workingcat.articles(content=True))
if not articles:
- pywikibot.output(
- u"Category %s does not exist or is empty. Which page to start with?"
- % workingcatname)
- answer = pywikibot.input(u"(Default is [[%s]]):" % workingcatname)
+ pywikibot.output('Category {} does not exist or is empty. '
+ 'Which page to start with?'
+ .format(workingcatname))
+ answer = pywikibot.input('(Default is [[{}]]):'.format(workingcatname))
if not answer:
answer = workingcatname
- pywikibot.output(u'' + answer)
+ pywikibot.output('' + answer)
pl = pywikibot.Page(mysite, answer)
articles = [pl]
--
To view, visit https://gerrit.wikimedia.org/r/458491
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I39f9d4d15d5bc0bf7e84575337826a2ef801832f
Gerrit-Change-Number: 458491
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: jenkins-bot (75)
[View Less]
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/458359 )
Change subject: page.Link.langlinkUnsafe: Always set _namespace to a Namespace object
......................................................................
page.Link.langlinkUnsafe: Always set _namespace to a Namespace object
APISite.pagelanglinks creates link objects using Link.langlinkUnsafe.
If the links was in the main namespace, Page(link).namespace() was an
int object which caused …
[View More]attribute errors.
Fix the docstring of NamespacesDict.__getattr__.
Bug: T203491
Change-Id: Ic2ab6707a88f2980049b92517cb98713f9e6a14e
---
M pywikibot/page.py
M pywikibot/site.py
2 files changed, 4 insertions(+), 4 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py
index a249933..126fb95 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -5897,14 +5897,14 @@
link._section = None
link._source = source
- link._namespace = 0
-
+ link._namespace = link._site.namespaces[0]
if ':' in title:
ns, t = title.split(':', 1)
ns = link._site.namespaces.lookup_name(ns)
if ns:
link._namespace = ns
title = t
+
if u"#" in title:
t, sec = title.split(u'#', 1)
title, link._section = t.rstrip(), sec.lstrip()
diff --git a/pywikibot/site.py b/pywikibot/site.py
index d48f808..5ac00c9 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -543,8 +543,8 @@
"""
Get the namespace with the given key.
- @param key: namespace key
- @type key: Namespace, int or str
+ @param attr: namespace key
+ @type attr: Namespace, int or str
@rtype: Namespace
"""
# lookup_name access _namespaces
--
To view, visit https://gerrit.wikimedia.org/r/458359
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Ic2ab6707a88f2980049b92517cb98713f9e6a14e
Gerrit-Change-Number: 458359
Gerrit-PatchSet: 6
Gerrit-Owner: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot (75)
[View Less]