diff options
| author | Joshua Harlow <harlowja@yahoo-inc.com> | 2015-05-01 12:44:17 -0700 |
|---|---|---|
| committer | Joshua Harlow <harlowja@gmail.com> | 2015-05-02 10:31:40 -0700 |
| commit | 7c3fdcc8c4bdfd156cca83e9f840aed0c8eb37ad (patch) | |
| tree | e6c12d3acfd2ebd805cbefa2f5ec87dcaee74d17 /taskflow/utils/misc.py | |
| parent | 1fc59837ebddacc530536008ec4fd71e7dc3ec88 (diff) | |
| download | taskflow-7c3fdcc8c4bdfd156cca83e9f840aed0c8eb37ad.tar.gz | |
Small refactoring of 'merge_uri' utility function
Perform some small adjustments/cleanups and add some
unit tests to ensure this function keeps operating as
expected.
Change-Id: I496bd6844072f57624de31fc7ddb0362f163cc53
Diffstat (limited to 'taskflow/utils/misc.py')
| -rw-r--r-- | taskflow/utils/misc.py | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/taskflow/utils/misc.py b/taskflow/utils/misc.py index ee5fb39..ffd487e 100644 --- a/taskflow/utils/misc.py +++ b/taskflow/utils/misc.py @@ -124,21 +124,29 @@ def reverse_enumerate(items): def merge_uri(uri, conf): """Merges a parsed uri into the given configuration dictionary. - Merges the username, password, hostname, and query params of a uri into - the given configuration (it does not overwrite the configuration keys if - they already exist) and returns the adjusted configuration. + Merges the username, password, hostname, port, and query parameters of + a URI into the given configuration dictionary (it does **not** overwrite + existing configuration keys if they already exist) and returns the merged + configuration. NOTE(harlowja): does not merge the path, scheme or fragment. """ - for (k, v) in [('username', uri.username), ('password', uri.password)]: - if not v: - continue - conf.setdefault(k, v) - if uri.hostname: - hostname = uri.hostname - if uri.port is not None: - hostname += ":%s" % (uri.port) - conf.setdefault('hostname', hostname) + uri_port = uri.port + specials = [ + ('username', uri.username, lambda v: bool(v)), + ('password', uri.password, lambda v: bool(v)), + # NOTE(harlowja): A different check function is used since 0 is + # false (when bool(v) is applied), and that is a valid port... + ('port', uri_port, lambda v: v is not None), + ] + hostname = uri.hostname + if hostname: + if uri_port is not None: + hostname += ":%s" % (uri_port) + specials.append(('hostname', hostname, lambda v: bool(v))) + for (k, v, is_not_empty_value_func) in specials: + if is_not_empty_value_func(v): + conf.setdefault(k, v) for (k, v) in six.iteritems(uri.params()): conf.setdefault(k, v) return conf |
