-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move set_ca_bundle_path to network and add tests
- Loading branch information
Showing
12 changed files
with
136 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ API Documentation | |
proj | ||
list | ||
datadir | ||
network | ||
sync | ||
global_context | ||
enums | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.. _network: | ||
|
||
PROJ Network Settings | ||
====================== | ||
|
||
|
||
pyproj.network.set_ca_bundle_path | ||
---------------------------------- | ||
|
||
.. autofunction:: pyproj.network.set_ca_bundle_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
def _set_ca_bundle_path(ca_bundle_path: str) -> None: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
include "proj.pxi" | ||
|
||
from pyproj.compat import cstrencode | ||
|
||
|
||
def _set_ca_bundle_path(ca_bundle_path): | ||
""" | ||
Sets the path to the CA Bundle used by the `curl` | ||
built into PROJ. | ||
Parameters | ||
---------- | ||
ca_bundle_path: str | ||
The path to the CA Bundle. | ||
""" | ||
proj_context_set_ca_bundle_path(NULL, cstrencode(ca_bundle_path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
Module for managing the PROJ network settings. | ||
""" | ||
import os | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
import certifi | ||
|
||
from pyproj._network import _set_ca_bundle_path | ||
|
||
|
||
def set_ca_bundle_path(ca_bundle_path: Union[Path, str, bool, None] = None) -> None: | ||
""" | ||
.. versionadded:: 3.0.0 | ||
Sets the path to the CA Bundle used by the `curl` | ||
built into PROJ. | ||
Environment variables that can be used with PROJ 7.2+: | ||
- PROJ_CURL_CA_BUNDLE | ||
- CURL_CA_BUNDLE | ||
- SSL_CERT_FILE | ||
Parameters | ||
---------- | ||
ca_bundle_path: Union[Path, str, bool, None], optional | ||
Default is None, which only uses the `certifi` package path as a fallback if | ||
the environment variables are not set. If a path is passed in, then | ||
that will be the path used. If it is set to True, then it will default | ||
to using the path provied by the `certifi` package. If it is set to False, | ||
then it will not set the path. | ||
""" | ||
if ca_bundle_path is False: | ||
return None | ||
|
||
env_var_names = ( | ||
"PROJ_CURL_CA_BUNDLE", | ||
"CURL_CA_BUNDLE", | ||
"SSL_CERT_FILE", | ||
) | ||
if isinstance(ca_bundle_path, (str, Path)): | ||
ca_bundle_path = str(ca_bundle_path) | ||
elif (ca_bundle_path is True) or not any( | ||
env_var_name in os.environ for env_var_name in env_var_names | ||
): | ||
ca_bundle_path = certifi.where() | ||
|
||
if isinstance(ca_bundle_path, str): | ||
_set_ca_bundle_path(ca_bundle_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import certifi | ||
import pytest | ||
from mock import patch | ||
|
||
from pyproj.network import set_ca_bundle_path | ||
|
||
|
||
@patch.dict("os.environ", {}, clear=True) | ||
@patch("pyproj.network._set_ca_bundle_path") | ||
def test_ca_bundle_path__default(c_set_ca_bundle_path_mock): | ||
set_ca_bundle_path() | ||
c_set_ca_bundle_path_mock.assert_called_with(certifi.where()) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"env_var", ["PROJ_CURL_CA_BUNDLE", "CURL_CA_BUNDLE", "SSL_CERT_FILE"] | ||
) | ||
@patch("pyproj.network._set_ca_bundle_path") | ||
def test_ca_bundle_path__always_certifi(c_set_ca_bundle_path_mock, env_var): | ||
with patch.dict("os.environ", {env_var: "/tmp/dummy/path/cacert.pem"}, clear=True): | ||
set_ca_bundle_path(True) | ||
c_set_ca_bundle_path_mock.assert_called_with(certifi.where()) | ||
|
||
|
||
@patch.dict("os.environ", {}, clear=True) | ||
@patch("pyproj.network._set_ca_bundle_path") | ||
def test_ca_bundle_path__skip(c_set_ca_bundle_path_mock): | ||
set_ca_bundle_path(False) | ||
c_set_ca_bundle_path_mock.assert_not_called() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"env_var", ["PROJ_CURL_CA_BUNDLE", "CURL_CA_BUNDLE", "SSL_CERT_FILE"] | ||
) | ||
@patch("pyproj.network._set_ca_bundle_path") | ||
def test_ca_bundle_path__env_var_skip(c_set_ca_bundle_path_mock, env_var): | ||
with patch.dict("os.environ", {env_var: "/tmp/dummy/path/cacert.pem"}, clear=True): | ||
set_ca_bundle_path() | ||
c_set_ca_bundle_path_mock.assert_not_called() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"env_var", ["PROJ_CURL_CA_BUNDLE", "CURL_CA_BUNDLE", "SSL_CERT_FILE"] | ||
) | ||
@patch("pyproj.network._set_ca_bundle_path") | ||
def test_ca_bundle_path__custom_path(c_set_ca_bundle_path_mock, env_var): | ||
with patch.dict("os.environ", {env_var: "/tmp/dummy/path/cacert.pem"}, clear=True): | ||
set_ca_bundle_path("/my/path/to/cacert.pem") | ||
c_set_ca_bundle_path_mock.assert_called_with("/my/path/to/cacert.pem") |