Martineznovo has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/168826

Change subject: upload.py: Allow to ignore warnings
......................................................................

upload.py: Allow to ignore warnings

Added the option -ignorewarn to ignore specific warnings, or all warnings,
using the same logic as the abortonwarn. Useful for batch uploads of files
to replace existing ones.

The UploadRobot already had a boolean ignoreWarning variable in
constructor, which has been extended to allow an array of warning
codes to ignore.

The message shown when a warning is found has been extended to display the
warning code, so the user can know what warning code use to ignore or
abort it.

Change-Id: Idf9e562f5a4fbae8184718c66cedc90042379e81
---
M scripts/upload.py
1 file changed, 28 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/26/168826/1

diff --git a/scripts/upload.py b/scripts/upload.py
index 69127b0..d9c827f 100755
--- a/scripts/upload.py
+++ b/scripts/upload.py
@@ -10,7 +10,9 @@
   -noverify     Do not ask for verification of the upload description if one
                 is given
   -abortonwarn: Abort upload on the specified warning type. If no warning type
-                is specified abort on all warnings.
+                is specified, aborts on any warning.
+  -ignorewarn:  Ignores specified upload warnings. If no warning type is
+                specified, ignores all warnings. Use with caution
   -chunked:     Upload the file in chunks (more overhead, but restartable). If
                 no value is specified the chunk size is 1 MiB. The value must
                 be a number which can be preceded by a suffix. The units are:
@@ -71,7 +73,8 @@
 
         @param ignoreWarning: Set this to True if you want to upload even if
             another file would be overwritten or another mistake would be
-            risked.
+            risked. You can also set it to an array of warning codes to
+            selectively ignore specific warnings.
 
         """
         self.url = url
@@ -240,6 +243,13 @@
         else:
             return warn_code in self.aborts
 
+    def ignore_on_warn(self, warn_code):
+        """Determine if the warning message should be ignored."""
+        if self.ignoreWarning is True:
+            return True
+        else:
+            return warn_code in self.ignoreWarning
+
     def upload_image(self, debug=False):
         """Upload the image at self.url to the target wiki.
 
@@ -257,23 +267,28 @@
         pywikibot.output(u'Uploading file to %s via API....' % site)
 
         try:
+            apiIgnoreWarnings = False
+            if self.ignoreWarning == True:
+                apiIgnoreWarnings = True
             if self.uploadByUrl:
                 site.upload(imagepage, source_url=self.url,
-                            ignore_warnings=self.ignoreWarning)
+                            ignore_warnings=apiIgnoreWarnings)
             else:
                 if "://" in self.url:
                     temp = self.read_file_content()
                 else:
                     temp = self.url
                 site.upload(imagepage, source_filename=temp,
-                            ignore_warnings=self.ignoreWarning,
+                            ignore_warnings=apiIgnoreWarnings,
                             chunk_size=self.chunk_size)
 
         except pywikibot.data.api.UploadWarning as warn:
             pywikibot.output(
-                u'We got a warning message: {0}'.format(warn.message))
+                u'We got a warning message: {0} - {1}'.format(warn.code, 
warn.message))
             if self.abort_on_warn(warn.code):
                 answer = "N"
+            elif self.ignore_on_warn(warn.code):
+                answer = "y"
             else:
                 answer = pywikibot.inputChoice(u"Do you want to ignore?",
                                                ['Yes', 'No'], ['y', 'N'], 'N')
@@ -338,6 +353,7 @@
     useFilename = None
     verifyDescription = True
     aborts = set()
+    ignorewarn = set()
     chunk_size = 0
     chunk_size_regex = r'^-chunked(?::(\d+(?:\.\d+)?)[ \t]*(k|ki|m|mi)?b?)?$'
     chunk_size_regex = re.compile(chunk_size_regex, re.I)
@@ -357,6 +373,11 @@
                     aborts.add(arg[len('-abortonwarn:'):])
                 else:
                     aborts = True
+            elif arg.startswith('-ignorewarn'):
+                if len(arg) > len('-ignorewarn:') and ignorewarn is not True:
+                    ignorewarn.add(arg[len('-ignorewarn:'):])
+                else:
+                    ignorewarn = True
             elif arg.startswith('-chunked'):
                 match = chunk_size_regex.match(arg)
                 if match:
@@ -389,7 +410,8 @@
     bot = UploadRobot(url, description=description, useFilename=useFilename,
                       keepFilename=keepFilename,
                       verifyDescription=verifyDescription,
-                      aborts=aborts, chunk_size=chunk_size)
+                      aborts=aborts, ignoreWarning=ignorewarn,
+                      chunk_size=chunk_size)
     bot.run()
 
 if __name__ == "__main__":

-- 
To view, visit https://gerrit.wikimedia.org/r/168826
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idf9e562f5a4fbae8184718c66cedc90042379e81
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Martineznovo <martinezn...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to