# Path: restkit/util.py
# def to_bytestring(s):
#     if not isinstance(s, basestring):
#         raise TypeError("value should be a str or unicode")
# 
#     if isinstance(s, unicode):
#         return s.encode('utf-8')
#     return s
# 
# def url_quote(s, charset='utf-8', safe='/:'):
#     """URL encode a single string with a given encoding."""
#     if isinstance(s, unicode):
#         s = s.encode(charset)
#     elif not isinstance(s, str):
#         s = str(s)
#     return urllib.quote(s, safe=safe)
# 
# def url_encode(obj, charset="utf8", encode_keys=False):
#     items = []
#     if isinstance(obj, dict):
#         for k, v in list(obj.items()):
#             items.append((k, v))
#     else:
#         items = list(items)
#         
#     tmp = []
#     for k, v in items:
#         if encode_keys: 
#             k = encode(k, charset)
#         
#         if not isinstance(v, (tuple, list)):
#             v = [v]
#             
#         for v1 in v:
#             if v1 is None:
#                 v1 = ''
#             elif callable(v1):
#                 v1 = encode(v1(), charset)
#             else:
#                 v1 = encode(v1, charset)
#             tmp.append('%s=%s' % (urllib.quote(k), urllib.quote_plus(v1)))
#     return '&'.join(tmp)


import mimetypes
import os
import re
import urllib
from restkit.util import to_bytestring, url_quote, url_encode
# See the NOTICE for more information.





MIME_BOUNDARY = 'END_OF_PART'
CRLF = '\r\n'

def form_encode(obj, charset="utf8"):
    encoded = url_encode(obj, charset=charset)
    return to_bytestring(encoded)


class BoundaryItem(object):
    def __init__(self, name, value, fname=None, filetype=None, filesize=None,
                 quote=url_quote):
        self.quote = quote
        self.name = quote(name)
        if value is not None and not hasattr(value, 'read'):
            value = self.encode_unreadable_value(value)
            self.size = len(value)
        self.value = value
        if fname is not None:
            if isinstance(fname, unicode):
                fname = fname.encode("utf-8").encode("string_escape").replace('"', '\\"')
            else:
                fname = fname.encode("string_escape").replace('"', '\\"')
        self.fname = fname
        if filetype is not None:

# Path: restkit/util.py
# def to_bytestring(s):
#     if not isinstance(s, basestring):
#         raise TypeError("value should be a str or unicode")
# 
#     if isinstance(s, unicode):
#         return s.encode('utf-8')
#     return s
# 
# def url_quote(s, charset='utf-8', safe='/:'):
#     """URL encode a single string with a given encoding."""
#     if isinstance(s, unicode):
#         s = s.encode(charset)
#     elif not isinstance(s, str):
#         s = str(s)
#     return urllib.quote(s, safe=safe)
# 
# def url_encode(obj, charset="utf8", encode_keys=False):
#     items = []
#     if isinstance(obj, dict):
#         for k, v in list(obj.items()):
#             items.append((k, v))
#     else:
#         items = list(items)
#         
#     tmp = []
#     for k, v in items:
#         if encode_keys: 
#             k = encode(k, charset)
#         
#         if not isinstance(v, (tuple, list)):
#             v = [v]
#             
#         for v1 in v:
#             if v1 is None:
#                 v1 = ''
#             elif callable(v1):
#                 v1 = encode(v1(), charset)
#             else:
#                 v1 = encode(v1, charset)
#             tmp.append('%s=%s' % (urllib.quote(k), urllib.quote_plus(v1)))
#     return '&'.join(tmp)


# Path: restkit/forms.py
import mimetypes
import os
import re
import urllib
from restkit.util import to_bytestring, url_quote, url_encode

# See the NOTICE for more information.





MIME_BOUNDARY = 'END_OF_PART'
CRLF = '\r\n'

def form_encode(obj, charset="utf8"):
    encoded = url_encode(obj, charset=charset)
    return to_bytestring(encoded)


class BoundaryItem(object):
    def __init__(self, name, value, fname=None, filetype=None, filesize=None,
                 quote=url_quote):
        self.quote = quote
        self.name = quote(name)
        if value is not None and not hasattr(value, 'read'):
            value = self.encode_unreadable_value(value)
            self.size = len(value)
        self.value = value
        if fname is not None:
            if isinstance(fname, unicode):
                fname = fname.encode("utf-8").encode("string_escape").replace('"', '\\"')
            else:
                fname = fname.encode("string_escape").replace('"', '\\"')
        self.fname = fname
        if filetype is not None:

            filetype = to_bytestring(filetype)