jenkins-bot has submitted this change and it was merged.
Change subject: pep8 changes ......................................................................
pep8 changes
Change-Id: I84afc01e5015babfd97724854c5b6df9d423678e --- M query.py 1 file changed, 68 insertions(+), 48 deletions(-)
Approvals: Ladsgroup: Looks good to me, approved jenkins-bot: Verified
diff --git a/query.py b/query.py index ced5323..8a2207f 100644 --- a/query.py +++ b/query.py @@ -31,6 +31,7 @@ from pywikibot.support import deprecate_arg import json
+ @deprecate_arg("encodeTitle", None) def GetData(params, site=None, useAPI=True, retryCount=config.maxretries, encodeTitle=True, sysop=False, back_response=False): @@ -38,9 +39,10 @@ """ if ('action' in params) and pywikibot.simulate and \ (params['action'] in pywikibot.config.actions_to_block): - pywikibot.output(u'\03{lightyellow}SIMULATION: %s action blocked.\03{default}'%\ - params['action']) - jsontext_dummy = {params['action']: {u'result':u''}} + pywikibot.output( + u'\03{lightyellow}SIMULATION: %s action blocked.\03{default}' + % params['action']) + jsontext_dummy = {params['action']: {u'result': u''}} if back_response: import StringIO res_dummy = StringIO.StringIO() @@ -54,18 +56,19 @@ data = {} titlecount = 0
- for k,v in params.iteritems(): + for k, v in params.iteritems(): if k == u'file': data[k] = v elif type(v) == list: - if k in [u'titles', u'pageids', u'revids', u'ususers'] and len(v) > 10: + if k in [u'titles', u'pageids', u'revids', + u'ususers'] and len(v) > 10: # Titles param might be long, case convert it to post request titlecount = len(params[k]) data[k] = unicode(ListToParam(v)) else: params[k] = unicode(ListToParam(v))
- elif not isinstance(v,basestring): + elif not isinstance(v, basestring): params[k] = unicode(v) elif type(v) == unicode: params[k] = ToUtf8(v) @@ -80,7 +83,7 @@ for k in data: del params[k]
- if pywikibot.verbose: #dump params info. + if pywikibot.verbose: # dump params info. pywikibot.output(u"==== API action:%s ====" % params[u'action']) if data and 'file' not in data: pywikibot.output(u"%s: (%d items)" % (data.keys()[0], titlecount)) @@ -91,21 +94,19 @@ v = u'XXXXX' elif not isinstance(v, unicode): v = v.decode('utf-8') - pywikibot.output(u"%s: %s" % (k, v) ) - pywikibot.output(u'-' * 16 ) - + pywikibot.output(u"%s: %s" % (k, v)) + pywikibot.output(u'-' * 16)
postAC = [ 'edit', 'login', 'purge', 'rollback', 'delete', 'undelete', 'protect', - 'parse', 'block', 'unblock', 'move', 'emailuser','import', 'userrights', - 'upload', 'patrol', 'wbcreateclaim', 'wbeditentity' + 'parse', 'block', 'unblock', 'move', 'emailuser', 'import', + 'userrights', 'upload', 'patrol', 'wbcreateclaim', 'wbeditentity' ] if site.versionnumber() >= 18: postAC.append('watch') if useAPI: if params['action'] in postAC: path = site.api_address() - cont = '' else: path = site.api_address() + site.urlEncode(params.items())
@@ -127,28 +128,35 @@ jsontext = "Nothing received" if params['action'] == 'upload' and ('file' in data): import upload - res, jsontext = upload.post_multipart(site, path, params.items(), - (('file', params['filename'].encode(site.encoding()), data['file']),), - site.cookies(sysop=sysop) - ) - elif params['action'] in postAC or params['action'][:5]=='wbset': - res, jsontext = site.postForm(path, params, sysop, site.cookies(sysop = sysop) ) + res, jsontext = upload.post_multipart( + site, path, params.items(), + (('file', params['filename'].encode(site.encoding()), + data['file']), ), + site.cookies(sysop=sysop)) + elif params['action'] in postAC or params['action'][:5] == 'wbset': + res, jsontext = site.postForm(path, params, sysop, + site.cookies(sysop=sysop)) else: if back_response: - res, jsontext = site.getUrl( path, retry=True, data=data, sysop=sysop, back_response=True) + res, jsontext = site.getUrl(path, retry=True, data=data, + sysop=sysop, back_response=True) else: - jsontext = site.getUrl( path, retry=True, sysop=sysop, data=data) + jsontext = site.getUrl(path, retry=True, sysop=sysop, + data=data)
- # This will also work, but all unicode strings will need to be converted from \u notation - # decodedObj = eval( jsontext ) + # This will also work, but all unicode strings will need to be + # converted from \u notation +## decodedObj = eval(jsontext)
- jsontext = json.loads( jsontext ) + jsontext = json.loads(jsontext)
if "error" in jsontext: errorDetails = jsontext["error"] if errorDetails["code"] == 'badtoken': - pywikibot.output('Received a bad login token error from the server. Attempting to refresh.') - params['token'] = site.getToken(sysop = sysop, getagain = True) + pywikibot.output('Received a bad login token error from ' + 'the server. Attempting to refresh.') + params['token'] = site.getToken(sysop=sysop, + getagain=True) continue
if back_response: @@ -160,7 +168,7 @@ if "<title>Wiki does not exist</title>" in jsontext: raise pywikibot.NoSuchSite(u'Wiki %s does not exist yet' % site)
- if 'Wikimedia Error' in jsontext: #wikimedia server error + if 'Wikimedia Error' in jsontext: # wikimedia server error raise pywikibot.ServerError
retryCount -= 1 @@ -175,10 +183,14 @@ if retry_idle_time > 30: retry_idle_time = 30 else: - pywikibot.debugDump('ApiGetDataParse', site, str(error) + '\n%s\n%s' % (site.hostname(), path), jsontext) + pywikibot.debugDump('ApiGetDataParse', site, + str(error) + '\n%s\n%s' % (site.hostname(), + path), + jsontext) raise lastError
-def GetInterwikies(site, titles, extraParams = None ): + +def GetInterwikies(site, titles, extraParams=None): """ Usage example: data = GetInterwikies('ru','user:yurik') titles may be either ane title (as a string), or a list of strings extraParams if given must be a dict() as taken by GetData() @@ -190,10 +202,11 @@ 'titles': ListToParam(titles), 'redirects': 1, } - params = CombineParams( params, extraParams ) + params = CombineParams(params, extraParams) return GetData(params, site)
-def GetLinks(site, titles, extraParams = None ): + +def GetLinks(site, titles, extraParams=None): """ Get list of templates for the given titles """ params = { @@ -202,7 +215,7 @@ 'titles': ListToParam(titles), 'redirects': 1, } - params = CombineParams( params, extraParams ) + params = CombineParams(params, extraParams) return GetData(params, site)
# @@ -211,41 +224,45 @@ # #
-def CleanParams( params ): + +def CleanParams(params): """Params may be either a tuple, a list of tuples or a dictionary. This method will convert it into a dictionary """ if params is None: return {} - pt = type( params ) + pt = type(params) if pt == dict: return params - elif pt == typle: - if len( params ) != 2: raise "Tuple size must be 2" - return {params[0]:params[1]} + elif pt == tuple: + if len(params) != 2: + raise "Tuple size must be 2" + return {params[0]: params[1]} elif pt == list: for p in params: - if p != tuple or len( p ) != 2: raise "Every list element must be a 2 item tuple" - return dict( params ) + if p != tuple or len(p) != 2: + raise "Every list element must be a 2 item tuple" + return dict(params) else: raise "Unknown param type %s" % pt
-def CombineParams( params1, params2 ): + +def CombineParams(params1, params2): """Merge two dictionaries. If they have the same keys, their values will be appended one after another separated by the '|' symbol. """
- params1 = CleanParams( params1 ) + params1 = CleanParams(params1) if params2 is None: return params1 - params2 = CleanParams( params2 ) + params2 = CleanParams(params2)
for k, v2 in params2.iteritems(): if k in params1: v1 = params1[k] - if len( v1 ) == 0: + if len(v1) == 0: params1[k] = v2 - elif len( v2 ) > 0: + elif len(v2) > 0: if str in [type(v1), type(v2)]: raise "Both merged values must be of type 'str'" params1[k] = v1 + '|' + v2 @@ -254,22 +271,24 @@ params1[k] = v2 return params1
-def ConvToList( item ): + +def ConvToList(item): """Ensure the output is a list """ if item is None: return [] - elif isinstance(item,basestring): + elif isinstance(item, basestring): return [item] else: return item
-def ListToParam( list ): + +def ListToParam(list): """Convert a list of unicode strings into a UTF8 string separated by the '|' symbols
""" - list = ConvToList( list ) + list = ConvToList(list) if len(list) == 0: return ''
@@ -295,6 +314,7 @@ # strip trailing '|' before returning return encList[:-1]
+ def ToUtf8(s): if type(s) != unicode: try:
pywikibot-commits@lists.wikimedia.org