Murfel has uploaded a new change for review. https://gerrit.wikimedia.org/r/184209
Change subject: Different filenames and descriptions support for Uploadbot ...................................................................... Different filenames and descriptions support for Uploadbot Parameter url may be a two-dimensional list with path, filename and description for each file. It is not available from command line (yet?). Bug: T86300 Change-Id: I6609af1c9b2119cbd76ff6045a207f84284ebeeb --- M scripts/upload.py M tests/uploadbot_tests.py 2 files changed, 90 insertions(+), 49 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/09/184209/1 diff --git a/scripts/upload.py b/scripts/upload.py index e3daa44..fbc2ed3 100755 --- a/scripts/upload.py +++ b/scripts/upload.py @@ -74,7 +74,9 @@ Constructor. @param url: path to url or local file (deprecated), or list of urls or - paths to local files. + paths to local files, or list of lists of form of [url, filename, + description] where filename and description are either strings or + None. @type url: string (deprecated) or list @param description: Description of file for its page. If multiple files are uploading the same description is used for every file. @@ -195,29 +197,42 @@ return tempname def process_filename(self, file_url=None): - """Return base filename portion of file_url.""" + """Return base filename portion of file_url or file_url[0].""" if not file_url: file_url = self.url - pywikibot.warning("file_url is not given. " - "Set to self.url by default.") + pywikibot.warning( + "file_url is not given. Set to " + "[self.url, None, self.description] by default.") + + return_filename = False + if isinstance(file_url, basestring): + return_filename = True + file_url = [file_url, None] + if self.description: + file_url.append(self.description) + else: + file_url.append(None) # Isolate the pure name - filename = file_url + filename = file_url[0] # 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 filename = os.path.basename(filename) + + if not file_url[1]: + file_url[1] = filename if self.useFilename: - filename = self.useFilename + file_url[1] = self.useFilename if not self.keepFilename: pywikibot.output( - u"The filename on the target wiki will default to: %s" - % filename) + u"Thefile_url[1]on the target wiki will default to: %s" + % file_url[1]) newfn = pywikibot.input( u'Enter a better name, or press enter to accept:') if newfn != "": - filename = newfn + file_url[1] = newfn # FIXME: these 2 belong somewhere else, presumably in family # forbidden characters are handled by pywikibot/page.py forbidden = ':*?/\\' # to be extended @@ -228,13 +243,13 @@ first_check = True while True: if not first_check: - filename = pywikibot.input(u'Enter a better name, ' - 'or press enter to skip:') - if not filename: + file_url[1] = pywikibot.input(u'Enter a better name, ' + 'or press enter to skip:') + if notfile_url[1]: return None first_check = False ext = os.path.splitext(filename)[1].lower().strip('.') - # are any chars in forbidden also in filename? + # are any chars in forbidden also infile_url[1]? invalid = set(forbidden) & set(filename) if invalid: c = "".join(invalid) @@ -247,42 +262,41 @@ % (u' '.join(allowed_formats), ext), default=False, automatic_quit=False): continue - potential_file_page = pywikibot.FilePage(self.targetSite, filename) + potential_file_page = pywikibot.FilePage(self.targetSite, file_url[1]) if potential_file_page.exists(): if potential_file_page.canBeEdited(): if pywikibot.input_yn(u"File with name %s already exists. " "Would you like to change the name? " "(Otherwise file will be overwritten.)" - % filename, default=True, + % file_url[1], default=True, automatic_quit=False): continue else: break else: pywikibot.output(u"File with name %s already exists and " - "cannot be overwritten." % filename) + "cannot be overwritten." % file_url[1]) continue else: try: if potential_file_page.fileIsShared(): pywikibot.output(u"File with name %s already exists in shared " "repository and cannot be overwritten." - % filename) + % file_url[1]) continue except pywikibot.NoPage: break # A proper description for the submission. # Empty descriptions are not accepted. + if not file_url[2]: + file_url[2] = self.description pywikibot.output(u'The suggested description is:\n%s' - % self.description) + % file_url[2]) # Description must be set and verified - if not self.description: - self.verifyDescription = True - - while not self.description or self.verifyDescription: - if not self.description: + while not file_url[2] or self.verifyDescription: + if not file_url[2]: pywikibot.output( u'\03{lightred}It is not possible to upload a file ' 'without a summary/description.\03{default}') @@ -290,20 +304,23 @@ # if no description, default is 'yes' if pywikibot.input_yn( u'Do you want to change this description?', - default=not self.description): + default=not file_url[2]): from pywikibot import editor as editarticle editor = editarticle.TextEditor() try: - newDescription = editor.edit(self.description) + newDescription = editor.edit(file_url[2]) except Exception as e: pywikibot.error(e) continue # if user saved / didn't press Cancel if newDescription: - self.description = newDescription + file_url[2] = newDescription self.verifyDescription = False - return filename + if return_filename: + self.description = file_url[2] + return file_url[1] + return file_url def abort_on_warn(self, warn_code): """Determine if the warning message should cause an abort.""" @@ -325,20 +342,24 @@ self.upload_file(self.url, debug) def upload_file(self, file_url, debug=False): - """Upload the image at file_url to the target wiki. + """Upload the image at file_url or file_url[0] to the target wiki. Return the filename that was used to upload the image. If the upload fails, ask the user whether to try again or not. - If the user chooses not to retry, return null. + If the user chooses not to retry, return None. """ filename = self.process_filename(file_url) if not filename: return None + elif isinstance(filename, basestring): + file_url = [file_url, filename, self.description] + else: + file_url = filename site = self.targetSite - imagepage = pywikibot.FilePage(site, filename) # normalizes filename - imagepage.text = self.description + imagepage = pywikibot.FilePage(site, file_url[1]) # normalizes filename + imagepage.text = file_url[2] pywikibot.output(u'Uploading file to %s via API...' % site) @@ -347,13 +368,13 @@ if self.ignoreWarning is True: apiIgnoreWarnings = True if self.uploadByUrl: - site.upload(imagepage, source_url=file_url, + site.upload(imagepage, source_url=file_url[0], ignore_warnings=apiIgnoreWarnings) else: - if "://" in file_url: - temp = self.read_file_content(file_url) + if "://" in file_url[0]: + temp = self.read_file_content(file_url[0]) else: - temp = file_url + temp = file_url[0] site.upload(imagepage, source_filename=temp, ignore_warnings=apiIgnoreWarnings, chunk_size=self.chunk_size) @@ -387,7 +408,7 @@ else: # No warning, upload complete. pywikibot.output(u"Upload of %s successful." % filename) - return filename # data['filename'] + return file_url[1] # data['filename'] def run(self): """Run bot.""" diff --git a/tests/uploadbot_tests.py b/tests/uploadbot_tests.py index a84667c..ae57295 100644 --- a/tests/uploadbot_tests.py +++ b/tests/uploadbot_tests.py @@ -13,6 +13,7 @@ # import os +import time from scripts import upload from tests import _images_dir @@ -28,38 +29,57 @@ family = 'wikipedia' code = 'test' - def test_png_list(self): - """Test uploading a list of pngs using upload.py.""" + def test_png_desc_list(self): + """Test uploading a descriptive list of pngs using upload.py.""" image_list = [] for directory_info in os.walk(_images_dir): for dir_file in directory_info[2]: image_list.append(os.path.join(directory_info[0], dir_file)) - bot = upload.UploadRobot(url=image_list, - description="pywikibot upload.py script test", + desc_list = [] + for image in image_list: + desc_list.append([]) + desc_list[-1] = [image, make_unique_filename(self.get_site(), image), None] + bot = upload.UploadRobot(url=desc_list, + description="pywikibot upload.py script test. " + "Tested uploading a descriptive list of pngs.", useFilename=None, keepFilename=True, - verifyDescription=True, aborts=set(), + verifyDescription=False, aborts=set(), ignoreWarning=True, targetSite=self.get_site()) bot.run() def test_png(self): """Test uploading a png using upload.py.""" - bot = upload.UploadRobot(url=[os.path.join(_images_dir, "MP_sounds.png")], - description="pywikibot upload.py script test", - useFilename=None, keepFilename=True, - verifyDescription=True, aborts=set(), + path = os.path.join(_images_dir, "MP_sounds.png") + filename = make_unique_filename(self.get_site(), path) + bot = upload.UploadRobot(url=[path], + description="pywikibot upload.py script test. " + "Tested uploading a png.", + useFilename=filename, keepFilename=True, + verifyDescription=False, aborts=set(), ignoreWarning=True, targetSite=self.get_site()) bot.run() def test_png_url(self): """Test uploading a png from url using upload.py.""" - bot = upload.UploadRobot(url=['https://upload.wikimedia.org/wikipedia/commons/f/fc/MP_sounds.png'], - description="pywikibot upload.py script test", - useFilename=None, keepFilename=True, - verifyDescription=True, aborts=set(), + path = 'https://upload.wikimedia.org/wikipedia/commons/f/fc/MP_sounds.png' + filename = make_unique_filename(self.get_site(), path) + bot = upload.UploadRobot(url=[path], + description="pywikibot upload.py script test. " + "Tested uploading a png from url.", + useFilename=filename, keepFilename=True, + verifyDescription=False, aborts=set(), ignoreWarning=True, targetSite=self.get_site()) bot.run() +def make_unique_filename(site, path): + """Make unique filename to run test without human attention required.""" + base = os.path.basename(path) + file_path = os.path.splitext(base)[0] + file_extension = os.path.splitext(base)[1] + filename = file_path + ' ' + str(time.time()) + file_extension + return filename + if __name__ == '__main__': try: unittest.main() -- To view, visit https://gerrit.wikimedia.org/r/184209 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I6609af1c9b2119cbd76ff6045a207f84284ebeeb Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: Murfel <murna...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits