# Path: airy/utils/encoding.py
# def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
#     """
#     Returns a bytestring version of 's', encoded as specified in 'encoding'.
# 
#     If strings_only is True, don't convert (some) non-string-like objects.
#     """
#     if strings_only and isinstance(s, (types.NoneType, int)):
#         return s
#     if isinstance(s, Promise):
#         return unicode(s).encode(encoding, errors)
#     elif not isinstance(s, basestring):
#         try:
#             return str(s)
#         except UnicodeEncodeError:
#             if isinstance(s, Exception):
#                 # An Exception subclass containing non-ASCII data that doesn't
#                 # know how to print itself properly. We shouldn't raise a
#                 # further exception.
#                 return ' '.join([smart_str(arg, encoding, strings_only,
#                         errors) for arg in s])
#             return unicode(s).encode(encoding, errors)
#     elif isinstance(s, unicode):
#         return s.encode(encoding, errors)
#     elif s and encoding != 'utf-8':
#         return s.decode('utf-8', errors).encode(encoding, errors)
#     else:
#         return s
# 
# def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'):
#     """
#     Similar to smart_unicode, except that lazy instances are resolved to
#     strings, rather than kept as lazy objects.
# 
#     If strings_only is True, don't convert (some) non-string-like objects.
#     """
#     # Handle the common case first, saves 30-40% in performance when s
#     # is an instance of unicode. This function gets called often in that
#     # setting.
#     if isinstance(s, unicode):
#         return s
#     if strings_only and is_protected_type(s):
#         return s
#     try:
#         if not isinstance(s, basestring,):
#             if hasattr(s, '__unicode__'):
#                 s = unicode(s)
#             else:
#                 try:
#                     s = unicode(str(s), encoding, errors)
#                 except UnicodeEncodeError:
#                     if not isinstance(s, Exception):
#                         raise
#                     # If we get to here, the caller has passed in an Exception
#                     # subclass populated with non-ASCII data without special
#                     # handling to display as a string. We need to handle this
#                     # without raising a further exception. We do an
#                     # approximation to what the Exception's standard str()
#                     # output should be.
#                     s = ' '.join([force_unicode(arg, encoding, strings_only,
#                             errors) for arg in s])
#         elif not isinstance(s, unicode):
#             # Note: We use .decode() here, instead of unicode(s, encoding,
#             # errors), so that if s is a SafeString, it ends up being a
#             # SafeUnicode at the end.
#             s = s.decode(encoding, errors)
#     except UnicodeDecodeError, e:
#         if not isinstance(s, Exception):
#             raise DjangoUnicodeDecodeError(s, *e.args)
#         else:
#             # If we get to here, the caller has passed in an Exception
#             # subclass populated with non-ASCII bytestring data without a
#             # working unicode method. Try to handle this without raising a
#             # further exception by individually forcing the exception args
#             # to unicode.
#             s = ' '.join([force_unicode(arg, encoding, strings_only,
#                     errors) for arg in s])
#     return s
# 
# Path: airy/utils/functional.py
# def allow_lazy(func, *resultclasses):
#     """
#     A decorator that allows a function to be called with one or more lazy
#     arguments. If none of the args are lazy, the function is evaluated
#     immediately, otherwise a __proxy__ is returned that will evaluate the
#     function when needed.
#     """
#     def wrapper(*args, **kwargs):
#         for arg in list(args) + kwargs.values():
#             if isinstance(arg, Promise):
#                 break
#         else:
#             return func(*args, **kwargs)
#         return lazy(func, *resultclasses)(*args, **kwargs)
#     return wraps(func)(wrapper)


import calendar
import datetime
import re
import sys
import urllib
import urlparse
from email.Utils import formatdate
from airy.utils.encoding import smart_str, force_unicode
from airy.utils.functional import allow_lazy

MONTHS = 'jan feb mar apr may jun jul aug sep oct nov dec'.split()
__D = r'(?P<day>\d{2})'
__D2 = r'(?P<day>[ \d]\d)'
__M = r'(?P<mon>\w{3})'
__Y = r'(?P<year>\d{4})'
__Y2 = r'(?P<year>\d{2})'
__T = r'(?P<hour>\d{2}):(?P<min>\d{2}):(?P<sec>\d{2})'
RFC1123_DATE = re.compile(r'^\w{3}, %s %s %s %s GMT$' % (__D, __M, __Y, __T))
RFC850_DATE = re.compile(r'^\w{6,9}, %s-%s-%s %s GMT$' % (__D, __M, __Y2, __T))
ASCTIME_DATE = re.compile(r'^\w{3} %s %s %s %s$' % (__M, __D2, __T, __Y))

def urlquote(url, safe='/'):
    """
    A version of Python's urllib.quote() function that can operate on unicode
    strings. The url is first UTF-8 encoded before quoting. The returned string
    can safely be used as part of an argument to a subsequent iri_to_uri() call
    without double-quoting occurring.
    """
    return force_unicode(urllib.quote(smart_str(url), smart_str(safe)))

urlquote = allow_lazy(urlquote, unicode)

def urlquote_plus(url, safe=''):
    """
    A version of Python's urllib.quote_plus() function that can operate on
    unicode strings. The url is first UTF-8 encoded before quoting. The
    returned string can safely be used as part of an argument to a subsequent
    iri_to_uri() call without double-quoting occurring.
    """

