Example #1
0
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)
Example #2
0
def test_file_url_to_local_path(input_url, windows_expected_path,
                                mac_expected_path):
    if ON_WINDOWS:
        expected_path = windows_expected_path
    elif ON_DARWIN:
        expected_path = mac_expected_path
    else:
        pytest.skip("Not on Windows or macOS/OSX")
    assert file_url_to_local_path(input_url) == expected_path
Example #3
0
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)