[Pywikibot-commits] [Gerrit] ...core[master]: [doc] Fix Sphinx directive

2024-04-03 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1016782?usp=email )

Change subject: [doc] Fix Sphinx directive
..

[doc] Fix Sphinx directive

Change-Id: I6829569e29abf12fcc4dbb7278a2f131bcc4f17e
Signed-off-by: Xqt 
---
M pywikibot/specialbots/_upload.py
1 file changed, 11 insertions(+), 1 deletion(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/pywikibot/specialbots/_upload.py b/pywikibot/specialbots/_upload.py
index e34f601..9c217db 100644
--- a/pywikibot/specialbots/_upload.py
+++ b/pywikibot/specialbots/_upload.py
@@ -474,7 +474,7 @@
 def run(self):
 """Run bot.

-.. versionchanged: 9.1
+.. versionchanged:: 9.1
count uploads.
 """
 if self.skip_run():

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1016782?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: I6829569e29abf12fcc4dbb7278a2f131bcc4f17e
Gerrit-Change-Number: 1016782
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt 
Gerrit-Reviewer: Xqt 
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
___
Pywikibot-commits mailing list -- pywikibot-commits@lists.wikimedia.org
To unsubscribe send an email to pywikibot-commits-le...@lists.wikimedia.org


[Pywikibot-commits] [Gerrit] ...core[master]: [IMPR} Add a post_processing attribute to UploadRobot

2024-04-03 Thread jenkins-bot (Code Review)
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 @@
 
 """
 #
-# (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
-

[Pywikibot-commits] [Gerrit] ...core[master]: [doc] Add admoniton and hint for pywikibot.handle_args() usage

2024-04-03 Thread jenkins-bot (Code Review)
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` wrapper instead. In
+   directory mode::
+
+   python pwb.py   
+
+   With installed site package::
+
+   pwb   
+
+   .. note:: the :mod:`pwb` 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 
Gerrit-Reviewer: Xqt 
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
___
Pywikibot-commits mailing list -- pywikibot-commits@lists.wikimedia.org
To unsubscribe send an email to pywikibot-commits-le...@lists.wikimedia.org