import zlib
-def dict_from_cookiejar(cookiejar):
- """Returns a key/value dictionary from a CookieJar."""
+def dict_from_cookiejar(cj):
+ """Returns a key/value dictionary from a CookieJar.
+
+ :param cj: CookieJar object to extract cookies from.
+ """
cookie_dict = {}
- for _, cookies in cookiejar._cookies.items():
+ for _, cookies in cj._cookies.items():
for _, cookies in cookies.items():
for cookie in cookies.values():
# print cookie
def cookiejar_from_dict(cookie_dict):
- """Returns a CookieJar from a key/value dictionary."""
+ """Returns a CookieJar from a key/value dictionary.
+
+ :param cookie_dict: Dict of key/values to insert into CookieJar.
+ """
# return cookiejar if one was passed in
if isinstance(cookie_dict, cookielib.CookieJar):
def add_dict_to_cookiejar(cj, cookie_dict):
- """Returns a CookieJar from a key/value dictionary."""
+ """Returns a CookieJar from a key/value dictionary.
+
+ :param cj: CookieJar to insert cookies into.
+ :param cookie_dict: Dict of key/values to insert into CookieJar.
+ """
for k, v in cookie_dict.items():
def get_encodings_from_content(content):
- """Returns encodings from given content string."""
+ """Returns encodings from given content string.
+
+ :param content: bytestring to extract encodings from.
+ """
charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I)
def get_encoding_from_headers(headers):
- """Returns encodings from given HTTP Header Dict."""
+ """Returns encodings from given HTTP Header Dict.
+
+ :param headers: dictionary to extract encoding from.
+ """
content_type = headers.get('content-type')
content_type, params = cgi.parse_header(content_type)
def get_unicode_from_response(r):
"""Returns the requested content back in unicode.
+ :param r: Reponse object to get unicode content from.
+
Tried:
1. charset from content-type
2. every encodings from <meta ... charset=XXX>
def decode_gzip(content):
- """Return gzip-decoded string."""
+ """Return gzip-decoded string.
+
+ :param content: bytestring to gzip-decode.
+ """
return zlib.decompress(content, 16+zlib.MAX_WBITS)
\ No newline at end of file