1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
# -*- coding: utf-8 -*-
#
# Copyright (C) 2009 Red Hat, Inc.
# This file is part of python-fedora
#
# python-fedora is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# python-fedora is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with python-fedora; if not, see <http://www.gnu.org/licenses/>
#
'''
Functions to manipulate urls.
.. versionadded:: 0.3.17
.. moduleauthor:: John (J5) Palmieri <johnp@redhat.com>
.. moduleauthor:: Toshio Kuratomi <tkuratom@redhat.com>
'''
from kitchen.iterutils import isiterable
from six.moves.urllib.parse import parse_qs, urlencode, urlparse, urlunparse
def update_qs(uri, new_params, overwrite=True):
'''Helper function for updating query string values.
Similar to calling update on a dictionary except we modify the query
string of the uri instead of another dictionary.
:arg uri: URI to modify
:arg new_params: Dict of new query parameters to add.
:kwarg overwrite: If True (default), any old query parameter with the same
name as a new query parameter will be overwritten. If False, the new
query parameters will be appended to a list with the old parameters at
the start of the list.
:returns: URI with the new parameters added.
'''
loc = list(urlparse(uri))
query_dict = parse_qs(loc[4])
if overwrite:
# Overwrite any existing values with the new values
query_dict.update(new_params)
else:
# Add new values in addition to the existing parameters
# For every new entry
for key in new_params:
# If the entry already is present
if key in query_dict:
if isiterable(new_params[key]):
# If the new entry is a non-string iterable
try:
# Try to add the new values to the existing entry
query_dict[key].extend(new_params[key])
except AttributeError:
# Existing value wasn't a list, make it one
query_dict[key] = [query_dict[key], new_params[key]]
else:
# The new entry is a scalar, so try to append it
try:
query_dict[key].append(new_params[key])
except AttributeError:
# Existing value wasn't a list, make it one
query_dict[key] = [query_dict[key], new_params[key]]
else:
# No previous entry, just set to the new entry
query_dict[key] = new_params[key]
# seems that we have to sanitize a bit here
query_list = []
for key, value in query_dict.items():
if isiterable(value):
for item in value:
query_list.append((key, item))
continue
query_list.append((key, value))
loc[4] = urlencode(query_list)
return urlunparse(loc)
__all__ = ['update_qs']
|