jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/667184 )
Change subject: [doctest] Solve doctest for OptionHandler
......................................................................
[doctest] Solve doctest for OptionHandler
Change-Id: I20f1730c957dec7b939a22560c099ba435fbcfee
---
M pywikibot/bot.py
1 file changed, 5 insertions(+), 4 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index 7a966ab..a49ef20 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -1025,10 +1025,11 @@
"""Class to get and set options.
How to use options of OptionHandler and its BaseBot subclasses:
- First define an available_options class attribute to define all
- available options:
+ First define an available_options class attribute for your own
+ option handler to define all available options:
- available_options = {'foo': 'bar', 'bar': 42, 'baz': False}
+ >>> default_options = {'foo': 'bar', 'bar': 42, 'baz': False}
+ >>> class MyHandler(OptionHandler): available_options = default_options
Or you may update the predefined setting in the class initializer.
BaseBot predefines a 'always' options and sets it to False:
@@ -1038,7 +1039,7 @@
Now you can instantiate an OptionHandler or BaseBot class passing
options other than default values:
- >>> bot = OptionHandler(baz=True)
+ >>> bot = MyHandler(baz=True)
You can access bot options either as keyword item or attribute:
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/667184
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I20f1730c957dec7b939a22560c099ba435fbcfee
Gerrit-Change-Number: 667184
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
Xqt has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/666962 )
Change subject: [IMPR] Update unusedfiles.py to add custom templates
......................................................................
[IMPR] Update unusedfiles.py to add custom templates
patch submitted by Dr03ramos
Change-Id: Idab0ec0d906c67ac072c54076e3038d12180693b
Signed-off-by: xqt <info(a)gno.de>
---
M CREDITS
M scripts/unusedfiles.py
2 files changed, 37 insertions(+), 14 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/CREDITS b/CREDITS
index ea6bbdf..ed6da85 100644
--- a/CREDITS
+++ b/CREDITS
@@ -53,6 +53,7 @@
Didicodes
Diwanshu Mittal
Dmytro Dziuma
+Dr03ramos
DrTrigon
Dvorapa
Ebrahim Byagowi
diff --git a/scripts/unusedfiles.py b/scripts/unusedfiles.py
index ac1e36f..b589bb8 100755
--- a/scripts/unusedfiles.py
+++ b/scripts/unusedfiles.py
@@ -6,15 +6,19 @@
-always Don't be asked every time.
-nouserwarning Do not warn uploader about orphaned file.
+-filetemplate: Use a custom template on unused file pages.
+-usertemplate: Use a custom template to warn the uploader.
-limit Specify number of pages to work on with "-limit:n" where
n is the maximum number of articles to work on.
If not used, all pages are used.
"""
#
-# (C) Pywikibot team, 2007-2020
+# (C) Pywikibot team, 2007-2021
#
# Distributed under the terms of the MIT license.
#
+import re
+
import pywikibot
from pywikibot import i18n, pagegenerators
from pywikibot.bot import SingleSiteBot, AutomaticTWSummaryBot, ExistingPageBot
@@ -44,18 +48,31 @@
def __init__(self, **kwargs):
"""Initializer."""
self.available_options.update({
- 'nouserwarning': False # do not warn uploader
+ 'nouserwarning': False, # do not warn uploader
+ 'filetemplate': '',
+ 'usertemplate': '',
})
super().__init__(**kwargs)
- self.template_image = i18n.translate(self.site,
- template_to_the_image)
- self.template_user = i18n.translate(self.site,
- template_to_the_user)
- if not (self.template_image
- and (self.template_user or self.opt.nouserwarning)):
- raise i18n.TranslationError('This script is not localized for {0} '
- 'site.'.format(self.site))
+ # handle the custom templates
+ if not self.opt.filetemplate:
+ self.opt.filetemplate = i18n.translate(self.site,
+ template_to_the_image)
+ elif not re.fullmatch('{{.+}}', self.opt.filetemplate):
+ self.opt.filetemplate = '{{%s}}' % self.opt.filetemplate
+
+ if not self.opt.usertemplate:
+ self.opt.usertemplate = i18n.translate(self.site,
+ template_to_the_user)
+ elif not re.fullmatch('{{.+}}', self.opt.usertemplate):
+ self.opt.usertemplate = '{{%s}}' % self.opt.usertemplate
+
+ if not (self.opt.filetemplate
+ and (self.opt.usertemplate or self.opt.nouserwarning)):
+ # if no templates are given
+ raise i18n.TranslationError(
+ 'This script is not localized for {} site;\n'
+ 'try using -filetemplate:<template name>.'.format(self.site))
def treat(self, image):
"""Process one image page."""
@@ -63,18 +80,19 @@
# rather than a local page with the same name as shared media.
if (image.get_file_url() and not image.file_is_shared()
and 'http://' not in image.text):
- if self.template_image in image.text:
+ if self.opt.filetemplate in image.text:
pywikibot.output('{0} done already'
.format(image.title(as_link=True)))
return
- self.append_text(image, '\n\n' + self.template_image)
+ self.append_text(image, '\n\n' + self.opt.filetemplate)
if self.opt.nouserwarning:
return
uploader = image.get_file_history().pop(0)['user']
user = pywikibot.User(image.site, uploader)
usertalkpage = user.getUserTalkPage()
- template2uploader = self.template_user % {'title': image.title()}
+ template2uploader = self.opt.usertemplate \
+ % {'title': image.title()}
msg2uploader = self.site.expand_text(template2uploader)
if usertalkpage.is_flow_page():
self.post_to_flow_board(usertalkpage, msg2uploader)
@@ -124,11 +142,15 @@
arg, sep, value = arg.partition(':')
if arg == '-limit':
total = value
+ elif arg == '-filetemplate':
+ options['filetemplate'] = value
+ elif arg == '-usertemplate':
+ options['usertemplate'] = value
else:
options[arg[1:]] = True
site = pywikibot.Site()
- gen = pagegenerators.UnusedFilesGenerator(total=total, site=site)
+ gen = site.unusedfiles(total=total)
gen = pagegenerators.PreloadingGenerator(gen)
bot = UnusedFilesBot(site=site, generator=gen, **options)
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/666962
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Idab0ec0d906c67ac072c54076e3038d12180693b
Gerrit-Change-Number: 666962
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki(a)aol.com>
Gerrit-Reviewer: Dr03ramos <dr03ramos(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/666908 )
Change subject: [scripts] Restore unusedfiles.py due to pull request
......................................................................
[scripts] Restore unusedfiles.py due to pull request
https://github.com/wikimedia/pywikibot/pull/36/commits/6ba08da0f83681a1cd98…
Change-Id: Id66681c1e6515bfb5da3a5a2bda15c5644e34744
---
M docs/scripts/scripts.rst
M scripts/README.rst
R scripts/unusedfiles.py
M tests/script_tests.py
4 files changed, 14 insertions(+), 3 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/docs/scripts/scripts.rst b/docs/scripts/scripts.rst
index 0877531..f202aa4 100644
--- a/docs/scripts/scripts.rst
+++ b/docs/scripts/scripts.rst
@@ -155,6 +155,11 @@
.. automodule:: scripts.replace
+scripts.replicate\_wiki script
+------------------------------
+
+.. automodule:: scripts.replicate_wiki
+
scripts.revertbot script
------------------------
@@ -195,6 +200,11 @@
.. automodule:: scripts.transferbot
+scripts.unusedfiles script
+--------------------------
+
+.. automodule:: scripts.unusedfiles
+
scripts.upload script
---------------------
diff --git a/scripts/README.rst b/scripts/README.rst
index 2e46e81..5835fbf 100644
--- a/scripts/README.rst
+++ b/scripts/README.rst
@@ -115,6 +115,9 @@
+------------------------+---------------------------------------------------------+
| transferbot.py | Transfers pages from a source wiki to a target wiki |
+------------------------+---------------------------------------------------------+
+ | unusedfiles.py | Bot appends some text to all unused images and other |
+ | | text to the respective uploaders. |
+ +------------------------+---------------------------------------------------------+
| upload.py | upload an image to a wiki. |
+------------------------+---------------------------------------------------------+
| version.py | Outputs Pywikibot's revision number, Python's version |
@@ -228,9 +231,6 @@
+------------------------+---------------------------------------------------------+
| unlink.py | This bot unlinks a page on every page that links to it. |
+------------------------+---------------------------------------------------------+
- | unusedfiles.py | Bot appends some text to all unused images and other |
- | | text to the respective uploaders. |
- +------------------------+---------------------------------------------------------+
| watchlists.py | Information retrieved by watchlist.py will be stored |
+------------------------+---------------------------------------------------------+
| wikisourcetext.py | This bot applies to Wikisource sites to upload text. |
diff --git a/scripts/archive/unusedfiles.py b/scripts/unusedfiles.py
similarity index 100%
rename from scripts/archive/unusedfiles.py
rename to scripts/unusedfiles.py
diff --git a/tests/script_tests.py b/tests/script_tests.py
index c9ee80f..7968462 100644
--- a/tests/script_tests.py
+++ b/tests/script_tests.py
@@ -95,6 +95,7 @@
'noreferences',
'parser_function_count',
'shell',
+ 'unusedfiles',
'upload',
'welcome',
]
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/666908
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Id66681c1e6515bfb5da3a5a2bda15c5644e34744
Gerrit-Change-Number: 666908
Gerrit-PatchSet: 1
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
Gerrit-MessageType: merged