jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/462558 )
Change subject: [cleanup] cleanup scripts/imagecopy_self.py ......................................................................
[cleanup] cleanup scripts/imagecopy_self.py
- remove preleading "u" from strings - use single quotes for string literals and double quotes **only** if they consist of single quotes within them - use str.format(...) instead of modulo for type specifier arguments
Change-Id: I1545c231acc91f82a6d698fa53e19873d7c5be2e --- M scripts/imagecopy_self.py 1 file changed, 210 insertions(+), 209 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/imagecopy_self.py b/scripts/imagecopy_self.py index 254bc55..3c51a4b 100644 --- a/scripts/imagecopy_self.py +++ b/scripts/imagecopy_self.py @@ -79,82 +79,82 @@ NL = ''
nowCommonsTemplate = { - 'de': u'{{NowCommons|%s}}', + 'de': '{{NowCommons|%s}}', 'en': ('{{NowCommons|1=File:%s|date=~~~~~|' 'reviewer={{subst:REVISIONUSER}}}}'), - 'lb': u'{{Elo op Commons|%s}}', - 'nds-nl': u'{{NoenCommons|1=File:%s}}', + 'lb': '{{Elo op Commons|%s}}', + 'nds-nl': '{{NoenCommons|1=File:%s}}', 'shared': ('{{NowCommons|1=File:%s|date=~~~~~|' 'reviewer={{subst:REVISIONUSER}}}}'), }
moveToCommonsTemplate = { 'de': ['NowCommons', 'NC', 'NCT', 'Nowcommons'], - 'en': [u'Commons ok', u'Copy to Wikimedia Commons', u'Move to commons', - u'Movetocommons', u'To commons', - u'Copy to Wikimedia Commons by BotMultichill'], - 'lb': [u'Move to commons'], - 'nds-nl': [u'Noar Commons', u'VNC'], - 'shared': [u'Move'], + 'en': ['Commons ok', 'Copy to Wikimedia Commons', 'Move to commons', + 'Movetocommons', 'To commons', + 'Copy to Wikimedia Commons by BotMultichill'], + 'lb': ['Move to commons'], + 'nds-nl': ['Noar Commons', 'VNC'], + 'shared': ['Move'], }
skipTemplates = { - 'de': [u'Löschprüfung', - u'NoCommons', - u'NowCommons', - u'NowCommons/Mängel', - u'NowCommons-Überprüft', - u'Wappenrecht', + 'de': ['Löschprüfung', + 'NoCommons', + 'NowCommons', + 'NowCommons/Mängel', + 'NowCommons-Überprüft', + 'Wappenrecht', ], - 'en': [u'Db-f1', - u'Db-f2', - u'Db-f3', - u'Db-f7', - u'Db-f8', - u'Db-f9', - u'Db-f10', - u'Do not move to Commons', - u'NowCommons', - u'CommonsNow', - u'Nowcommons', - u'NowCommonsThis', - u'Nowcommons2', - u'NCT', - u'Nowcommonsthis', - u'Moved to commons', - u'Now Commons', - u'Now at commons', - u'Db-nowcommons', - u'WikimediaCommons', - u'Now commons', - u'Di-no source', - u'Di-no license', - u'Di-no permission', - u'Di-orphaned fair use', - u'Di-no source no license', - u'Di-replaceable fair use', - u'Di-no fair use rationale', - u'Di-disputed fair use rationale', - u'Puf', - u'PUI', - u'Pui', - u'Ffd', - u'PD-user', # Only the self templates are supported for now. - u'Ticket Scan', - u'Non-free 2D art', - u'Non-free 3D art', - u'Non-free architectural work', - u'Non-free fair use in', + 'en': ['Db-f1', + 'Db-f2', + 'Db-f3', + 'Db-f7', + 'Db-f8', + 'Db-f9', + 'Db-f10', + 'Do not move to Commons', + 'NowCommons', + 'CommonsNow', + 'Nowcommons', + 'NowCommonsThis', + 'Nowcommons2', + 'NCT', + 'Nowcommonsthis', + 'Moved to commons', + 'Now Commons', + 'Now at commons', + 'Db-nowcommons', + 'WikimediaCommons', + 'Now commons', + 'Di-no source', + 'Di-no license', + 'Di-no permission', + 'Di-orphaned fair use', + 'Di-no source no license', + 'Di-replaceable fair use', + 'Di-no fair use rationale', + 'Di-disputed fair use rationale', + 'Puf', + 'PUI', + 'Pui', + 'Ffd', + 'PD-user', # Only the self templates are supported for now. + 'Ticket Scan', + 'Non-free 2D art', + 'Non-free 3D art', + 'Non-free architectural work', + 'Non-free fair use in', ], - 'lb': [u'Läschen', + 'lb': ['Läschen', ], - 'nds-nl': [u'Allinnig Wikipedie', - u'Bepark', - u'Gienidee', - u'NoenCommons', - u'NowCommons', + 'nds-nl': ['Allinnig Wikipedie', + 'Bepark', + 'Gienidee', + 'NoenCommons', + 'NowCommons', ], - 'shared': [u''], + 'shared': [''], }
@@ -266,48 +266,48 @@
informationFields = { 'de': { - u'anmerkungen': u'remarks', # FIXME: More flexible - u'beschreibung': u'description', - u'quelle': u'source', - u'datum': u'date', - u'urheber': u'author', - u'permission': u'permission', - u'andere Versione': u'other versions', + 'anmerkungen': 'remarks', # FIXME: More flexible + 'beschreibung': 'description', + 'quelle': 'source', + 'datum': 'date', + 'urheber': 'author', + 'permission': 'permission', + 'andere Versione': 'other versions', }, 'en': { - u'location': u'remarks', - u'description': u'description', - u'source': u'source', - u'date': u'date', - u'author': u'author', - u'permission': u'permission', - u'other versions': u'other versions', + 'location': 'remarks', + 'description': 'description', + 'source': 'source', + 'date': 'date', + 'author': 'author', + 'permission': 'permission', + 'other versions': 'other versions', }, 'lb': { - u'location': u'remarks', - u'description': u'description', - u'source': u'source', - u'date': u'date', - u'author': u'author', - u'permission': u'permission', - u'other versions': u'other versions', + 'location': 'remarks', + 'description': 'description', + 'source': 'source', + 'date': 'date', + 'author': 'author', + 'permission': 'permission', + 'other versions': 'other versions', }, 'nds-nl': { - u'location': u'remarks', - u'description': u'description', - u'source': u'source', - u'date': u'date', - u'author': u'author', - u'permission': u'permission', - u'other versions': u'other versions', + 'location': 'remarks', + 'description': 'description', + 'source': 'source', + 'date': 'date', + 'author': 'author', + 'permission': 'permission', + 'other versions': 'other versions', }, 'shared': { - u'description': u'description', - u'source': u'source', - u'date': u'date', - u'author': u'author', - u'permission': u'permission', - u'other versions': u'other versions', + 'description': 'description', + 'source': 'source', + 'date': 'date', + 'author': 'author', + 'permission': 'permission', + 'other versions': 'other versions', }, }
@@ -360,8 +360,8 @@ # First do autoskip. if self.doiskip(imagepage): pywikibot.output( - u'Skipping %s : Got a template on the skip list.' - % page.title()) + 'Skipping {} : Got a template on the skip list.' + .format(page.title())) return False
text = imagepage.get() @@ -372,8 +372,8 @@ foundMatch = True if not foundMatch: pywikibot.output( - u'Skipping %s : No suitable license template was found.' - % page.title()) + 'Skipping {} : No suitable license template was found.' + .format(page.title())) return False self.prefetchQueue.put(self.getNewFields(imagepage))
@@ -386,35 +386,36 @@ for template in imagepage.templates(): if template in skipTemplates[imagepage.site.lang]: pywikibot.output( - u'Found %s which is on the template skip list' % template) + 'Found {} which is on the template skip list' + .format(template)) return True return False
def getNewFields(self, imagepage): """Build a new description based on the imagepage.""" - if u'{{Information' in imagepage.get() or \ - u'{{information' in imagepage.get(): + if '{{Information' in imagepage.get() or \ + '{{information' in imagepage.get(): (description, date, source, author, permission, other_versions) = self.getNewFieldsFromInformation(imagepage) else: (description, date, source, author) = self.getNewFieldsFromFreetext(imagepage) - permission = u'' - other_versions = u'' + permission = '' + other_versions = ''
licensetemplate = self.getNewLicensetemplate(imagepage) categories = self.getNewCategories(imagepage) - return {u'imagepage': imagepage, + return {'imagepage': imagepage, 'filename': imagepage.title(with_ns=False), - u'description': description, - u'date': date, - u'source': source, - u'author': author, - u'permission': permission, - u'other_versions': other_versions, - u'licensetemplate': licensetemplate, - u'categories': categories, - u'skip': False} + 'description': description, + 'date': date, + 'source': source, + 'author': author, + 'permission': permission, + 'other_versions': other_versions, + 'licensetemplate': licensetemplate, + 'categories': categories, + 'skip': False}
def getNewFieldsFromInformation(self, imagepage): """Extract fields from current information template for new template. @@ -427,16 +428,16 @@ # FIXME: The implementation for German has to be checked for the # "strange" fields
- description = u'' - source = u'' - date = u'' - author = u'' - permission = u'' - other_versions = u'' + description = '' + source = '' + date = '' + author = '' + permission = '' + other_versions = '' contents = {}
for key, value in informationFields[imagepage.site.lang].items(): - contents[value] = u'' + contents[value] = ''
information = informationTemplate[imagepage.site.lang] fields = informationFields[imagepage.site.lang] @@ -447,7 +448,7 @@ # Split at = field, sep, value = param.partition('=') # To lowercase, remove underscores and strip of spaces - field = field.lower().replace(u'_', u' ').strip() + field = field.lower().replace('_', ' ').strip() key = fields.get(field) # See if first part is in fields list if key: @@ -459,49 +460,49 @@
# Description # FIXME: Add {{<lang>|<original text>}} if <lang is valid at Commons - if contents[u'description']: - description = self.convertLinks(contents[u'description'], + if contents['description']: + description = self.convertLinks(contents['description'], imagepage.site()) - if contents.get(u'remarks') and contents[u'remarks']: - if description == u'': - description = self.convertLinks(contents[u'remarks'], + if contents.get('remarks') and contents['remarks']: + if description == '': + description = self.convertLinks(contents['remarks'], imagepage.site()) else: - description += u'<BR/>\n' + self.convertLinks( - contents[u'remarks'], imagepage.site()) + description += '<BR/>\n' + self.convertLinks( + contents['remarks'], imagepage.site())
# Source source = self.getSource(imagepage, - source=self.convertLinks(contents[u'source'], + source=self.convertLinks(contents['source'], imagepage.site()))
# Date - if contents[u'date']: - date = contents[u'date'] + if contents['date']: + date = contents['date'] else: date = self.getUploadDate(imagepage)
# Author - if not (contents[u'author'] == u'' or - contents[u'author'] == self.getAuthor(imagepage)): - author = self.convertLinks(contents[u'author'], imagepage.site()) + if not (contents['author'] == '' or + contents['author'] == self.getAuthor(imagepage)): + author = self.convertLinks(contents['author'], imagepage.site()) else: author = self.getAuthorText(imagepage)
# Permission # Still have to filter out crap like "see below" or "yes" - if contents[u'permission']: + if contents['permission']: # Strip of the license temlate if it's in the permission section for (regex, repl) in licenseTemplates[imagepage.site.lang]: - contents[u'permission'] = re.sub(regex, u'', - contents[u'permission'], - flags=re.IGNORECASE) - permission = self.convertLinks(contents[u'permission'], + contents['permission'] = re.sub(regex, '', + contents['permission'], + flags=re.IGNORECASE) + permission = self.convertLinks(contents['permission'], imagepage.site())
# Other_versions - if contents[u'other versions']: - other_versions = self.convertLinks(contents[u'other versions'], + if contents['other versions']: + other_versions = self.convertLinks(contents['other versions'], imagepage.site())
return (description, date, source, author, permission, other_versions) @@ -514,10 +515,10 @@ # text = re.sub('{{(self|self2)|[^}]+}}', '', text, re.IGNORECASE)
for toRemove in sourceGarbage[imagepage.site.lang]: - text = re.sub(toRemove, u'', text, flags=re.IGNORECASE) + text = re.sub(toRemove, '', text, flags=re.IGNORECASE)
for (regex, repl) in licenseTemplates[imagepage.site.lang]: - text = re.sub(regex, u'', text, flags=re.IGNORECASE) + text = re.sub(regex, '', text, flags=re.IGNORECASE)
text = pywikibot.removeCategoryLinks(text, imagepage.site()).strip()
@@ -535,22 +536,22 @@
""" uploadtime = imagepage.getFileVersionHistory()[-1][0] - uploadDatetime = datetime.strptime(uploadtime, u'%Y-%m-%dT%H:%M:%SZ') - return (u'{{Date|' + str(uploadDatetime.year) + u'|' + - str(uploadDatetime.month) + u'|' + str(uploadDatetime.day) + - u'}} (original upload date)') + uploadDatetime = datetime.strptime(uploadtime, '%Y-%m-%dT%H:%M:%SZ') + return ('{{Date|' + str(uploadDatetime.year) + '|' + + str(uploadDatetime.month) + '|' + str(uploadDatetime.day) + + '}} (original upload date)')
- def getSource(self, imagepage, source=u''): + def getSource(self, imagepage, source=''): """Get text to put in the source field of new information template.""" site = imagepage.site() lang = site.code family = site.family.name - if source == u'': - source = u'{{Own}}' + if source == '': + source = '{{Own}}'
return (source.strip() + '<BR />Transferred from [http://%(lang)s.%(family)s.org ' - '%(lang)s.%(family)s]') % {u'lang': lang, u'family': family} + '%(lang)s.%(family)s]') % {'lang': lang, 'family': family}
def getAuthorText(self, imagepage): """Get uploader to put in the author field of information template.""" @@ -561,8 +562,8 @@ firstuploader = self.getAuthor(imagepage) return ('[[:%(lang)s:User:%(firstuploader)s|%(firstuploader)s]] at ' '[http://%(lang)s.%(family)s.org %(lang)s.%(family)s]' - % {u'lang': lang, u'family': family, - u'firstuploader': firstuploader}) + % {'lang': lang, 'family': family, + 'firstuploader': firstuploader})
def getAuthor(self, imagepage): """Get the first uploader.""" @@ -577,8 +578,8 @@ (r'[[([^[]|]+)]]', r'[[:%(lang)s:\1|\1]]'), ] for (regex, replacement) in conversions: - text = re.sub(regex, replacement % {u'lang': lang, - u'family': family}, text) + text = re.sub(regex, replacement % {'lang': lang, + 'family': family}, text) return text
def getNewLicensetemplate(self, imagepage): @@ -587,16 +588,16 @@ site = imagepage.site() lang = site.code family = site.family.name - result = u'' + result = '' for (regex, replacement) in licenseTemplates[imagepage.site.lang]: match = re.search(regex, text, flags=re.IGNORECASE) if match: result = re.sub(regex, replacement, match.group(0), flags=re.IGNORECASE) - return result % {u'author': self.getAuthor(imagepage), - u'lang': lang, - u'family': family} + return result % {'author': self.getAuthor(imagepage), + 'lang': lang, + 'family': family} return result
def getNewCategories(self, imagepage): @@ -605,12 +606,12 @@ Don't forget to filter.
""" - result = u'' + result = '' (commonshelperCats, usage, galleries) = imagerecat.getCommonshelperCats(imagepage) newcats = imagerecat.applyAllFilters(commonshelperCats) for newcat in newcats: - result += u'[[Category:' + newcat + u']] ' + result += '[[Category:' + newcat + ']] ' return result
@@ -649,7 +650,7 @@ # already taken. CommonsPage = pywikibot.Page(pywikibot.Site('commons', 'commons'), - u'File:' + fields.get('filename')) + 'File:' + fields.get('filename')) if CommonsPage.exists(): return False else: @@ -658,14 +659,14 @@ fields = TkdialogICS(fields).getnewmetadata()
if fields.get('skip'): - pywikibot.output(u'Skipping %s : User pressed skip.' - % fields.get('imagepage').title()) + pywikibot.output('Skipping {} : User pressed skip.' + .format(fields.get('imagepage').title())) return False
# Check if the image already exists CommonsPage = pywikibot.Page(pywikibot.Site('commons', 'commons'), - u'File:' + fields.get('filename')) + 'File:' + fields.get('filename')) if not CommonsPage.exists(): break else: @@ -753,7 +754,7 @@ self.information_licensetemplate_label = Tkinter.Label( self.root, text='License : ') self.information_categories_label = Tkinter.Label( - self.root, text=u'Categories : ') + self.root, text='Categories : ')
self.filename_field = Tkinter.Entry(self.root) self.information_description = Tkinter.Entry(self.root) @@ -847,17 +848,17 @@ """Activate dialog and return new name and if the image is skipped.""" self.root.mainloop()
- return {u'imagepage': self.imagepage, - u'filename': self.filename, - u'description': self.description, - u'date': self.date, - u'source': self.source, - u'author': self.author, - u'permission': self.permission, - u'other_versions': self.other_versions, - u'licensetemplate': self.licensetemplate, - u'categories': self.categories, - u'skip': self.skip} + return {'imagepage': self.imagepage, + 'filename': self.filename, + 'description': self.description, + 'date': self.date, + 'source': self.source, + 'author': self.author, + 'permission': self.permission, + 'other_versions': self.other_versions, + 'licensetemplate': self.licensetemplate, + 'categories': self.categories, + 'skip': self.skip}
def open_in_browser(self): """The user pressed the View in browser button.""" @@ -910,7 +911,7 @@ lang = site.code family = site.family.name
- cid = u'' + cid = '' if self.checktemplate: cid += ('\n{{BotMoveToCommons|%(lang)s.%(family)s' '|year={{subst:CURRENTYEAR}}' @@ -918,24 +919,24 @@ '|day={{subst:CURRENTDAY}}}}\n' % {'lang': lang, 'family': family} ) - cid += u'== {{int:filedesc}} ==\n' - cid += u'{{Information\n' - cid += u'|description=%(description)s\n' % fields - cid += u'|date=%(date)s\n' % fields - cid += u'|source=%(source)s\n' % fields - cid += u'|author=%(author)s\n' % fields - cid += u'|permission=%(permission)s\n' % fields - cid += u'|other_versions=%(other_versions)s\n' % fields - cid += u'}}\n' - cid += u'== {{int:license}} ==\n' - cid += u'%(licensetemplate)s\n' % fields - cid += u'\n' + cid += '== {{int:filedesc}} ==\n' + cid += '{{Information\n' + cid += '|description=%(description)s\n' % fields + cid += '|date=%(date)s\n' % fields + cid += '|source=%(source)s\n' % fields + cid += '|author=%(author)s\n' % fields + cid += '|permission=%(permission)s\n' % fields + cid += '|other_versions=%(other_versions)s\n' % fields + cid += '}}\n' + cid += '== {{int:license}} ==\n' + cid += '%(licensetemplate)s\n' % fields + cid += '\n' cid += self.getOriginalUploadLog(fields.get('imagepage')) - cid += u'__NOTOC__\n' - if fields.get('categories').strip() == u'': - cid = cid + u'{{Subst:Unc}}' + cid += '__NOTOC__\n' + if fields.get('categories').strip() == '': + cid = cid + '{{Subst:Unc}}' else: - cid = cid + u'%(categories)s\n' % fields + cid = cid + '%(categories)s\n' % fields return cid
def getOriginalUploadLog(self, imagepage): @@ -952,17 +953,17 @@ family = site.family.name
sourceimage = imagepage.site.get_address( - imagepage.title()).replace(u'&redirect=no&useskin=monobook', u'') + imagepage.title()).replace('&redirect=no&useskin=monobook', '')
- result = u'== {{Original upload log}} ==\n' + result = '== {{Original upload log}} ==\n' result += ('The original description page is/was ' '[http://%(lang)s.%(family)s.org%(sourceimage)s here]. ' 'All following user names refer to %(lang)s.%(family)s.\n' - % {u'lang': lang, u'family': family, - u'sourceimage': sourceimage}) + % {'lang': lang, 'family': family, + 'sourceimage': sourceimage}) for (timestamp, username, resolution, size, comment) in filehistory: date = datetime.strptime( - timestamp, u'%Y-%m-%dT%H:%M:%SZ').strftime('%Y-%m-%d %H:%M') + timestamp, '%Y-%m-%dT%H:%M:%SZ').strftime('%Y-%m-%d %H:%M') result += ( '* %(date)s [[:%(lang)s:user:%(username)s|%(username)s]] ' "%(resolution)s (%(size)s bytes) ''" @@ -980,7 +981,7 @@ def tagNowcommons(self, imagepage, filename): """Tagged the imag which has been moved to Commons for deletion.""" if pywikibot.Page(pywikibot.Site('commons', 'commons'), - u'File:' + filename).exists(): + 'File:' + filename).exists(): # Get a fresh copy, force to get the page so we dont run into edit # conflicts imtxt = imagepage.get(force=True) @@ -1048,13 +1049,13 @@ return False
if not supportedSite(): - pywikibot.output(u'Sorry, this site is not supported (yet).') + pywikibot.output('Sorry, this site is not supported (yet).') return False
- pywikibot.warning(u'This is an experimental bot') - pywikibot.warning(u'It will only work on self published work images') - pywikibot.warning(u'This bot is still full of bugs') - pywikibot.warning(u'Use at your own risk!') + pywikibot.warning('This is an experimental bot') + pywikibot.warning('It will only work on self published work images') + pywikibot.warning('This bot is still full of bugs') + pywikibot.warning('Use at your own risk!')
prefetchQueue = Queue(maxsize=50) uploadQueue = Queue(maxsize=200) @@ -1073,8 +1074,8 @@ userInteractionThread.setAutonomous()
if not checkTemplate: - pywikibot.output(u'No check template will be added to the uploaded ' - u'files.') + pywikibot.output('No check template will be added to the uploaded ' + 'files.') uploaderThread.nochecktemplate()
# Using the listed variables one may keep track of thread start status @@ -1083,5 +1084,5 @@ # uploadDone = uploaderThread.start()
-if __name__ == "__main__": +if __name__ == '__main__': main()
pywikibot-commits@lists.wikimedia.org