From 9bc371ec4e98de77a6a81f300154a67c61133b9e Mon Sep 17 00:00:00 2001 From: Marko Toplak Date: Tue, 13 Feb 2018 13:32:40 +0100 Subject: [PATCH] Settings for HTTP and HTTPS proxies in the canvas --- Orange/canvas/__main__.py | 31 +++++++++++++++++++++------ Orange/canvas/application/settings.py | 20 +++++++++++++++++ Orange/canvas/config.py | 4 ++++ 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/Orange/canvas/__main__.py b/Orange/canvas/__main__.py index 27d02904cf8..bd064701a64 100644 --- a/Orange/canvas/__main__.py +++ b/Orange/canvas/__main__.py @@ -52,6 +52,9 @@ pyqtgraph.setConfigOption("exitCleanup", False) +default_proxies = None + + def fix_osx_10_9_private_font(): # Fix fonts on Os X (QTBUG 47206, 40833, 32789) if sys.platform == "darwin": @@ -88,10 +91,26 @@ def fix_win_pythonw_std_stream(): def fix_set_proxy_env(): """ Set http_proxy/https_proxy environment variables (for requests, pip, ...) - from system settings on OS X and from registry on Windos. On unix, no-op. + from user-specified settings or, if none, from system settings on OS X + and from registry on Windos. """ - for scheme, proxy in getproxies().items(): - os.environ[scheme + '_proxy'] = proxy + # save default proxies so that setting can be reset + global default_proxies + if default_proxies is None: + default_proxies = getproxies() # can also read windows and macos settings + + settings = QSettings() + proxies = getproxies() + for scheme in set(["http", "https"]) | set(proxies): + from_settings = settings.value("network/" + scheme + "-proxy", "", type=str) + from_default = default_proxies.get(scheme, "") + env_scheme = scheme + '_proxy' + if from_settings: + os.environ[env_scheme] = from_settings + elif from_default: + os.environ[env_scheme] = from_default # crucial for windows/macos support + else: + os.environ.pop(env_scheme, "") def make_sql_logger(level=logging.INFO): @@ -247,9 +266,6 @@ def main(argv=None): # Try to fix fonts on OSX Mavericks fix_osx_10_9_private_font() - # Set http_proxy environment variable(s) for some clients - fix_set_proxy_env() - # File handler should always be at least INFO level so we need # the application root level to be at least at INFO. root_level = min(levels[options.log_level], logging.INFO) @@ -313,6 +329,9 @@ def main(argv=None): config.widget_settings_dir(), ignore_errors=True) + # Set http_proxy environment variables, after (potentially) clearing settings + fix_set_proxy_env() + file_handler = logging.FileHandler( filename=os.path.join(config.log_dir(), "canvas.log"), mode="w" diff --git a/Orange/canvas/application/settings.py b/Orange/canvas/application/settings.py index 6a2bb26d0aa..2a089b58d79 100644 --- a/Orange/canvas/application/settings.py +++ b/Orange/canvas/application/settings.py @@ -26,6 +26,11 @@ log = logging.getLogger(__name__) +def refresh_proxies(): + from Orange.canvas.__main__ import fix_set_proxy_env + fix_set_proxy_env() + + class UserDefaultsPropertyBinding(AbstractBoundProperty): """ A Property binding for a setting in a @@ -399,6 +404,20 @@ def __setupUi(self): tab.setLayout(form) + # Network Tab + tab = QWidget() + self.addTab(tab, self.tr("Network"), + toolTip="Settings related to networking") + + form = QFormLayout() + line_edit_http_proxy = QLineEdit() + self.bind(line_edit_http_proxy, "text", "network/http-proxy") + form.addRow("HTTP proxy:", line_edit_http_proxy) + line_edit_https_proxy = QLineEdit() + self.bind(line_edit_https_proxy, "text", "network/https-proxy") + form.addRow("HTTPS proxy:", line_edit_https_proxy) + tab.setLayout(form) + if self.__macUnified: # Need some sensible size otherwise mac unified toolbar 'takes' # the space that should be used for layout of the contents @@ -461,6 +480,7 @@ def exec_(self): self.show() status = self.__loop.exec_() self.__loop = None + refresh_proxies() return status def hideEvent(self, event): diff --git a/Orange/canvas/config.py b/Orange/canvas/config.py index 332d34deb6e..2a65d14f20d 100644 --- a/Orange/canvas/config.py +++ b/Orange/canvas/config.py @@ -127,6 +127,10 @@ def init(): ("add-ons/pip-install-arguments", str, '', 'Arguments to pass to "pip install" when installing add-ons.'), + + ("network/http-proxy", str, '', 'HTTP proxy.'), + + ("network/https-proxy", str, '', 'HTTPS proxy.'), ] spec = [config_slot(*t) for t in spec]