|
1 | 1 | import os
|
2 | 2 | import warnings
|
3 | 3 | from distutils.util import strtobool
|
| 4 | +from pathlib import Path |
4 | 5 |
|
| 6 | +import certifi |
5 | 7 | from libc.stdlib cimport free, malloc
|
6 | 8 |
|
7 | 9 | from pyproj.compat import cstrencode, pystrdecode
|
@@ -111,6 +113,47 @@ def get_user_data_dir(create=False):
|
111 | 113 | return pystrdecode(proj_context_get_user_writable_directory(NULL, bool(create)))
|
112 | 114 |
|
113 | 115 |
|
| 116 | +def set_ca_bundle_path(ca_bundle_path=None): |
| 117 | + """ |
| 118 | + .. versionadded:: 3.0.0 |
| 119 | +
|
| 120 | + Sets the path to the CA Bundle used by the `curl` |
| 121 | + built into PROJ. |
| 122 | +
|
| 123 | + Environment variables that can be used with PROJ 7.2+: |
| 124 | + - PROJ_CURL_CA_BUNDLE |
| 125 | + - CURL_CA_BUNDLE |
| 126 | + - SSL_CERT_FILE |
| 127 | +
|
| 128 | + Parameters |
| 129 | + ---------- |
| 130 | + ca_bundle_path: Union[Path, str, bool, None], optional |
| 131 | + Default is None, which only uses the `certifi` package path as a fallback if |
| 132 | + the environment variables are not set. If a path is passed in, then |
| 133 | + that will be the path used. If it is set to True, then it will default |
| 134 | + to using the path provied by the `certifi` package. If it is set to False, |
| 135 | + then it will not set the path. |
| 136 | + """ |
| 137 | + if ca_bundle_path is False: |
| 138 | + return None |
| 139 | + |
| 140 | + env_var_names = ( |
| 141 | + "PROJ_CURL_CA_BUNDLE", |
| 142 | + "CURL_CA_BUNDLE", |
| 143 | + "SSL_CERT_FILE", |
| 144 | + ) |
| 145 | + if isinstance(ca_bundle_path, (str, Path)): |
| 146 | + ca_bundle_path = str(ca_bundle_path) |
| 147 | + elif ( |
| 148 | + (ca_bundle_path is True) or |
| 149 | + not any(env_var_name in os.environ for env_var_name in env_var_names) |
| 150 | + ): |
| 151 | + ca_bundle_path = certifi.where() |
| 152 | + |
| 153 | + if isinstance(ca_bundle_path, str): |
| 154 | + proj_context_set_ca_bundle_path(NULL, cstrencode(ca_bundle_path)) |
| 155 | + |
| 156 | + |
114 | 157 | cdef void pyproj_log_function(void *user_data, int level, const char *error_msg):
|
115 | 158 | """
|
116 | 159 | Log function for catching PROJ errors.
|
|
0 commit comments