# Path: src/cloudant/_2to3.py
# PY2 = sys.version_info[0] < 3
# ENCODING = 'utf-8'
# NONETYPE = type(None)
# STRTYPE = basestring if PY2 else str
# UNITYPE = unicode if PY2 else str
# LONGTYPE = long if PY2 else int
# UNICHR = unichr if PY2 else chr
#     def iteritems_(adict):
#     def next_(itr):
#     def iteritems_(adict):
#     def next_(itr):
# def bytes_(astr):
# def unicode_(astr):
# 
# Path: src/cloudant/_common_util.py
# def response_to_json_dict(response, **kwargs):
#     """
#     Standard place to convert responses to JSON.
# 
#     :param response: requests response object
#     :param **kwargs: arguments accepted by json.loads
# 
#     :returns: dict of JSON response
#     """
#     if response.encoding is None:
#         response.encoding = 'utf-8'
#     return json.loads(response.text, **kwargs)
# 
# def assert_document_type_id(docid):
#     """
#     Validate the document ID.  Raises an error if the ID is an `_` prefixed name
#     that isn't either `_design` or `_local`.
#     :return:
#     """
#     invalid = False
#     if docid.startswith('_'):
#         if docid.startswith(DESIGN_PREFIX) and DESIGN_PREFIX != docid:
#             invalid = False
#         elif docid.startswith(LOCAL_PREFIX) and LOCAL_PREFIX != docid:
#             invalid = False
#         else:
#             invalid = True
#     if invalid:
#         raise CloudantArgumentError(137, docid)
# 
# def assert_attachment_name(attname):
#     """
#     Validate the document attachment's name.  Raises an error if `_` prefixed name exists.
#     :return:
#     """
#     if attname.startswith('_'):
#         raise CloudantArgumentError(138, attname)
# 
# Path: src/cloudant/error.py
# class CloudantDocumentException(CloudantException):
#     """
#     Provides a way to issue Cloudant library document specific exceptions.
# 
#     :param int code: A code value used to identify the document exception.
#     :param args: A list of arguments used to format the exception message.
#     """
#     def __init__(self, code=100, *args):
#         try:
#             msg = DOCUMENT[code].format(*args)
#         except (KeyError, IndexError):
#             code = 100
#             msg = DOCUMENT[code]
#         super(CloudantDocumentException, self).__init__(msg, code)


import json
import requests
from requests.exceptions import HTTPError
from ._2to3 import url_quote, url_quote_plus
from ._common_util import response_to_json_dict, assert_document_type_id, assert_attachment_name
from .error import CloudantDocumentException
                attachment_type = 'json'
            else:
                attachment_type = 'binary'

        if write_to is not None:
            if attachment_type in ('text', 'json'):
                write_to.write(resp.text)
            else:
                write_to.write(resp.content)
        if attachment_type == 'text':
            return resp.text
        if attachment_type == 'json':
            return response_to_json_dict(resp)

        return resp.content

    def delete_attachment(self, attachment, headers=None):
        """
        Removes an attachment from a remote document and refreshes the locally
        cached document object.

        :param str attachment: Attachment file name used to identify the
            attachment.
        :param dict headers: Optional, additional headers to be sent
            with request.

        :returns: Attachment deletion status in JSON format
        """
        # need latest rev
        self.fetch()

# Path: src/cloudant/_2to3.py
# PY2 = sys.version_info[0] < 3
# ENCODING = 'utf-8'
# NONETYPE = type(None)
# STRTYPE = basestring if PY2 else str
# UNITYPE = unicode if PY2 else str
# LONGTYPE = long if PY2 else int
# UNICHR = unichr if PY2 else chr
#     def iteritems_(adict):
#     def next_(itr):
#     def iteritems_(adict):
#     def next_(itr):
# def bytes_(astr):
# def unicode_(astr):
# 
# Path: src/cloudant/_common_util.py
# def response_to_json_dict(response, **kwargs):
#     """
#     Standard place to convert responses to JSON.
# 
#     :param response: requests response object
#     :param **kwargs: arguments accepted by json.loads
# 
#     :returns: dict of JSON response
#     """
#     if response.encoding is None:
#         response.encoding = 'utf-8'
#     return json.loads(response.text, **kwargs)
# 
# def assert_document_type_id(docid):
#     """
#     Validate the document ID.  Raises an error if the ID is an `_` prefixed name
#     that isn't either `_design` or `_local`.
#     :return:
#     """
#     invalid = False
#     if docid.startswith('_'):
#         if docid.startswith(DESIGN_PREFIX) and DESIGN_PREFIX != docid:
#             invalid = False
#         elif docid.startswith(LOCAL_PREFIX) and LOCAL_PREFIX != docid:
#             invalid = False
#         else:
#             invalid = True
#     if invalid:
#         raise CloudantArgumentError(137, docid)
# 
# def assert_attachment_name(attname):
#     """
#     Validate the document attachment's name.  Raises an error if `_` prefixed name exists.
#     :return:
#     """
#     if attname.startswith('_'):
#         raise CloudantArgumentError(138, attname)
# 
# Path: src/cloudant/error.py
# class CloudantDocumentException(CloudantException):
#     """
#     Provides a way to issue Cloudant library document specific exceptions.
# 
#     :param int code: A code value used to identify the document exception.
#     :param args: A list of arguments used to format the exception message.
#     """
#     def __init__(self, code=100, *args):
#         try:
#             msg = DOCUMENT[code].format(*args)
#         except (KeyError, IndexError):
#             code = 100
#             msg = DOCUMENT[code]
#         super(CloudantDocumentException, self).__init__(msg, code)


# Path: src/cloudant/document.py
import json
import requests
from requests.exceptions import HTTPError
from ._2to3 import url_quote, url_quote_plus
from ._common_util import response_to_json_dict, assert_document_type_id, assert_attachment_name
from .error import CloudantDocumentException

                attachment_type = 'json'
            else:
                attachment_type = 'binary'

        if write_to is not None:
            if attachment_type in ('text', 'json'):
                write_to.write(resp.text)
            else:
                write_to.write(resp.content)
        if attachment_type == 'text':
            return resp.text
        if attachment_type == 'json':
            return response_to_json_dict(resp)

        return resp.content

    def delete_attachment(self, attachment, headers=None):
        """
        Removes an attachment from a remote document and refreshes the locally
        cached document object.

        :param str attachment: Attachment file name used to identify the
            attachment.
        :param dict headers: Optional, additional headers to be sent
            with request.

        :returns: Attachment deletion status in JSON format
        """
        # need latest rev
        self.fetch()

        assert_attachment_name(attachment)