jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1017109?usp=email )
Change subject: [doc] update UploadRobot documentation
......................................................................
[doc] update UploadRobot documentation
Change-Id: I07a428bf4476214179a5ea5d9981755b1d9e94fb
---
M pywikibot/specialbots/_upload.py
1 file changed, 14 insertions(+), 8 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/specialbots/_upload.py b/pywikibot/specialbots/_upload.py
index 9c217db..128d9bd 100644
--- a/pywikibot/specialbots/_upload.py
+++ b/pywikibot/specialbots/_upload.py
@@ -31,10 +31,10 @@
"""Upload bot."""
post_processor: Callable[[str, str | None], None] | None = None
- """If this attribute is set to a callable, the :meth:`run` method calls it
- after upload. The parameter passed to the callable is the origin *file_url*
- passed to the :meth:`upload` method and the filename returned from that
- method. It can be used like this:
+ """If this attribute is set to a callable, the :meth:`run` method
+ calls it after upload. The parameters passed to the callable is the
+ origin *file_url* passed to the :meth:`upload` method and the
+ *filename* returned from that method. It can be used like this:
.. code:: python
@@ -70,7 +70,6 @@
.. versionchanged:: 6.2
asynchronous upload is used if *asynchronous* parameter is set
-
.. versionchanged:: 6.4
*force_if_shared* parameter was added
@@ -78,7 +77,6 @@
to local files.
:param description: Description of file for its page. If multiple files
are uploading the same description is used for every file.
- :type description: str
:param use_filename: Specify title of the file's page. If multiple
files are uploading it asks to change the name for second, third,
etc. files, otherwise the last file will overwrite the other.
@@ -104,11 +102,10 @@
:param force_if_shared: Upload the file even if it's currently
shared to the target site (e.g. when moving from Commons to another
wiki)
- :keyword always: Disables any input, requires that either
+ :keyword bool always: Disables any input, requires that either
ignore_warning or aborts are set to True and that the
description is also set. It overwrites verify_description to
False and keep_filename to True.
- :type always: bool
"""
super().__init__(**kwargs)
if self.opt.always:
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1017109?usp=email
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: I07a428bf4476214179a5ea5d9981755b1d9e94fb
Gerrit-Change-Number: 1017109
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info(a)gno.de>
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/+/1015990?usp=email )
Change subject: [IMPR} Add a post_processing attribute to UploadRobot
......................................................................
[IMPR} Add a post_processing attribute to UploadRobot
If this attribute is set to a callable, the run method calls it
after upload. The parameter passed to the callable is the origin file_url
passed to the upload method and the filename returned from that method.
- use this feature with imagetransfer and use bot.run() mehtod instead
of bot.upload_file()
Bug: T360837
Change-Id: I4a9c6182a87a601ce770ebb5e84c8898d2a4563d
---
M pywikibot/specialbots/_upload.py
M scripts/imagetransfer.py
M tests/uploadbot_tests.py
3 files changed, 75 insertions(+), 29 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/specialbots/_upload.py b/pywikibot/specialbots/_upload.py
index ce8bd29..e34f601 100644
--- a/pywikibot/specialbots/_upload.py
+++ b/pywikibot/specialbots/_upload.py
@@ -3,7 +3,7 @@
Do not import classes directly from here but from specialbots.
"""
#
-# (C) Pywikibot team, 2003-2023
+# (C) Pywikibot team, 2003-2024
#
# Distributed under the terms of the MIT license.
#
@@ -21,6 +21,7 @@
import pywikibot
import pywikibot.comms.http as http
from pywikibot import config
+from pywikibot.backports import Callable
from pywikibot.bot import BaseBot, QuitKeyboardInterrupt
from pywikibot.exceptions import APIError, FatalServerError, NoPageError
@@ -29,6 +30,27 @@
"""Upload bot."""
+ post_processor: Callable[[str, str | None], None] | None = None
+ """If this attribute is set to a callable, the :meth:`run` method calls it
+ after upload. The parameter passed to the callable is the origin *file_url*
+ passed to the :meth:`upload` method and the filename returned from that
+ method. It can be used like this:
+
+ .. code:: python
+
+ def summarize(old: str, new: str | None) -> None:
+ if new is None:
+ print(f'{old} was ignored')
+ else:
+ print(f'{old} was uploaded as {new}')
+
+ bot = UploadRobot('Myfile.bmp')
+ bot.post_processor = summarize
+ bot.run()
+
+ .. versionadded:: 9.1
+ """
+
def __init__(self, url: list[str] | str, *,
url_encoding=None,
description: str = '',
@@ -464,6 +486,8 @@
self.counter['read'] += 1
if filename:
self.counter['upload'] += 1
+ if callable(self.post_processor):
+ self.post_processor(file_url, filename)
except QuitKeyboardInterrupt:
pywikibot.info(f'\nUser quit {type(self).__name__} bot run...')
except KeyboardInterrupt:
diff --git a/scripts/imagetransfer.py b/scripts/imagetransfer.py
index eb4bf8c..7cf4836 100755
--- a/scripts/imagetransfer.py
+++ b/scripts/imagetransfer.py
@@ -37,7 +37,7 @@
¶ms;
"""
#
-# (C) Pywikibot team, 2004-2022
+# (C) Pywikibot team, 2004-2024
#
# Distributed under the terms of the MIT license.
#
@@ -201,6 +201,34 @@
:return: the filename which was used to upload the image
"""
+ def delete_source(old_filename, target_filename):
+ """Delete source image or tag nowCommons template to it.
+
+ This function is called when upload to Commons was
+ successful.
+ """
+ if not target_filename \
+ or self.opt.target.sitename != 'commons:commons':
+ return
+
+ reason = i18n.twtranslate(sourceSite,
+ 'imagetransfer-nowcommons_notice')
+ # try to delete the original image if we have a sysop account
+ if sourceSite.has_right('delete') \
+ and sourceImagePage.delete(reason):
+ return
+
+ if sourceSite.lang in nowCommonsTemplate \
+ and sourceSite.family.name in config.usernames \
+ and sourceSite.lang in config.usernames[sourceSite.family.name]:
+ # add the nowCommons template.
+ pywikibot.info('Adding nowCommons template to '
+ + sourceImagePage.title())
+ sourceImagePage.put(sourceImagePage.get() + '\n\n'
+ + nowCommonsTemplate[sourceSite.code]
+ % target_filename,
+ summary=reason)
+
sourceSite = sourceImagePage.site
pywikibot.info(
'\n>>> Transfer {source} from {source.site} to {target}\n'
@@ -246,32 +274,8 @@
force_if_shared=self.opt.force_if_shared,
asynchronous=self.opt.asynchronous,
chunk_size=self.opt.chunk_size)
-
- # try to upload
- if bot.skip_run():
- return
- target_filename = bot.upload_file(url)
-
- if target_filename \
- and self.opt.target.sitename == 'commons:commons':
- # upload to Commons was successful
- reason = i18n.twtranslate(sourceSite,
- 'imagetransfer-nowcommons_notice')
- # try to delete the original image if we have a sysop account
- if sourceSite.has_right('delete') \
- and sourceImagePage.delete(reason):
- return
- if sourceSite.lang in nowCommonsTemplate \
- and sourceSite.family.name in config.usernames \
- and sourceSite.lang in \
- config.usernames[sourceSite.family.name]:
- # add the nowCommons template.
- pywikibot.info('Adding nowCommons template to '
- + sourceImagePage.title())
- sourceImagePage.put(sourceImagePage.get() + '\n\n'
- + nowCommonsTemplate[sourceSite.lang]
- % target_filename,
- summary=reason)
+ bot.post_processor = delete_source
+ bot.run()
def show_image_list(self, imagelist) -> None:
"""Print image list."""
diff --git a/tests/uploadbot_tests.py b/tests/uploadbot_tests.py
index 4bc255f..7bffec2 100755
--- a/tests/uploadbot_tests.py
+++ b/tests/uploadbot_tests.py
@@ -5,7 +5,7 @@
These tests write to the wiki.
"""
#
-# (C) Pywikibot team, 2014-2022
+# (C) Pywikibot team, 2014-2024
#
# Distributed under the terms of the MIT license.
#
@@ -83,6 +83,7 @@
self.assertTrue(bot._handle_warning('any warning')) # ignore_warning
self.assertTrue(bot.ignore_on_warn('any warning')) # ignore_warning
self.assertFalse(bot.abort_on_warn('any warning')) # aborts
+ self.assertIsNone(bot.post_processor)
if __name__ == '__main__':
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1015990?usp=email
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: I4a9c6182a87a601ce770ebb5e84c8898d2a4563d
Gerrit-Change-Number: 1015990
Gerrit-PatchSet: 7
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: JJMC89 <JJMC89.Wikimedia(a)gmail.com>
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1016793?usp=email )
Change subject: [doc] Add admoniton and hint for pywikibot.handle_args() usage
......................................................................
[doc] Add admoniton and hint for pywikibot.handle_args() usage
Bug: T359766
Change-Id: Idf3d76406590dd0a39cc08dd47c39d45c6b32c60
---
M pywikibot/bot.py
M pywikibot/scripts/wrapper.py
2 files changed, 35 insertions(+), 3 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index 04606a0..935886d 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -672,6 +672,26 @@
['-myoption']
>>> for arg in local_args: pass # do whatever is wanted with local_args
+ .. caution::
+ Global options might be introduced without warning period. It is
+ up to developers to verify that global options do not interfere
+ with local script options of private scripts.
+
+ .. tip::
+ Avoid using this method in your private scripts and use the
+ :mod:`pwb<pywikibot.scripts.wrapper>` wrapper instead. In
+ directory mode::
+
+ python pwb.py <global options> <name_of_script> <local options>
+
+ With installed site package::
+
+ pwb <global options> <name_of_script> <local options>
+
+ .. note:: the :mod:`pwb<pywikibot.scripts.wrapper>` wrapper can
+ be used even if the `handle_args` method is used within the
+ script.
+
.. versionchanged:: 5.2
*-site* global option was added
.. versionchanged:: 7.1
diff --git a/pywikibot/scripts/wrapper.py b/pywikibot/scripts/wrapper.py
index ba63b8d..32eaa8f 100755
--- a/pywikibot/scripts/wrapper.py
+++ b/pywikibot/scripts/wrapper.py
@@ -30,12 +30,14 @@
python pwb.py -lang:de bot_tests -v
+.. seealso:: :mod:`pwb` entry point
.. versionchanged:: 7.0
- pwb wrapper was added to the Python site package lib
+ pwb wrapper was added to the Python site package lib.
.. versionchanged:: 7.7
- pwb wrapper is able to set ``PYWIKIBOT_TEST_...`` environment variables
+ pwb wrapper is able to set ``PYWIKIBOT_TEST_...`` environment variables,
+ see :ref:`Environment variables`.
.. versionchanged:: 8.0
- renamed to wrapper.py
+ renamed to wrapper.py.
"""
#
# (C) Pywikibot team, 2012-2024
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1016793?usp=email
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: Idf3d76406590dd0a39cc08dd47c39d45c6b32c60
Gerrit-Change-Number: 1016793
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <info(a)gno.de>
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/+/1016331?usp=email )
Change subject: [doc] Update documentation for BasePage.get() and BasePage.text
......................................................................
[doc] Update documentation for BasePage.get() and BasePage.text
The argument of IsRedirectPageError is the page itself since Pywikibot
core release. With compat release it was the redirect target page.
The reason for this change in core release is documented in
rPWBC1585a990e3d1:
"... getting the redirect target requires an additional API request
... Knowing that we have a redirect page without knowing its target
is sometimes enough."
Bug: T361531
Change-Id: I07f9ff7b270d561020fdf72c999df3641e3eda18
---
M pywikibot/page/_basepage.py
1 file changed, 66 insertions(+), 14 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/page/_basepage.py b/pywikibot/page/_basepage.py
index 6af8970..8eaeeb0 100644
--- a/pywikibot/page/_basepage.py
+++ b/pywikibot/page/_basepage.py
@@ -358,19 +358,31 @@
"""Return the wiki-text of the page.
This will retrieve the page from the server if it has not been
- retrieved yet, or if force is True. This can raise the following
- exceptions that should be caught by the calling code:
+ retrieved yet, or if force is True. Exceptions should be caught
+ by the calling code.
- :exception pywikibot.exceptions.NoPageError: The page does not exist
- :exception pywikibot.exceptions.IsRedirectPageError: The page is a
- redirect. The argument of the exception is the title of the page
- it redirects to.
- :exception pywikibot.exceptions.SectionError: The section does not
- exist on a page with a # link
+ **Example:**
- :param force: reload all page attributes, including errors.
- :param get_redirect: return the redirect text, do not follow the
- redirect, do not raise an exception.
+ >>> import pywikibot
+ >>> site = pywikibot.Site('mediawiki')
+ >>> page = pywikibot.Page(site, 'Pywikibot')
+ >>> page.get(get_redirect=True)
+ '#REDIRECT[[Manual:Pywikibot]]'
+ >>> page.get()
+ Traceback (most recent call last):
+ ...
+ pywikibot.exceptions.IsRedirectPageError: ... is a redirect page.
+
+ .. seealso:: :attr:`text` property
+
+ :param force: reload all page attributes, including errors.
+ :param get_redirect: return the redirect text, do not follow the
+ redirect, do not raise an exception.
+
+ :raises NoPageError: The page does not exist.
+ :raises IsRedirectPageError: The page is a redirect.
+ :raises SectionError: The section does not exist on a page with
+ a # link.
"""
if force:
del self.latest_revision_id
@@ -519,8 +531,29 @@
@property
def text(self) -> str:
- """
- Return the current (edited) wikitext, loading it if necessary.
+ """Return the current (edited) wikitext, loading it if necessary.
+
+ This property should be prefered over :meth:`get`. If the page
+ does not exist, an empty string will be returned. For a redirect
+ it returns the redirect page content and does not raise an
+ :exc:`exceptions.IsRedirectPageError` exception.
+
+ **Example:**
+
+ >>> import pywikibot
+ >>> site = pywikibot.Site('mediawiki')
+ >>> page = pywikibot.Page(site, 'Pywikibot')
+ >>> page.text
+ '#REDIRECT[[Manual:Pywikibot]]'
+ >>> page.text = 'PWB Framework'
+ >>> page.text
+ 'PWB Framework'
+ >>> page.text = None # reload from wiki
+ >>> page.text
+ '#REDIRECT[[Manual:Pywikibot]]'
+ >>> del page.text # other way to reload from wiki
+
+ To save the modified text :meth:`save` is one possible method.
:return: text of the page
"""
@@ -534,7 +567,7 @@
return ''
@text.setter
- def text(self, value: str | None):
+ def text(self, value: str | None) -> None:
"""Update the current (edited) wikitext.
:param value: New value or None
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1016331?usp=email
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: I07f9ff7b270d561020fdf72c999df3641e3eda18
Gerrit-Change-Number: 1016331
Gerrit-PatchSet: 8
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: RoySmith <roy(a)panix.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/+/1015981?usp=email )
Change subject: [IMPR] show upload count with UploadRobot
......................................................................
[IMPR] show upload count with UploadRobot
Also update doc strings and replace format strings
Change-Id: Id46329e9f60669087e4ee8673717500acd557289
---
M pywikibot/specialbots/_upload.py
1 file changed, 39 insertions(+), 19 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/specialbots/_upload.py b/pywikibot/specialbots/_upload.py
index 9096957..ce8bd29 100644
--- a/pywikibot/specialbots/_upload.py
+++ b/pywikibot/specialbots/_upload.py
@@ -216,11 +216,13 @@
default=False, automatic_quit=False)
return answer
- def process_filename(self, file_url):
- """Return base filename portion of file_url."""
+ def process_filename(self, file_url: str) -> str | None:
+ """Return base filename portion of *file_url*.
+
+ :param file_url: either a URL or a local file path
+ """
# Isolate the pure name
filename = file_url
- # Filename may be either a URL or a local file path
if '://' in filename:
# extract the path portion of the URL
filename = urlparse(filename).path
@@ -288,10 +290,10 @@
if potential_file_page.has_permission():
if overwrite is None:
overwrite = not pywikibot.input_yn(
- 'File with name {} already exists. '
- 'Would you like to change the name? '
- '(Otherwise file will be overwritten.)'
- .format(filename), default=True,
+ f'File with name {filename} already exists.'
+ ' Would you like to change the name?'
+ ' (Otherwise file will be overwritten.)',
+ default=True,
automatic_quit=False)
if not overwrite:
continue
@@ -361,7 +363,7 @@
"""
return self.ignore_warning is True or warn_code in self.ignore_warning
- def upload_file(self, file_url):
+ def upload_file(self, file_url: str) -> str | None:
"""
Upload the image at file_url to the target wiki.
@@ -374,6 +376,8 @@
.. versionchanged:: 7.0
If 'copyuploadbaddomain' API error occurred in first step,
download the file and upload it afterwards
+
+ :param file_url: either a URL or a local file path
"""
filename = self.process_filename(file_url)
if not filename:
@@ -432,36 +436,41 @@
"""Check whether processing is to be skipped."""
# early check that upload is enabled
if self.target_site.is_uploaddisabled():
- pywikibot.error(
- 'Upload error: Local file uploads are disabled on {}.'
- .format(self.target_site))
+ pywikibot.error(f'Upload error: Local file uploads are disabled '
+ f'on {self.target_site}.')
return True
# early check that user has proper rights to upload
self.target_site.login()
if not self.target_site.has_right('upload'):
- pywikibot.error(
- "User '{}' does not have upload rights on site {}."
- .format(self.target_site.user(), self.target_site))
+ pywikibot.error(f"User '{self.target_site.user()}' does not have "
+ f'upload rights on site {self.target_site}.')
return True
return False
def run(self):
- """Run bot."""
+ """Run bot.
+
+ .. versionchanged: 9.1
+ count uploads.
+ """
if self.skip_run():
return
+
try:
for file_url in self.url:
- self.upload_file(file_url)
+ filename = self.upload_file(file_url)
self.counter['read'] += 1
+ if filename:
+ self.counter['upload'] += 1
except QuitKeyboardInterrupt:
- pywikibot.info(f'\nUser quit {self.__class__.__name__} bot run...')
+ pywikibot.info(f'\nUser quit {type(self).__name__} bot run...')
except KeyboardInterrupt:
if config.verbose_output:
raise
- pywikibot.info('\nKeyboardInterrupt during {} bot run...'
- .format(self.__class__.__name__))
+ pywikibot.info(
+ f'\nKeyboardInterrupt during {type(self).__name__} bot run...')
finally:
self.exit()
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1015981?usp=email
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: Id46329e9f60669087e4ee8673717500acd557289
Gerrit-Change-Number: 1015981
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged