55File: client.py
66Author: goodspeed
77Email: cacique1103@gmail.com
8- Github: https://github.com/zongxiao
8+ Github: https://github.com/gusibi
99Date: 2015-02-11
1010Description: Weixin helpers
1111"""
2020
2121PY2 = sys .version_info [0 ] == 2
2222
23- _always_safe = ('abcdefghijklmnopqrstuvwxyz'
24- 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-+' )
23+ _always_safe = "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-+"
2524
2625safe_char = _always_safe
2726
28- error_dict = {
29- 'AppID 参数错误' : {
30- 'errcode' : 40013 ,
31- 'errmsg' : 'invalid appid'
32- }
33- }
27+ error_dict = {"AppID 参数错误" : {"errcode" : 40013 , "errmsg" : "invalid appid" }}
3428
3529
3630if PY2 :
3731 text_type = unicode
3832 iteritems = lambda d , * args , ** kwargs : d .iteritems (* args , ** kwargs )
3933
40- def to_native (x , charset = sys .getdefaultencoding (), errors = ' strict' ):
34+ def to_native (x , charset = sys .getdefaultencoding (), errors = " strict" ):
4135 if x is None or isinstance (x , str ):
4236 return x
4337 return x .encode (charset , errors )
38+
39+
4440else :
4541 text_type = str
4642 iteritems = lambda d , * args , ** kwargs : iter (d .items (* args , ** kwargs ))
4743
48- def to_native (x , charset = sys .getdefaultencoding (), errors = ' strict' ):
44+ def to_native (x , charset = sys .getdefaultencoding (), errors = " strict" ):
4945 if x is None or isinstance (x , str ):
5046 return x
5147 return x .decode (charset , errors )
@@ -60,15 +56,18 @@ def to_native(x, charset=sys.getdefaultencoding(), errors='strict'):
6056
6157try :
6258 import hashlib
59+
6360 md5_constructor = hashlib .md5
6461 md5_hmac = md5_constructor
6562 sha_constructor = hashlib .sha1
6663 sha_hmac = sha_constructor
6764except ImportError :
6865 import md5
66+
6967 md5_constructor = md5 .new
7068 md5_hmac = md5
7169 import sha
70+
7271 sha_constructor = sha .new
7372 sha_hmac = sha
7473
@@ -79,6 +78,7 @@ class Promise(object):
7978 the closure of the lazy function. It can be used to recognize
8079 promises in code.
8180 """
81+
8282 pass
8383
8484
@@ -89,11 +89,10 @@ def __init__(self, obj, *args):
8989
9090 def __str__ (self ):
9191 original = UnicodeDecodeError .__str__ (self )
92- return '%s. You passed in %r (%s)' % (original , self .obj ,
93- type (self .obj ))
92+ return "%s. You passed in %r (%s)" % (original , self .obj , type (self .obj ))
9493
9594
96- def smart_text (s , encoding = ' utf-8' , strings_only = False , errors = ' strict' ):
95+ def smart_text (s , encoding = " utf-8" , strings_only = False , errors = " strict" ):
9796 """
9897 Returns a text object representing 's' -- unicode on Python 2 and str on
9998 Python 3. Treats bytestrings using the 'encoding' codec.
@@ -105,9 +104,14 @@ def smart_text(s, encoding='utf-8', strings_only=False, errors='strict'):
105104 return force_text (s , encoding , strings_only , errors )
106105
107106
108- _PROTECTED_TYPES = six .integer_types + (type (None ), float , Decimal ,
109- datetime .datetime , datetime .date ,
110- datetime .time )
107+ _PROTECTED_TYPES = six .integer_types + (
108+ type (None ),
109+ float ,
110+ Decimal ,
111+ datetime .datetime ,
112+ datetime .date ,
113+ datetime .time ,
114+ )
111115
112116
113117def is_protected_type (obj ):
@@ -118,7 +122,7 @@ def is_protected_type(obj):
118122 return isinstance (obj , _PROTECTED_TYPES )
119123
120124
121- def force_text (s , encoding = ' utf-8' , strings_only = False , errors = ' strict' ):
125+ def force_text (s , encoding = " utf-8" , strings_only = False , errors = " strict" ):
122126 """
123127 Similar to smart_text, except that lazy instances are resolved to
124128 strings, rather than kept as lazy objects.
@@ -136,7 +140,7 @@ def force_text(s, encoding='utf-8', strings_only=False, errors='strict'):
136140 s = six .text_type (s , encoding , errors )
137141 else :
138142 s = six .text_type (s )
139- elif hasattr (s , ' __unicode__' ):
143+ elif hasattr (s , " __unicode__" ):
140144 s = six .text_type (s )
141145 else :
142146 s = six .text_type (bytes (s ), encoding , errors )
@@ -154,12 +158,11 @@ def force_text(s, encoding='utf-8', strings_only=False, errors='strict'):
154158 # working unicode method. Try to handle this without raising a
155159 # further exception by individually forcing the exception args
156160 # to unicode.
157- s = ' ' .join (force_text (arg , encoding , strings_only , errors )
158- for arg in s )
161+ s = " " .join (force_text (arg , encoding , strings_only , errors ) for arg in s )
159162 return s
160163
161164
162- def smart_bytes (s , encoding = ' utf-8' , strings_only = False , errors = ' strict' ):
165+ def smart_bytes (s , encoding = " utf-8" , strings_only = False , errors = " strict" ):
163166 """
164167 Returns a bytestring version of 's', encoded as specified in 'encoding'.
165168 If strings_only is True, don't convert (some) non-string-like objects.
@@ -170,18 +173,18 @@ def smart_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
170173 return force_bytes (s , encoding , strings_only , errors )
171174
172175
173- def force_bytes (s , encoding = ' utf-8' , strings_only = False , errors = ' strict' ):
176+ def force_bytes (s , encoding = " utf-8" , strings_only = False , errors = " strict" ):
174177 """
175178 Similar to smart_bytes, except that lazy instances are resolved to
176179 strings, rather than kept as lazy objects.
177180 If strings_only is True, don't convert (some) non-string-like objects.
178181 """
179182 # Handle the common case first for performance reasons.
180183 if isinstance (s , bytes ):
181- if encoding == ' utf-8' :
184+ if encoding == " utf-8" :
182185 return s
183186 else :
184- return s .decode (' utf-8' , errors ).encode (encoding , errors )
187+ return s .decode (" utf-8" , errors ).encode (encoding , errors )
185188 if strings_only and is_protected_type (s ):
186189 return s
187190 if isinstance (s , Promise ):
@@ -197,13 +200,14 @@ def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
197200 # An Exception subclass containing non-ASCII data that doesn't
198201 # know how to print itself properly. We shouldn't raise a
199202 # further exception.
200- return b' ' .join (force_bytes ( arg , encoding ,
201- strings_only , errors )
202- for arg in s )
203+ return b" " .join (
204+ force_bytes ( arg , encoding , strings_only , errors ) for arg in s
205+ )
203206 return six .text_type (s ).encode (encoding , errors )
204207 else :
205208 return s .encode (encoding , errors )
206209
210+
207211if six .PY3 :
208212 smart_str = smart_text
209213 force_str = force_text
@@ -228,31 +232,32 @@ def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
228232def genarate_js_signature (params ):
229233 keys = params .keys ()
230234 keys .sort ()
231- params_str = b''
235+ params_str = b""
232236 for key in keys :
233- params_str += b' %s=%s&' % (smart_str (key ), smart_str (params [key ]))
237+ params_str += b" %s=%s&" % (smart_str (key ), smart_str (params [key ]))
234238 params_str = params_str [:- 1 ]
235239 return sha1 (params_str ).hexdigest ()
236240
237241
238242def genarate_signature (params ):
239243 sorted_params = sorted ([v for k , v in params .items ()])
240- params_str = smart_str ('' .join (sorted_params ))
241- return sha1 (str (params_str ).encode (' utf-8' )).hexdigest ()
244+ params_str = smart_str ("" .join (sorted_params ))
245+ return sha1 (str (params_str ).encode (" utf-8" )).hexdigest ()
242246
243247
244248def get_encoding (html = None , headers = None ):
245249 try :
246250 import chardet
251+
247252 if html :
248- encoding = chardet .detect (html ).get (' encoding' )
253+ encoding = chardet .detect (html ).get (" encoding" )
249254 return encoding
250255 except ImportError :
251256 pass
252257 if headers :
253- content_type = headers .get (' content-type' )
258+ content_type = headers .get (" content-type" )
254259 try :
255- encoding = content_type .split (' ' )[1 ].split ('=' )[1 ]
260+ encoding = content_type .split (" " )[1 ].split ("=" )[1 ]
256261 return encoding
257262 except IndexError :
258263 pass
@@ -275,7 +280,7 @@ def iter_multi_items(mapping):
275280 yield item
276281
277282
278- def url_quote (string , charset = ' utf-8' , errors = ' strict' , safe = '/:' , unsafe = '' ):
283+ def url_quote (string , charset = " utf-8" , errors = " strict" , safe = "/:" , unsafe = "" ):
279284 """
280285 URL encode a single string with a given encoding.
281286
@@ -302,12 +307,12 @@ def url_quote(string, charset='utf-8', errors='strict', safe='/:', unsafe=''):
302307 if char in safe :
303308 rv .append (char )
304309 else :
305- rv .extend ((' %%%02X' % char ).encode (' ascii' ))
310+ rv .extend ((" %%%02X" % char ).encode (" ascii" ))
306311 return to_native (bytes (rv ))
307312
308313
309- def url_quote_plus (string , charset = ' utf-8' , errors = ' strict' , safe = '' ):
310- return url_quote (string , charset , errors , safe + ' ' , '+' ).replace (' ' , '+' )
314+ def url_quote_plus (string , charset = " utf-8" , errors = " strict" , safe = "" ):
315+ return url_quote (string , charset , errors , safe + " " , "+" ).replace (" " , "+" )
311316
312317
313318def _url_encode_impl (obj , charset , encode_keys , sort , key ):
@@ -321,40 +326,40 @@ def _url_encode_impl(obj, charset, encode_keys, sort, key):
321326 key = text_type (key ).encode (charset )
322327 if not isinstance (value , bytes ):
323328 value = text_type (value ).encode (charset )
324- yield url_quote_plus (key ) + '=' + url_quote_plus (value )
329+ yield url_quote_plus (key ) + "=" + url_quote_plus (value )
325330
326331
327- def url_encode (obj , charset = 'utf-8' , encode_keys = False , sort = False , key = None ,
328- separator = b'&' ):
329- separator = to_native (separator , 'ascii' )
332+ def url_encode (
333+ obj , charset = "utf-8" , encode_keys = False , sort = False , key = None , separator = b"&"
334+ ):
335+ separator = to_native (separator , "ascii" )
330336 return separator .join (_url_encode_impl (obj , charset , encode_keys , sort , key ))
331337
332338
333339class WeixiErrorParser (html_parser .HTMLParser ):
334-
335340 def __init__ (self ):
336341 html_parser .HTMLParser .__init__ (self )
337342 self .recording = 0
338343 self .data = []
339344
340345 def handle_starttag (self , tag , attrs ):
341- if tag != 'h4' :
346+ if tag != "h4" :
342347 return
343348 if self .recording :
344349 self .recording += 1
345350 self .recording = 1
346351
347352 def handle_endtag (self , tag ):
348- if tag == 'h4' and self .recording :
353+ if tag == "h4" and self .recording :
349354 self .recording -= 1
350355
351356 def handle_data (self , data ):
352357 if self .recording :
353358 self .data .append (data )
354359
355360
356- def error_parser (error_html , encoding = ' gbk' ):
357- html = text_type (error_html , encoding or ' gbk' )
361+ def error_parser (error_html , encoding = " gbk" ):
362+ html = text_type (error_html , encoding or " gbk" )
358363 error_parser = WeixiErrorParser ()
359364 error_parser .feed (html )
360365 if error_parser .data :
0 commit comments