http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9607
Revision: 9607
Author: xqt
Date: 2011-10-08 16:29:04 +0000 (Sat, 08 Oct 2011)
Log Message:
-----------
update from trunk
Modified Paths:
--------------
branches/rewrite/scripts/cosmetic_changes.py
Modified: branches/rewrite/scripts/cosmetic_changes.py
===================================================================
--- branches/rewrite/scripts/cosmetic_changes.py 2011-10-08 16:22:03 UTC (rev 9606)
+++ branches/rewrite/scripts/cosmetic_changes.py 2011-10-08 16:29:04 UTC (rev 9607)
@@ -31,10 +31,18 @@
if you're running a bot on multiple sites and want to do cosmetic changes on
all of them, but be careful if you do.
"""
+#
+# (C) xqt, 2009-2011
+# (C) Pywikipedia bot team, 2006-2010
+#
+# Distributed under the terms of the MIT license.
+#
__version__ = '$Id$'
+#
import pywikibot
import isbn
-from pywikibot import pagegenerators, i18n
+from pywikibot import pagegenerators
+from pywikibot import i18n
import sys
import re
@@ -48,13 +56,6 @@
'&warning;': warning,
}
-# Summary message when using this module as a stand-alone script
-msg_standalone = 'cosmetic_changes-standalone'
-
-# Summary message that will be appended to the normal message when
-# cosmetic changes are made on the fly
-msg_append = 'cosmetic_changes-append'
-
nn_iw_msg = u'<!--interwiki (no, sv, da first; then other languages alphabetically by name)-->'
# This is from interwiki.py;
@@ -323,6 +324,8 @@
# ignore ' see http://eo.wikipedia.org/w/index.php?title=Liberec&diff=next&oldid=2320801
#if self.site.lang == 'eo':
# ignore += [39]
+ if self.template:
+ ignore += [58]
text = pywikibot.html2unicode(text, ignore = ignore)
return text
@@ -374,12 +377,14 @@
German, and French Wikipedia. It might be that it is not wanted on other
wikis. If there are any complaints, please file a bug report.
"""
- exceptions = ['comment', 'math', 'nowiki', 'pre', 'source', 'timeline']
+ exceptions = ['comment', 'math', 'nowiki', 'pre', 'source', 'template',
+ 'timeline']
if not (self.redirect or self.template) and \
pywikibot.calledModuleName() != 'capitalize_redirects':
text = pywikibot.replaceExcept(
text,
- r'(?m)^(?P<bullet>[:;]*(\*+|#+)[:;\*#]*)(?P<char>[^\s\*#:;].+?)', '\g<bullet> \g<char>',
+ r'(?m)^(?P<bullet>[:;]*(\*+|#+)[:;\*#]*)(?P<char>[^\s\*#:;].+?)',
+ '\g<bullet> \g<char>',
exceptions)
return text
@@ -643,7 +648,8 @@
if editSummary == '':
# Load default summary message.
- editSummary = i18n.twtranslate(pywikibot.getSite(), msg_standalone)
+ editSummary = i18n.twtranslate(pywikibot.getSite(),
+ 'cosmetic_changes-standalone')
if pageTitle:
site = pywikibot.getSite()
gen = iter([pywikibot.Page(pywikibot.Link(t, site)) for t in pageTitle])
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9606
Revision: 9606
Author: xqt
Date: 2011-10-08 16:22:03 +0000 (Sat, 08 Oct 2011)
Log Message:
-----------
PEP8 changes
Modified Paths:
--------------
trunk/pywikipedia/upload.py
Modified: trunk/pywikipedia/upload.py
===================================================================
--- trunk/pywikipedia/upload.py 2011-10-08 13:58:31 UTC (rev 9605)
+++ trunk/pywikipedia/upload.py 2011-10-08 16:22:03 UTC (rev 9606)
@@ -19,7 +19,7 @@
"""
#
# (C) Rob W.W. Hooft, Andre Engels 2003-2004
-# (C) Pywikipedia bot team, 2003-2010
+# (C) Pywikipedia bot team, 2003-2011
#
# Distributed under the terms of the MIT license.
#
@@ -33,20 +33,25 @@
import config, query
def post_multipart(site, address, fields, files, cookies):
+ """ Post fields and files to an http host as multipart/form-data.
+
+ @param fields: sequence of (name, value) elements for regular form fields.
+ @param files: sequence of (name, filename, value) elements for data to be
+ uploaded as files
+ @return: the server's response page.
+
"""
- Post fields and files to an http host as multipart/form-data.
- fields is a sequence of (name, value) elements for regular form fields.
- files is a sequence of (name, filename, value) elements for data to be uploaded as files
- Return the server's response page.
- """
contentType, body = encode_multipart_formdata(fields, files)
- return site.postData(address, body, contentType = contentType, cookies = cookies)
+ return site.postData(address, body, contentType=contentType,
+ cookies=cookies)
def encode_multipart_formdata(fields, files):
"""
- fields is a sequence of (name, value) elements for regular form fields.
- files is a sequence of (name, filename, value) elements for data to be uploaded as files
- Return (content_type, body) ready for httplib.HTTP instance
+ @param fields: sequence of (name, value) elements for regular form fields.
+ @param files: sequence of (name, filename, value) elements for data to be
+ uploaded as files
+ @return: (content_type, body) ready for httplib.HTTP instance
+
"""
boundary = '----------ThIs_Is_tHe_bouNdaRY_$'
lines = []
@@ -60,7 +65,8 @@
lines.append(value.encode('utf-8'))
for (key, filename, value) in files:
lines.append('--' + boundary)
- lines.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
+ lines.append('Content-Disposition: form-data; name="%s"; filename="%s"'
+ % (key, filename))
lines.append('Content-Type: %s' % get_content_type(filename))
lines.append('')
lines.append(value)
@@ -75,12 +81,13 @@
class UploadRobot:
- def __init__(self, url, urlEncoding = None, description = u'', useFilename = None, keepFilename = False,
- verifyDescription = True, ignoreWarning = False, targetSite = None, uploadByUrl = False):
+ def __init__(self, url, urlEncoding=None, description=u'',
+ useFilename=None, keepFilename=False, verifyDescription=True,
+ ignoreWarning=False, targetSite=None, uploadByUrl=False):
"""
- ignoreWarning - Set this to True if you want to upload even if another
- file would be overwritten or another mistake would be
- risked.
+ @param ignoreWarning: Set this to True if you want to upload even if
+ another file would be overwritten or another mistake would be
+ risked.
"""
self._retrieved = False
@@ -92,7 +99,8 @@
self.verifyDescription = verifyDescription
self.ignoreWarning = ignoreWarning
if config.upload_to_commons:
- self.targetSite = targetSite or pywikibot.getSite('commons', 'commons')
+ self.targetSite = targetSite or pywikibot.getSite('commons',
+ 'commons')
else:
self.targetSite = targetSite or pywikibot.getSite()
self.targetSite.forceLogin()
@@ -125,11 +133,13 @@
file = uo.open(self.url)
if 'text/html' in file.info().getheader('Content-Type'):
- print "Couldn't download the image: the requested URL was not found on this server."
+ print \
+"Couldn't download the image: the requested URL was not found on this server."
return
content_len = file.info().getheader('Content-Length')
- accept_ranges = file.info().getheader('Accept-Ranges') == 'bytes'
+ accept_ranges = file.info().getheader(
+ 'Accept-Ranges') == 'bytes'
if resume:
self._contents += file.read()
@@ -144,7 +154,9 @@
content_len = int(content_len)
if rlen < content_len:
self._retrieved = False
- pywikibot.output(u"Connection closed at byte %s (%s left)" % (rlen, content_len))
+ pywikibot.output(
+ u"Connection closed at byte %s (%s left)"
+ % (rlen, content_len))
if accept_ranges and rlen > 0:
resume = True
pywikibot.output(u"Sleeping for %d seconds..." % dt)
@@ -155,7 +167,8 @@
dt += 60
else:
if pywikibot.verbose:
- pywikibot.output(u"WARNING: No check length to retrieved data is possible.")
+ pywikibot.output(
+u"WARNING: No check length to retrieved data is possible.")
else:
# Opening local files with MyURLopener would be possible, but we
# don't do it because it only accepts ASCII characters in the
@@ -202,7 +215,10 @@
print "Invalid character: %s. Please try again" % c
ok = False
if ext not in allowed_formats and ok:
- choice = pywikibot.inputChoice(u"File format is not one of [%s], but %s. Continue?" % (u' '.join(allowed_formats), ext), ['yes', 'no'], ['y', 'N'], 'N')
+ choice = pywikibot.inputChoice(
+ u"File format is not one of [%s], but %s. Continue?"
+ % (u' '.join(allowed_formats), ext),
+ ['yes', 'no'], ['y', 'N'], 'N')
if choice == 'n':
ok = False
if newfn != '':
@@ -276,7 +292,8 @@
pywikibot.output("Got warning message:")
for k,v in data['warnings'].iteritems():
if k == 'duplicate-archive':
- pywikibot.output("\tThe file is duplicate a deleted file %s." % v)
+ pywikibot.output(
+ "\tThe file is duplicate a deleted file %s." % v)
elif k == 'was-deleted':
pywikibot.output("\tThis file was deleted for %s." % v)
elif k == 'emptyfile':
@@ -284,25 +301,28 @@
elif k == 'exists':
pywikibot.output("\tFile %s is exists." % v)
elif k == 'duplicate':
- pywikibot.output("\tUploaded file is duplicate with %s." % v)
+ pywikibot.output("\tUploaded file is duplicate with %s."
+ % v)
elif k == 'badfilename':
pywikibot.output("\tTarget filename is invalid.")
elif k == 'filetype-unwanted-type':
- pywikibot.output("\tFile %s type is unwatched type." % v)
- answer = pywikibot.inputChoice(u"Do you want to ignore?", ['Yes', 'No'], ['y', 'N'], 'N')
+ pywikibot.output("\tFile %s type is unwatched type."
+ % v)
+ answer = pywikibot.inputChoice(u"Do you want to ignore?",
+ ['Yes', 'No'], ['y', 'N'], 'N')
if answer == "y":
self.ignoreWarning = 1
self.keepFilename = True
- return self.upload_image(debug, sessionKey = data['sessionkey'])
+ return self.upload_image(debug,
+ sessionKey=data['sessionkey'])
else:
pywikibot.output("Upload aborted.")
return
-
- elif data['result'] == u'Success': #No any warning, upload and online complete.
+ #No any warning, upload and online complete.
+ elif data['result'] == u'Success':
pywikibot.output(u"Upload successful.")
return filename #data['filename']
-
def _uploadImageOld(self, debug=False):
if not hasattr(self,'_contents'):
self.read_file_content()
@@ -312,7 +332,6 @@
# target wiki
encodedFilename = filename.encode(self.targetSite.encoding())
-
formdata = {
'wpUploadDescription': self.description,
'wpUploadAffirm': '1',
@@ -333,11 +352,13 @@
# the home Wikipedia uses Latin-1), convert all non-ASCII characters to
# HTML entities.
for key in formdata:
- assert isinstance(key, basestring), "ERROR: %s is not a string but %s" % (key, type(key))
+ assert isinstance(key, basestring), \
+ "ERROR: %s is not a string but %s" % (key, type(key))
try:
formdata[key] = formdata[key].encode(self.targetSite.encoding())
except (UnicodeEncodeError, UnicodeDecodeError):
- formdata[key] = pywikibot.UnicodeToAsciiHtml(formdata[key]).encode(self.targetSite.encoding())
+ formdata[key] = pywikibot.UnicodeToAsciiHtml(
+ formdata[key]).encode(self.targetSite.encoding())
# don't upload if we're in debug mode
if not debug:
@@ -345,48 +366,62 @@
if self.uploadByUrl:
# Just do a post with all the fields filled out
- response, returned_html = self.targetSite.postForm(self.targetSite.upload_address(), formdata.items(), cookies = self.targetSite.cookies())
+ response, returned_html = self.targetSite.postForm(
+ self.targetSite.upload_address(), formdata.items(),
+ cookies=self.targetSite.cookies())
else:
- response, returned_html = post_multipart(self.targetSite, self.targetSite.upload_address(),
- formdata.items(), (('wpUploadFile', encodedFilename, self._contents),),
- cookies = self.targetSite.cookies())
+ response, returned_html = post_multipart(
+ self.targetSite, self.targetSite.upload_address(),
+ formdata.items(),
+ (('wpUploadFile', encodedFilename, self._contents),),
+ cookies = self.targetSite.cookies())
# There are 2 ways MediaWiki can react on success: either it gives
# a 200 with a success message, or it gives a 302 (redirection).
# Do we know how the "success!" HTML page should look like?
- # ATTENTION: if you changed your Wikimedia Commons account not to show
- # an English interface, this detection will fail!
+ # ATTENTION: if you changed your Wikimedia Commons account not to
+ # show an English interface, this detection will fail!
success_msg = self.targetSite.mediawiki_message('successfulupload')
if success_msg in returned_html or response.code == 302:
pywikibot.output(u"Upload successful.")
- # The following is not a good idea, because the server also gives a 200 when
- # something went wrong.
- #if response.code in [200, 302]:
- # pywikibot.output(u"Upload successful.")
+ # The following is not a good idea, because the server also gives a
+ # 200 when something went wrong.
+## if response.code in [200, 302]:
+## pywikibot.output(u"Upload successful.")
elif response.code == 301:
pywikibot.output(u"Following redirect...")
address = response.getheader('Location')
- pywikibot.output(u"Changed upload address to %s. Please update %s.py" % (address, self.targetSite.family.__module__))
- exec('self.targetSite.upload_address = lambda: %r' % address, locals(), globals())
+ pywikibot.output(
+ u"Changed upload address to %s. Please update %s.py"
+ % (address, self.targetSite.family.__module__))
+ exec('self.targetSite.upload_address = lambda: %r' % address,
+ locals(), globals())
return self.upload_image(debug)
else:
try:
# Try to find the error message within the HTML page.
# If we can't find it, we just dump the entire HTML page.
- returned_html = returned_html[returned_html.index('<!-- start content -->') + 22: returned_html.index('<!-- end content -->')]
+ returned_html = returned_html[
+ returned_html.index('<!-- start content -->') + 22:
+ returned_html.index('<!-- end content -->')]
except:
pass
pywikibot.output(u'%s\n\n' % returned_html)
pywikibot.output(u'%i %s' % (response.code, response.msg))
- if self.targetSite.mediawiki_message('uploadwarning') in returned_html:
- answer = pywikibot.inputChoice(u"You have recevied an upload warning message. Ignore?", ['Yes', 'No'], ['y', 'N'], 'N')
+ if self.targetSite.mediawiki_message('uploadwarning'
+ ) in returned_html:
+ answer = pywikibot.inputChoice(
+ u"You have recevied an upload warning message. Ignore?",
+ ['Yes', 'No'], ['y', 'N'], 'N')
if answer == "y":
self.ignoreWarning = 1
self.keepFilename = True
return self._uploadImageOld(debug)
else:
- answer = pywikibot.inputChoice(u'Upload of %s probably failed. Above you see the HTML page which was returned by MediaWiki. Try again?' % filename, ['Yes', 'No'], ['y', 'N'], 'N')
+ answer = pywikibot.inputChoice(
+u'Upload of %s probably failed. Above you see the HTML page which was returned by MediaWiki. Try again?'
+ % filename, ['Yes', 'No'], ['y', 'N'], 'N')
if answer == "y":
return self._uploadImageOld(debug)
else: