From ab90c583ec85608291275f60e6cf9911d3971a92 Mon Sep 17 00:00:00 2001 From: hans-permana Date: Tue, 17 Apr 2018 11:54:15 +0200 Subject: [PATCH] Use only one variable (http_proxy) for proxy URL in conf.py. The value of this variable is then returned when get_config() is called. Part of #544 --- CHANGES.md | 2 ++ cate/conf/conf.py | 4 ++++ cate/conf/template.py | 8 +++----- cate/core/common.py | 25 ++++++++++++++----------- cate/webapi/websocket.py | 8 ++++++-- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 9703eab11..a98860cff 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,6 +5,8 @@ introduced new error type `cate.core.types.ValidationError` for special treatment in the GUI. * Make Cate HTTP User-Agent distinguishable [#510](https://github.com/CCI-Tools/cate/issues/510). * Fixed broken WebAPI invocation from CLI. +* Use only one variable (http_proxy) for proxy URL in conf.py. The value of this variable is then returned when + get_config() is called. [#544](https://github.com/CCI-Tools/cate/issues/544) ## Version 2.0.0.dev8 diff --git a/cate/conf/conf.py b/cate/conf/conf.py index 454e34e78..deafe0bb3 100644 --- a/cate/conf/conf.py +++ b/cate/conf/conf.py @@ -91,6 +91,10 @@ def get_default_res_pattern() -> str: return default_res_pattern +def get_http_proxy() -> str: + return get_config().get('http_proxy') + + def get_variable_display_settings(var_name: str) -> Optional[Dict[str, Any]]: """ Get the global variable display settings which is a combination of defaults. diff --git a/cate/conf/template.py b/cate/conf/template.py index cd20c577b..faf2c6001 100644 --- a/cate/conf/template.py +++ b/cate/conf/template.py @@ -29,16 +29,14 @@ # This prefix is used only if no specific prefix is defined for a given operation. # default_res_pattern = 'res_{index}' -# User defined HTTP proxy settings, will replace one stored in System environment variable 'http_proxy' and/or -# optionally 'https_proxy' +# User defined HTTP proxy settings, will replace one stored in System environment variable 'http_proxy' # Accepted proxy details formats: # 'http://user:password@host:port' +# 'https://user:password@host:port' # 'http://host:port' +# 'https://host:port' # http_proxy = -# Uncomment and fill only if your Organization or Internet Service Provider provides proxy setting for HTTPS connections -# https_proxy = - # Include/exclude data sources (currently effective in Cate Desktop GUI only, not used by API, CLI). # # If 'included_data_sources' is a list, its entries are expected to be wildcard patterns for the identifiers of data diff --git a/cate/core/common.py b/cate/core/common.py index 4623662e3..214149dab 100644 --- a/cate/core/common.py +++ b/cate/core/common.py @@ -19,6 +19,9 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +from cate.conf import get_config_value + +import logging __author__ = "Chris Bernat (Telespazio VEGA UK Ltd)" @@ -28,22 +31,22 @@ def initialize_proxy(): Initialize user defined proxy settings, read proxy setting from config file. Populates value to 3rd-party libraries using proper environment variables. """ - from cate.conf import get_config_value from os import environ - config_key_http_proxy = 'http_proxy' - config_key_https_proxy = 'https_proxy' + http_proxy_url = get_config_value('http_proxy') - environ_key_http_proxy = 'http_proxy' - environ_key_https_proxy = 'https_proxy' + if not http_proxy_url: + log_invalid_http_url(http_proxy_url) + elif http_proxy_url.startswith('https'): + environ['https_proxy'] = http_proxy_url + elif http_proxy_url.startswith('http'): + environ['http_proxy'] = http_proxy_url + else: + log_invalid_http_url(http_proxy_url) - http_proxy_config = get_config_value(config_key_http_proxy) - if http_proxy_config: - environ[environ_key_http_proxy] = http_proxy_config - https_proxy_config = get_config_value(config_key_https_proxy) - if https_proxy_config: - environ[environ_key_https_proxy] = https_proxy_config +def log_invalid_http_url(http_proxy_url): + logging.warning('invalid proxy URL "' + str(http_proxy_url) + '"') def configure_user_agent(): diff --git a/cate/webapi/websocket.py b/cate/webapi/websocket.py index ebd32a709..75be00089 100644 --- a/cate/webapi/websocket.py +++ b/cate/webapi/websocket.py @@ -54,7 +54,8 @@ def __init__(self, workspace_manager: WorkspaceManager): def get_config(self) -> dict: return dict(data_stores_path=conf.get_data_stores_path(), use_workspace_imagery_cache=conf.get_use_workspace_imagery_cache(), - default_res_pattern=conf.get_default_res_pattern()) + default_res_pattern=conf.get_default_res_pattern(), + http_proxy=conf.get_http_proxy()) def set_config(self, config: dict) -> None: @@ -71,7 +72,10 @@ def set_config(self, config: dict) -> None: # Split into config file lines conf_lines = conf_text.split('\n') for key, value in config.items(): - new_entry = '%s = %s' % (key, repr(value)) + if value: + new_entry = '%s = %s' % (key, repr(value)) + else: + new_entry = '# %s =' % key # Try replacing existing code lines starting with key # Replace in reverse line order, because config files are interpreted top-down indices = list(range(len(conf_lines)))