jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/597276 )
Change subject: [FEAT] Users might want to know only the count of their watchlist
......................................................................
[FEAT] Users might want to know only the count of their watchlist
In a case where we have a large list of watchlist items and the terminal
can't scroll to the top due to buffer overflow, users might want to know
just the number of items/pages in their watchlist.
This feature introduces -count and -count:all options to return total number
of pages in the watchlist the bot has access to (for that wiki or all wikis
respectively).
In addition, to avoid a run-time error I encountered that says:
“RuntimeError: dictionary changed size during iteration", I had to make
a copy of the keys of the config usernames via `list(config.usernames)`.
Change-Id: Ie535f12a51f9ebf3b1b2dd11ef4d3bf544c42929
---
M scripts/watchlist.py
1 file changed, 38 insertions(+), 3 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/watchlist.py b/scripts/watchlist.py
index 61131c5..72122b2 100755
--- a/scripts/watchlist.py
+++ b/scripts/watchlist.py
@@ -7,12 +7,16 @@
Syntax:
- python pwb.py watchlist [-all | -new]
+ python pwb.py watchlist [-all | -count | -count:all | -new]
Command line options:
-all Reloads watchlists for all wikis where a watchlist is already
present
+-count Count only the total number of pages on the watchlist of the
+ account the bot has access to
+-count:all Count only the total number of pages on all wikis watchlists
+ that the bot is connected to.
-new Load watchlists for all wikis where accounts is setting in
user-config.py
"""
@@ -39,6 +43,27 @@
return watchlist
+def count_watchlist(site=None):
+ """Count only the total number of page(s) in watchlist for this wiki."""
+ if site is None:
+ site = pywikibot.Site()
+ watchlist_count = len(refresh(site))
+ pywikibot.output('There are {} page(s) in the watchlist.'
+ .format(watchlist_count))
+
+
+def count_watchlist_all():
+ """Count only the total number of page(s) in watchlist for all wikis."""
+ wl_count_all = 0
+ pywikibot.output('Counting pages in watchlists of all wikis...')
+ for family in config.usernames:
+ for lang in config.usernames[family]:
+ site = pywikibot.Site(lang, family)
+ wl_count_all += len(refresh(site))
+ pywikibot.output('There are a total of {} page(s) in the watchlists'
+ 'for all wikis.'.format(wl_count_all))
+
+
def isWatched(pageName, site=None):
"""Check whether a page is being watched."""
watchlist = get(site)
@@ -87,19 +112,29 @@
"""
opt_all = False
opt_new = False
+ opt_count = False
+ opt_count_all = False
for arg in pywikibot.handle_args(args):
if arg in ('-all', '-update'):
opt_all = True
elif arg == '-new':
opt_new = True
+ elif arg == '-count':
+ opt_count = True
+ elif arg == '-count:all':
+ opt_count_all = True
if opt_all:
refresh_all()
elif opt_new:
refresh_new()
+ elif opt_count:
+ count_watchlist()
+ elif opt_count_all:
+ count_watchlist_all()
else:
site = pywikibot.Site()
- watchlist = refresh(site)
- pywikibot.output('{} pages in the watchlist.'.format(len(watchlist)))
+ count_watchlist(site)
+ watchlist = list(site.watched_pages(force=True))
for page in watchlist:
try:
pywikibot.stdout(page.title())
--
To view, visit https://gerrit.wikimedia.org/r/597276
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: Ie535f12a51f9ebf3b1b2dd11ef4d3bf544c42929
Gerrit-Change-Number: 597276
Gerrit-PatchSet: 13
Gerrit-Owner: D3r1ck01 <xsavitar.wiki(a)aol.com>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki(a)aol.com>
Gerrit-Reviewer: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: Dvorapa <dvorapa(a)seznam.cz>
Gerrit-Reviewer: Framawiki <framawiki(a)tools.wmflabs.org>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot (75)
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/597313 )
Change subject: [bugfix] Do not change usernames when creating a Site
......................................................................
[bugfix] Do not change usernames when creating a Site
usernames is a defaultdict which inserts a new item if
it does not already exists. Therefore
code_to_user = config.usernames['*'].copy()
inserts an empty dict with '*' if it not already exists
but this changes the usernames and lead scripts to fail.
Bug: T253127
Change-Id: I6740968adec0e67c1a3e0e1a24603282a2986a8e
---
M pywikibot/__init__.py
1 file changed, 3 insertions(+), 1 deletion(-)
Approvals:
D3r1ck01: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 80b0122..dd2b6ad 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -1243,7 +1243,9 @@
# config.usernames is initialised with a defaultdict for each family name
family_name = str(fam)
- code_to_user = config.usernames['*'].copy()
+ code_to_user = {}
+ if '*' in config.usernames: # T253127: usernames is a defaultdict
+ code_to_user = config.usernames['*'].copy()
code_to_user.update(config.usernames[family_name])
user = user or code_to_user.get(code) or code_to_user.get('*')
--
To view, visit https://gerrit.wikimedia.org/r/597313
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: I6740968adec0e67c1a3e0e1a24603282a2986a8e
Gerrit-Change-Number: 597313
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki(a)aol.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot (75)