# Path: pyTOP/packages/requests/auth.py
# def dispatch(t):
#     """Given an auth tuple, return an expanded version."""
# 
#     if not t:
#         return t
#     else:
#         t = list(t)
# 
#     # Make sure they're passing in something.
#     assert len(t) >= 2
# 
#     # If only two items are passed in, assume HTTPBasic.
#     if (len(t) == 2):
#         t.insert(0, 'basic')
# 
#     # Allow built-in string referenced auths.
#     if isinstance(t[0], basestring):
#         if t[0] in ('basic', 'forced_basic'):
#             t[0] = http_basic
#         elif t[0] in ('digest',):
#             t[0] = http_digest
# 
#     # Return a custom callable.
#     return (t[0], tuple(t[1:]))
# 
# Path: pyTOP/packages/requests/utils.py
# def get_unicode_from_response(r):
#     """Returns the requested content back in unicode.
# 
#     :param r: Response object to get unicode content from.
# 
#     Tried:
# 
#     1. charset from content-type
# 
#     2. every encodings from ``<meta ... charset=XXX>``
# 
#     3. fall back and replace all unicode characters
# 
#     """
# 
#     tried_encodings = []
# 
#     # Try charset from content-type
#     encoding = get_encoding_from_headers(r.headers)
# 
#     if encoding:
#         try:
#             return unicode(r.content, encoding)
#         except UnicodeError:
#             tried_encodings.append(encoding)
# 
#     # Fall back:
#     try:
#         return unicode(r.content, encoding, errors='replace')
#     except TypeError:
#         return r.content
# 
# def stream_decode_response_unicode(iterator, r):
#     """Stream decodes a iterator."""
#     encoding = get_encoding_from_headers(r.headers)
#     if encoding is None:
#         for item in iterator:
#             yield item
#         return
# 
#     decoder = codecs.getincrementaldecoder(encoding)(errors='replace')
#     for chunk in iterator:
#         rv = decoder.decode(chunk)
#         if rv:
#             yield rv
#     rv = decoder.decode('', final=True)
#     if rv:
#         yield rv
# 
# def decode_gzip(content):
#     """Return gzip-decoded string.
# 
#     :param content: bytestring to gzip-decode.
#     """
# 
#     return zlib.decompress(content, 16 + zlib.MAX_WBITS)
# 
# def stream_decode_gzip(iterator):
#     """Stream decodes a gzip-encoded iterator"""
#     try:
#         dec = zlib.decompressobj(16 + zlib.MAX_WBITS)
#         for chunk in iterator:
#             rv = dec.decompress(chunk)
#             if rv:
#                 yield rv
#         buf = dec.decompress('')
#         rv = buf + dec.flush()
#         if rv:
#             yield rv
#     except zlib.error:
#         pass
# 
# def guess_filename(obj):
#     """Tries to guess the filename of the given object."""
#     name = getattr(obj, 'name', None)
#     if name and name[0] != '<' and name[-1] != '>':
#         return name


import urllib
import zlib
from Cookie import SimpleCookie
from urlparse import urlparse, urlunparse, urljoin, urlsplit
from datetime import datetime
from .auth import dispatch as auth_dispatch
from .hooks import dispatch_hook
from .structures import CaseInsensitiveDict
from .status_codes import codes
from .packages.urllib3.exceptions import MaxRetryError
from .packages.urllib3.exceptions import SSLError as _SSLError
from .packages.urllib3.exceptions import HTTPError as _HTTPError
from .packages.urllib3 import connectionpool, poolmanager
from .packages.urllib3.filepost import encode_multipart_formdata
from .exceptions import (
    Timeout, URLRequired, TooManyRedirects, HTTPError, ConnectionError)
