# Path: pyoauth/_compat.py
# def unquote_plus(v):
# 
# Path: pyoauth/constants.py
# SYMBOL_QUESTION_MARK = b("?")
# 
# SYMBOL_AMPERSAND = b("&")
# 
# SYMBOL_EQUAL = b("=")
# 
# OAUTH_PARAM_PREFIX = "oauth_"
# 
# OAUTH_VALUE_CALLBACK_OOB = b("oob")
# 
# OAUTH_PARAM_CONSUMER_SECRET = "oauth_consumer_secret"
# 
# OAUTH_PARAM_TOKEN_SECRET = "oauth_token_secret"
# 
# SYMBOL_EMPTY_BYTES = b("")
# 
# Path: pyoauth/error.py
# class InvalidQueryParametersError(OAuthError):
#     """
#     Raised when a query parameter is invalid.
#     """
#     pass
# 
# class InsecureOAuthParametersError(OAuthError):
#     """
#     Raised when an OAuth confidential parameter is passed into protocol
#     parameters.
#     """
#     pass
# 
# class InvalidOAuthParametersError(OAuthError):
#     """
#     Raised when invalid protocol parameters are detected.
#     """
#     pass
# 
# class InsecureOAuthUrlError(OAuthError):
#     """
#     Raised when an insecure (non-HTTPS) URL is detected for requesting OAuth
#     credentials.
#     """
#     pass
# 
# class InvalidUrlError(OAuthError):
#     """
#     Raised when a specified URL is invalid for consumption by a routine.
#     """
#     pass


import logging
from mom.builtins import is_sequence, bytes, is_bytes_or_unicode, is_bytes
from mom.codec.text import utf8_encode_if_unicode, \
    utf8_encode, utf8_decode_if_bytes
from mom.functional import select_dict, map_dict
from mom.builtins import b
from pyoauth._compat import urlparse, urlunparse, parse_qs as _parse_qs, \
    quote, \
    unquote_plus
from pyoauth.constants import SYMBOL_QUESTION_MARK, \
    SYMBOL_AMPERSAND, SYMBOL_EQUAL, OAUTH_PARAM_PREFIX, \
    OAUTH_VALUE_CALLBACK_OOB, OAUTH_PARAM_CONSUMER_SECRET, \
    OAUTH_PARAM_TOKEN_SECRET, SYMBOL_EMPTY_BYTES
from pyoauth.error import InvalidQueryParametersError, \
    InsecureOAuthParametersError, \
    InvalidOAuthParametersError, \
    InsecureOAuthUrlError, \
    InvalidUrlError
    dictionary or given a parameter dictionary, un-flattens it.

    Example::

        dict(a=1, b=[1, 2], c="")   ->   dict(a[1], b=[1, 2], c=[""])
        a=1&b=1&b=2&c=              ->   dict(a[1], b=[1, 2], c=[""])

    :param query:
        A query parameter dictionary or a query string.
        If this argument is ``None`` an empty dictionary will be returned.
        Any other value will raise a
        :class:`pyoauth.errors.InvalidQueryParametersError` exception.
    :returns:
        An un-flattened query parameter dictionary.
    """
    if is_bytes_or_unicode(query):
        return parse_qs(query)
    elif isinstance(query, dict):
        # Un-flatten the dictionary.
        def _choose(key, value):
            if not isinstance(value, list) and not isinstance(value, tuple):
                return key, [value]
            else:
                return key, list(value)
        return map_dict(_choose, query)
        # Alternative, but slower:
        #return parse_qs(urlencode_s(query))
    elif query is None:
        return {}
    else:

# Path: pyoauth/_compat.py
# def unquote_plus(v):
# 
# Path: pyoauth/constants.py
# SYMBOL_QUESTION_MARK = b("?")
# 
# SYMBOL_AMPERSAND = b("&")
# 
# SYMBOL_EQUAL = b("=")
# 
# OAUTH_PARAM_PREFIX = "oauth_"
# 
# OAUTH_VALUE_CALLBACK_OOB = b("oob")
# 
# OAUTH_PARAM_CONSUMER_SECRET = "oauth_consumer_secret"
# 
# OAUTH_PARAM_TOKEN_SECRET = "oauth_token_secret"
# 
# SYMBOL_EMPTY_BYTES = b("")
# 
# Path: pyoauth/error.py
# class InvalidQueryParametersError(OAuthError):
#     """
#     Raised when a query parameter is invalid.
#     """
#     pass
# 
# class InsecureOAuthParametersError(OAuthError):
#     """
#     Raised when an OAuth confidential parameter is passed into protocol
#     parameters.
#     """
#     pass
# 
# class InvalidOAuthParametersError(OAuthError):
#     """
#     Raised when invalid protocol parameters are detected.
#     """
#     pass
# 
# class InsecureOAuthUrlError(OAuthError):
#     """
#     Raised when an insecure (non-HTTPS) URL is detected for requesting OAuth
#     credentials.
#     """
#     pass
# 
# class InvalidUrlError(OAuthError):
#     """
#     Raised when a specified URL is invalid for consumption by a routine.
#     """
#     pass


# Path: pyoauth/url.py
import logging
from mom.builtins import is_sequence, bytes, is_bytes_or_unicode, is_bytes
from mom.codec.text import utf8_encode_if_unicode, \
    utf8_encode, utf8_decode_if_bytes
from mom.functional import select_dict, map_dict
from mom.builtins import b
from pyoauth._compat import urlparse, urlunparse, parse_qs as _parse_qs, \
    quote, \
    unquote_plus
from pyoauth.constants import SYMBOL_QUESTION_MARK, \
    SYMBOL_AMPERSAND, SYMBOL_EQUAL, OAUTH_PARAM_PREFIX, \
    OAUTH_VALUE_CALLBACK_OOB, OAUTH_PARAM_CONSUMER_SECRET, \
    OAUTH_PARAM_TOKEN_SECRET, SYMBOL_EMPTY_BYTES
from pyoauth.error import InvalidQueryParametersError, \
    InsecureOAuthParametersError, \
    InvalidOAuthParametersError, \
    InsecureOAuthUrlError, \
    InvalidUrlError

    dictionary or given a parameter dictionary, un-flattens it.

    Example::

        dict(a=1, b=[1, 2], c="")   ->   dict(a[1], b=[1, 2], c=[""])
        a=1&b=1&b=2&c=              ->   dict(a[1], b=[1, 2], c=[""])

    :param query:
        A query parameter dictionary or a query string.
        If this argument is ``None`` an empty dictionary will be returned.
        Any other value will raise a
        :class:`pyoauth.errors.InvalidQueryParametersError` exception.
    :returns:
        An un-flattened query parameter dictionary.
    """
    if is_bytes_or_unicode(query):
        return parse_qs(query)
    elif isinstance(query, dict):
        # Un-flatten the dictionary.
        def _choose(key, value):
            if not isinstance(value, list) and not isinstance(value, tuple):
                return key, [value]
            else:
                return key, list(value)
        return map_dict(_choose, query)
        # Alternative, but slower:
        #return parse_qs(urlencode_s(query))
    elif query is None:
        return {}
    else:

        raise InvalidQueryParametersError(