def test_reg_errors_reraise(): with _patch_winreg_qve(side_effect=WindowsError()): assert not autoconfig_url_from_registry() with _patch_winreg_qve(side_effect=CalledProcessError(2, "foo")): with pytest.raises(CalledProcessError) as exinfo: autoconfig_url_from_registry() assert exinfo.value.returncode == 2
def collect_pac_urls(from_os_settings=True, from_dns=True, **kwargs): """ Get all the URLs that potentially yield a PAC file. :param bool from_os_settings: Look for a PAC URL from the OS settings. If a value is found and is a URL, it comes first in the returned list. Doesn't do anything on non-Windows or non-macOS/OSX platforms. :param bool from_dns: Assemble a list of PAC URL candidates using the WPAD protocol. :return: A list of URLs that should be tried in order. :rtype: list[str] """ # Deprecated in 0.8.2 from_registry = kwargs.get('from_registry') if from_registry is not None: import warnings warnings.warn( 'from_registry is deprecated, use from_os_settings instead.') from_os_settings = from_registry pac_urls = [] if from_os_settings: if ON_WINDOWS: url_or_path = autoconfig_url_from_registry() elif ON_DARWIN: url_or_path = autoconfig_url_from_preferences() else: url_or_path = None if url_or_path and (url_or_path.lower().startswith('http://') or url_or_path.lower().startswith('https://')): pac_urls.append(url_or_path) if from_dns: pac_urls.extend(proxy_urls_from_dns()) return pac_urls
def collect_pac_urls(from_os_settings=True, from_dns=True, **kwargs): """ Get all the URLs that potentially yield a PAC file. :param bool from_os_settings: Look for a PAC URL from the OS settings. If a value is found and is a URL, it comes first in the returned list. Doesn't do anything on non-Windows or non-macOS/OSX platforms. :param bool from_dns: Assemble a list of PAC URL candidates using the WPAD protocol. :return: A list of URLs that should be tried in order. :rtype: list[str] """ # Deprecated in 0.8.2 from_registry = kwargs.get('from_registry') if from_registry is not None: import warnings warnings.warn('from_registry is deprecated, use from_os_settings instead.') from_os_settings = from_registry pac_urls = [] if from_os_settings: if ON_WINDOWS: url_or_path = autoconfig_url_from_registry() elif ON_DARWIN: url_or_path = autoconfig_url_from_preferences() else: url_or_path = None if url_or_path and (url_or_path.lower().startswith('http://') or url_or_path.lower().startswith('https://')): pac_urls.append(url_or_path) if from_dns: pac_urls.extend(proxy_urls_from_dns()) return pac_urls
def get_pac(url=None, js=None, from_os_settings=True, from_dns=True, timeout=2, allowed_content_types=None, session=None, **kwargs): """ Convenience function for finding and getting a parsed PAC file (if any) that's ready to use. :param str url: Download PAC from a URL. If provided, `from_os_settings` and `from_dns` are ignored. :param str js: Parse the given string as a PAC file. If provided, `from_os_settings` and `from_dns` are ignored. :param bool from_os_settings: Look for a PAC URL or filesystem path from the OS settings, and use it if present. Doesn't do anything on non-Windows or non-macOS/OSX platforms. :param bool from_dns: Look for a PAC file using the WPAD protocol. :param timeout: Time to wait for host resolution and response for each URL. :param allowed_content_types: If the response has a ``Content-Type`` header, then consider the response to be a PAC file only if the header is one of these values. If not specified, the allowed types are ``application/x-ns-proxy-autoconfig`` and ``application/x-javascript-config``. :return: The first valid parsed PAC file according to the criteria, or `None` if nothing was found. :rtype: PACFile|None :raises MalformedPacError: If something that claims to be a PAC file was obtained but could not be parsed. """ if url: downloaded_pac = download_pac([url], timeout=timeout, allowed_content_types=allowed_content_types, session=session) if not downloaded_pac: return return PACFile(downloaded_pac, **kwargs) if js: return PACFile(js, **kwargs) # Deprecated in 0.8.2 from_registry = kwargs.get('from_registry') if from_registry is not None: import warnings warnings.warn('from_registry is deprecated, use from_os_settings instead.') from_os_settings = from_registry if from_os_settings: if ON_WINDOWS: path = autoconfig_url_from_registry() elif ON_DARWIN: path = autoconfig_url_from_preferences() else: path = None if path and path.lower().startswith('file://'): path = file_url_to_local_path(path) if path and os.path.isfile(path): with open(path) as f: return PACFile(f.read(), **kwargs) pac_candidate_urls = collect_pac_urls(from_os_settings=True, from_dns=from_dns) downloaded_pac = download_pac(pac_candidate_urls, timeout=timeout, allowed_content_types=allowed_content_types, session=session) if not downloaded_pac: return return PACFile(downloaded_pac, **kwargs)
def get_pac(url=None, js=None, from_os_settings=True, from_dns=True, timeout=2, allowed_content_types=None, **kwargs): """ Convenience function for finding and getting a parsed PAC file (if any) that's ready to use. :param str url: Download PAC from a URL. If provided, `from_os_settings` and `from_dns` are ignored. :param str js: Parse the given string as a PAC file. If provided, `from_os_settings` and `from_dns` are ignored. :param bool from_os_settings: Look for a PAC URL or filesystem path from the OS settings, and use it if present. Doesn't do anything on non-Windows or non-macOS/OSX platforms. :param bool from_dns: Look for a PAC file using the WPAD protocol. :param timeout: Time to wait for host resolution and response for each URL. :param allowed_content_types: If the response has a ``Content-Type`` header, then consider the response to be a PAC file only if the header is one of these values. If not specified, the allowed types are ``application/x-ns-proxy-autoconfig`` and ``application/x-javascript-config``. :return: The first valid parsed PAC file according to the criteria, or `None` if nothing was found. :rtype: PACFile|None :raises MalformedPacError: If something that claims to be a PAC file was obtained but could not be parsed. """ if url: downloaded_pac = download_pac([url], timeout=timeout, allowed_content_types=allowed_content_types) if not downloaded_pac: return return PACFile(downloaded_pac, **kwargs) if js: return PACFile(js, **kwargs) # Deprecated in 0.8.2 from_registry = kwargs.get('from_registry') if from_registry is not None: import warnings warnings.warn('from_registry is deprecated, use from_os_settings instead.') from_os_settings = from_registry if from_os_settings: if ON_WINDOWS: path = autoconfig_url_from_registry() elif ON_DARWIN: path = autoconfig_url_from_preferences() else: path = None if path and path.lower().startswith('file://'): path = file_url_to_local_path(path) if path and os.path.isfile(path): with open(path) as f: return PACFile(f.read(), **kwargs) pac_candidate_urls = collect_pac_urls(from_os_settings=True, from_dns=from_dns) downloaded_pac = download_pac(pac_candidate_urls, timeout=timeout, allowed_content_types=allowed_content_types) if not downloaded_pac: return return PACFile(downloaded_pac, **kwargs)
def test_mock_autoconfigurl_windows(): with _patch_winreg_qve(return_value=(test_reg_output_url, "foo")): assert autoconfig_url_from_registry() == test_reg_output_url
def test_autoconfig_url_from_registry(): value = autoconfig_url_from_registry() assert value is None or value.startswith("http://")