from .utils import (
    get_unicode_from_response, stream_decode_response_unicode,
    decode_gzip, stream_decode_gzip, guess_filename)

        Once a request is successfully sent, `sent` will equal True.

        :param anyway: If True, request will be sent, even if it has
        already been sent.
        """

        # Logging
        if self.config.get('verbose'):
            self.config.get('verbose').write('%s   %s   %s\n' % (
                datetime.now().isoformat(), self.method, self.url
            ))

        # Build the URL
        url = self.full_url

        # Nottin' on you.
        body = None
        content_type = None

        # Multi-part file uploads.
        if self.files:
            if not isinstance(self.data, basestring):

                try:
                    fields = self.data.copy()
                except AttributeError:
                    fields = dict(self.data)

                for (k, v) in self.files.items():

# Path: pyTOP/packages/requests/auth.py
# def dispatch(t):
#     """Given an auth tuple, return an expanded version."""
# 
#     if not t:
#         return t
#     else:
#         t = list(t)
# 
#     # Make sure they're passing in something.
#     assert len(t) >= 2
# 
#     # If only two items are passed in, assume HTTPBasic.
#     if (len(t) == 2):
#         t.insert(0, 'basic')
# 
#     # Allow built-in string referenced auths.
#     if isinstance(t[0], basestring):
#         if t[0] in ('basic', 'forced_basic'):
#             t[0] = http_basic
#         elif t[0] in ('digest',):
#             t[0] = http_digest
# 
#     # Return a custom callable.
#     return (t[0], tuple(t[1:]))
# 
# Path: pyTOP/packages/requests/utils.py
# def get_unicode_from_response(r):
#     """Returns the requested content back in unicode.
# 
#     :param r: Response object to get unicode content from.
# 
#     Tried:
# 
#     1. charset from content-type
# 
#     2. every encodings from ``<meta ... charset=XXX>``
# 
#     3. fall back and replace all unicode characters
# 
#     """
# 
#     tried_encodings = []
# 
#     # Try charset from content-type
#     encoding = get_encoding_from_headers(r.headers)
# 
#     if encoding:
#         try:
#             return unicode(r.content, encoding)
#         except UnicodeError:
#             tried_encodings.append(encoding)
# 
#     # Fall back:
#     try:
#         return unicode(r.content, encoding, errors='replace')
#     except TypeError:
#         return r.content
# 
# def stream_decode_response_unicode(iterator, r):
#     """Stream decodes a iterator."""
#     encoding = get_encoding_from_headers(r.headers)
#     if encoding is None:
#         for item in iterator:
#             yield item
#         return
# 
#     decoder = codecs.getincrementaldecoder(encoding)(errors='replace')
#     for chunk in iterator:
#         rv = decoder.decode(chunk)
#         if rv:
#             yield rv
#     rv = decoder.decode('', final=True)
#     if rv:
#         yield rv
# 
# def decode_gzip(content):
#     """Return gzip-decoded string.
# 
#     :param content: bytestring to gzip-decode.
#     """
# 
#     return zlib.decompress(content, 16 + zlib.MAX_WBITS)
# 
# def stream_decode_gzip(iterator):
#     """Stream decodes a gzip-encoded iterator"""
#     try:
#         dec = zlib.decompressobj(16 + zlib.MAX_WBITS)
#         for chunk in iterator:
#             rv = dec.decompress(chunk)
#             if rv:
#                 yield rv
#         buf = dec.decompress('')
#         rv = buf + dec.flush()
#         if rv:
#             yield rv
#     except zlib.error:
#         pass
# 
# def guess_filename(obj):
#     """Tries to guess the filename of the given object."""
#     name = getattr(obj, 'name', None)
#     if name and name[0] != '<' and name[-1] != '>':
#         return name


# Path: pyTOP/packages/requests/models.py
import urllib
import zlib
from Cookie import SimpleCookie
from urlparse import urlparse, urlunparse, urljoin, urlsplit
from datetime import datetime
from .auth import dispatch as auth_dispatch
from .hooks import dispatch_hook
from .structures import CaseInsensitiveDict
from .status_codes import codes
from .packages.urllib3.exceptions import MaxRetryError
from .packages.urllib3.exceptions import SSLError as _SSLError
from .packages.urllib3.exceptions import HTTPError as _HTTPError
from .packages.urllib3 import connectionpool, poolmanager
from .packages.urllib3.filepost import encode_multipart_formdata
from .exceptions import (
    Timeout, URLRequired, TooManyRedirects, HTTPError, ConnectionError)
from .utils import (
    get_unicode_from_response, stream_decode_response_unicode,
    decode_gzip, stream_decode_gzip, guess_filename)


        Once a request is successfully sent, `sent` will equal True.

        :param anyway: If True, request will be sent, even if it has
        already been sent.
        """

        # Logging
        if self.config.get('verbose'):
            self.config.get('verbose').write('%s   %s   %s\n' % (
                datetime.now().isoformat(), self.method, self.url
            ))

        # Build the URL
        url = self.full_url

        # Nottin' on you.
        body = None
        content_type = None

        # Multi-part file uploads.
        if self.files:
            if not isinstance(self.data, basestring):

                try:
                    fields = self.data.copy()
                except AttributeError:
                    fields = dict(self.data)

                for (k, v) in self.files.items():

                    fields.update({k: (guess_filename(k) or k, v.read())})