# Path: airy/utils/encoding.py
# def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
#     """
#     Returns a bytestring version of 's', encoded as specified in 'encoding'.
# 
#     If strings_only is True, don't convert (some) non-string-like objects.
#     """
#     if strings_only and isinstance(s, (types.NoneType, int)):
#         return s
#     if isinstance(s, Promise):
#         return unicode(s).encode(encoding, errors)
#     elif not isinstance(s, basestring):
#         try:
#             return str(s)
#         except UnicodeEncodeError:
#             if isinstance(s, Exception):
#                 # An Exception subclass containing non-ASCII data that doesn't
#                 # know how to print itself properly. We shouldn't raise a
#                 # further exception.
#                 return ' '.join([smart_str(arg, encoding, strings_only,
#                         errors) for arg in s])
#             return unicode(s).encode(encoding, errors)
#     elif isinstance(s, unicode):
#         return s.encode(encoding, errors)
#     elif s and encoding != 'utf-8':
#         return s.decode('utf-8', errors).encode(encoding, errors)
#     else:
#         return s
# 
# def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'):
#     """
#     Similar to smart_unicode, except that lazy instances are resolved to
#     strings, rather than kept as lazy objects.
# 
#     If strings_only is True, don't convert (some) non-string-like objects.
#     """
#     # Handle the common case first, saves 30-40% in performance when s
#     # is an instance of unicode. This function gets called often in that
#     # setting.
#     if isinstance(s, unicode):
#         return s
#     if strings_only and is_protected_type(s):
#         return s
#     try:
#         if not isinstance(s, basestring,):
#             if hasattr(s, '__unicode__'):
#                 s = unicode(s)
#             else:
#                 try:
#                     s = unicode(str(s), encoding, errors)
#                 except UnicodeEncodeError:
#                     if not isinstance(s, Exception):
#                         raise
#                     # If we get to here, the caller has passed in an Exception
#                     # subclass populated with non-ASCII data without special
#                     # handling to display as a string. We need to handle this
#                     # without raising a further exception. We do an
#                     # approximation to what the Exception's standard str()
#                     # output should be.
#                     s = ' '.join([force_unicode(arg, encoding, strings_only,
#                             errors) for arg in s])
#         elif not isinstance(s, unicode):
#             # Note: We use .decode() here, instead of unicode(s, encoding,
#             # errors), so that if s is a SafeString, it ends up being a
#             # SafeUnicode at the end.
#             s = s.decode(encoding, errors)
#     except UnicodeDecodeError, e:
#         if not isinstance(s, Exception):
#             raise DjangoUnicodeDecodeError(s, *e.args)
#         else:
#             # If we get to here, the caller has passed in an Exception
#             # subclass populated with non-ASCII bytestring data without a
#             # working unicode method. Try to handle this without raising a
#             # further exception by individually forcing the exception args
#             # to unicode.
#             s = ' '.join([force_unicode(arg, encoding, strings_only,
#                     errors) for arg in s])
#     return s
# 
# Path: airy/utils/functional.py
# def allow_lazy(func, *resultclasses):
#     """
#     A decorator that allows a function to be called with one or more lazy
#     arguments. If none of the args are lazy, the function is evaluated
#     immediately, otherwise a __proxy__ is returned that will evaluate the
#     function when needed.
#     """
#     def wrapper(*args, **kwargs):
#         for arg in list(args) + kwargs.values():
#             if isinstance(arg, Promise):
#                 break
#         else:
#             return func(*args, **kwargs)
#         return lazy(func, *resultclasses)(*args, **kwargs)
#     return wraps(func)(wrapper)


# Path: airy/utils/http.py
import calendar
import datetime
import re
import sys
import urllib
import urlparse
from email.Utils import formatdate
from airy.utils.encoding import smart_str, force_unicode
from airy.utils.functional import allow_lazy


MONTHS = 'jan feb mar apr may jun jul aug sep oct nov dec'.split()
__D = r'(?P<day>\d{2})'
__D2 = r'(?P<day>[ \d]\d)'
__M = r'(?P<mon>\w{3})'
__Y = r'(?P<year>\d{4})'
__Y2 = r'(?P<year>\d{2})'
__T = r'(?P<hour>\d{2}):(?P<min>\d{2}):(?P<sec>\d{2})'
RFC1123_DATE = re.compile(r'^\w{3}, %s %s %s %s GMT$' % (__D, __M, __Y, __T))
RFC850_DATE = re.compile(r'^\w{6,9}, %s-%s-%s %s GMT$' % (__D, __M, __Y2, __T))
ASCTIME_DATE = re.compile(r'^\w{3} %s %s %s %s$' % (__M, __D2, __T, __Y))

def urlquote(url, safe='/'):
    """
    A version of Python's urllib.quote() function that can operate on unicode
    strings. The url is first UTF-8 encoded before quoting. The returned string
    can safely be used as part of an argument to a subsequent iri_to_uri() call
    without double-quoting occurring.
    """
    return force_unicode(urllib.quote(smart_str(url), smart_str(safe)))

urlquote = allow_lazy(urlquote, unicode)

def urlquote_plus(url, safe=''):
    """
    A version of Python's urllib.quote_plus() function that can operate on
    unicode strings. The url is first UTF-8 encoded before quoting. The
    returned string can safely be used as part of an argument to a subsequent
    iri_to_uri() call without double-quoting occurring.
    """

    return force_unicode(urllib.quote_plus(smart_str(url), smart_str(safe)))