# Path: iktomi/web/url_templates.py
# def urlquote(value):
#     if isinstance(value, six.integer_types):
#         value = six.text_type(value)
#     return quote(value.encode('utf-8'))


import six
    from urlparse import urlparse, parse_qs, unquote
    from urllib.parse import urlparse, parse_qs, unquote
from webob.multidict import MultiDict
from .url_templates import urlquote
from iktomi.utils.url import uri_to_iri_parts
# -*- coding: utf-8 -*-

__all__ = ['URL']

if six.PY2:
else:# pragma: no cover; we check coverage only in python2 part


def construct_url(path, query, host, port, scheme, fragment=None):
    query = ('?' + '&'.join('{}={}'.format(urlquote(k), urlquote(v))
                            for k, v in six.iteritems(query))
             if query else '')

    hash_part = ('#' + fragment) if fragment is not None else ''

    if host:
        host = host.encode('idna').decode('utf-8')
        port = ':' + port if port else ''
        return ''.join((scheme, '://', host, port, path, query, hash_part))
    else:
        return path + query + hash_part

if six.PY2:
    def _parse_qs(query):
        return sum([[(k.decode('utf-8', errors="replace"),
                      v.decode('utf-8', errors="replace"))

# Path: iktomi/web/url_templates.py
# def urlquote(value):
#     if isinstance(value, six.integer_types):
#         value = six.text_type(value)
#     return quote(value.encode('utf-8'))


# Path: iktomi/web/url.py
import six
    from urlparse import urlparse, parse_qs, unquote
    from urllib.parse import urlparse, parse_qs, unquote
from webob.multidict import MultiDict
from .url_templates import urlquote
from iktomi.utils.url import uri_to_iri_parts

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

__all__ = ['URL']

if six.PY2:
else:# pragma: no cover; we check coverage only in python2 part


def construct_url(path, query, host, port, scheme, fragment=None):
    query = ('?' + '&'.join('{}={}'.format(urlquote(k), urlquote(v))
                            for k, v in six.iteritems(query))
             if query else '')

    hash_part = ('#' + fragment) if fragment is not None else ''

    if host:
        host = host.encode('idna').decode('utf-8')
        port = ':' + port if port else ''
        return ''.join((scheme, '://', host, port, path, query, hash_part))
    else:
        return path + query + hash_part

if six.PY2:
    def _parse_qs(query):
        return sum([[(k.decode('utf-8', errors="replace"),
                      v.decode('utf-8', errors="replace"))

                     for v in values]