# -*- coding: utf-8 -*-

import json
import functools
import sys


if sys.version_info[0] == 3:
    from urllib.parse import quote as _quote
    from urllib.parse import unquote as _unquote
    from urllib.parse import urlunsplit, urlsplit

    string_type = str
    bytes_type = bytes

else:
    from urllib import quote as _quote
    from urllib import unquote as _unquote
    from urlparse import urlunsplit, urlsplit

    string_type = unicode
    bytes_type = str


json_encoder = json.JSONEncoder()


def _extract_credentials(url):
    """
    Extract authentication (user name and password) credentials from the
    given URL.

    >>> extract_credentials('http://localhost:5984/_config/')
    ('http://localhost:5984/_config/', None)
    >>> extract_credentials('http://joe:secret@localhost:5984/_config/')
    ('http://localhost:5984/_config/', ('joe', 'secret'))
    >>> extract_credentials('http://joe%40example.com:secret@localhost:5984/_config/')
    ('http://localhost:5984/_config/', ('joe@example.com', 'secret'))
    """
    parts = urlsplit(url)
    netloc = parts[1]
    if '@' in netloc:
        creds, netloc = netloc.split('@')
        credentials = tuple(_unquote(i) for i in creds.split(':'))
        parts = list(parts)
        parts[1] = netloc
    else:
        credentials = None
    return urlunsplit(parts), credentials


def quote(data, safe=b''):
    if isinstance(data, str):
        data = data.encode('utf-8')
    return _quote(data, safe)



def urljoin(base, *path):
    """
    Assemble a uri based on a base, any number of path segments, and query
    string parameters.

    >>> urljoin('http://example.org', '_all_dbs')
    'http://example.org/_all_dbs'

    A trailing slash on the uri base is handled gracefully:

    >>> urljoin('http://example.org/', '_all_dbs')
    'http://example.org/_all_dbs'

    And multiple positional arguments become path parts:

    >>> urljoin('http://example.org/', 'foo', 'bar')
    'http://example.org/foo/bar'

    All slashes within a path part are escaped:

    >>> urljoin('http://example.org/', 'foo/bar')
    'http://example.org/foo%2Fbar'
    >>> urljoin('http://example.org/', 'foo', '/bar/')
    'http://example.org/foo/%2Fbar%2F'

    >>> urljoin('http://example.org/', None) #doctest:+IGNORE_EXCEPTION_DETAIL
    Traceback (most recent call last):
        ...
    TypeError: argument 2 to map() must support iteration
    """
    if base and base.endswith('/'):
        base = base[:-1]
    retval = [base]

    # build the path
    path = '/'.join([''] + [quote(s) for s in path])
    if path:
        retval.append(path)

    return ''.join(retval)

def as_json(response):
    if "application/json" in response.headers['content-type']:
        if response.text != "":
            return json.loads(response.text)
        else:
            return response.text

    return None

def to_json(doc):
    return json.dumps(doc)

def _path_from_name(name, type):
    """
    Expand a 'design/foo' style name to its full path as a list of
    segments.
    """
    if name.startswith('_'):
        return name.split('/')
    design, name = name.split('/', 1)
    return ['_design', design, type, name]

def _encode_view_options(options):
    """
    Encode any items in the options dict that are sent as a JSON string to a
    view/list function.
    """
    retval = {}

    for name, value in options.items():
        if name in ('key', 'startkey', 'endkey'):
            value = json_encoder.encode(value)
        retval[name] = value
    return retval

def as_list(function):
    """
    Convert to list a generator function.
    """
    @functools.wraps(function)
    def _decorator(*args, **kwargs):
        return list(function(*args, **kwargs))
    return _decorator

def as_dict(function):
    """
    Convert to list a generator function.
    """
    @functools.wraps(function)
    def _decorator(*args, **kwargs):
        return dict(list(function(*args, **kwargs)))
    return _decorator

def force_bytes(data, encoding="utf-8"):
    if isinstance(data, string_type):
        data = data.encode(encoding)
    return data

def force_text(data, encoding="utf-8"):
    if isinstance(data, bytes_type):
        data = data.decode(encoding)
    return data

def int_to_bool(data):
    if data == 0:
        return True
    return False

@@ -34,25 +34,8 @@
 son%0A
-import functools%0A
 impo
@@ -611,32 +611,33 @@
 n URL.%0A%0A    %3E%3E%3E 
+_
 extract_credenti
@@ -719,32 +719,33 @@
 , None)%0A    %3E%3E%3E 
+_
 extract_credenti
@@ -859,16 +859,17 @@
     %3E%3E%3E 
+_
 extract_
@@ -905,32 +905,64 @@
 mple.com:secret@
+'%0A    ...                      '
 localhost:5984/_
@@ -1488,17 +1488,16 @@
 safe)%0A%0A%0A
-%0A
 def urlj
@@ -2649,16 +2649,17 @@
 etval)%0A%0A
+%0A
 def as_j
@@ -2860,17 +2860,16 @@
 se.text%0A
-%0A
     retu
@@ -2877,16 +2877,17 @@
 n None%0A%0A
+%0A
 def to_j
@@ -2924,16 +2924,17 @@
 s(doc)%0A%0A
+%0A
 def _pat
@@ -3197,16 +3197,17 @@
  name%5D%0A%0A
+%0A
 def _enc
@@ -3556,451 +3556,8 @@
 al%0A%0A
-def as_list(function):%0A    %22%22%22%0A    Convert to list a generator function.%0A    %22%22%22%0A    @functools.wraps(function)%0A    def _decorator(*args, **kwargs):%0A        return list(function(*args, **kwargs))%0A    return _decorator%0A%0Adef as_dict(function):%0A    %22%22%22%0A    Convert to list a generator function.%0A    %22%22%22%0A    @functools.wraps(function)%0A    def _decorator(*args, **kwargs):%0A        return dict(list(function(*args, **kwargs)))%0A    return _decorator%0A
 %0Adef
@@ -3682,24 +3682,25 @@
 eturn data%0A%0A
+%0A
 def force_te
@@ -3821,83 +3821,4 @@
 ata%0A
-%0Adef int_to_bool(data):%0A    if data == 0:%0A        return True%0A    return False%0A

Remove unused methods from pycouchdb/utils.py