Skip to content

Commit 6d83854

Browse files
committed
ENH: Added pyproj.set_ca_bundle_path
1 parent 3773248 commit 6d83854

File tree

8 files changed

+62
-5
lines changed

8 files changed

+62
-5
lines changed

docs/api/datadir.rst

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
.. _data_directory:
1+
.. _data_and_ca_bundle_directory:
22

3-
Data Directory
4-
==============
3+
Data Directory & CA Bundle Path
4+
===============================
55

66
pyproj.datadir.get_data_dir
77
---------------------------
@@ -25,3 +25,9 @@ pyproj.datadir.get_user_data_dir
2525
---------------------------------
2626

2727
.. autofunction:: pyproj.datadir.get_user_data_dir
28+
29+
30+
pyproj.set_ca_bundle_path
31+
--------------------------
32+
33+
.. autofunction:: pyproj.set_ca_bundle_path

docs/history.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Change Log
2222
* ENH: Added ability to use global context (issue #661)
2323
* ENH: Add support for temporal CRS CF coordinate system (issue #672)
2424
* BUG: Fix handling of polygon holes when calculating area in Geod (pull #686)
25+
* ENH: Added :func:`pyproj.set_ca_bundle_path` (pull #691)
2526

2627
2.6.1
2728
~~~~~

docs/transformation_grids.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ More information about the data available is located under the PROJ
99
`resource files <https://proj.org/resource_files.html#transformation-grids>`__
1010
documentation.
1111

12-
`pyproj` API for managing the :ref:`data_directory`
12+
`pyproj` API for managing the :ref:`data_and_ca_bundle_directory`
1313

1414
.. warning:: pyproj 2 includes datumgrid 1.8 in the wheels. pyproj 3 will not include any datum grids.
1515

pyproj/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from pyproj._datadir import ( # noqa: F401
5050
_pyproj_global_context_initialize,
5151
is_global_context_network_enabled,
52+
set_ca_bundle_path,
5253
set_global_context_network,
5354
set_use_global_context,
5455
)
@@ -79,3 +80,5 @@
7980
_pyproj_global_context_initialize()
8081
except DataDirError as err:
8182
warnings.warn(str(err))
83+
84+
set_ca_bundle_path()

pyproj/_datadir.pyi

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from typing import Optional
1+
from pathlib import Path
2+
from typing import Optional, Union
23

34
def _pyproj_global_context_initialize() -> None: ...
45
def get_user_data_dir(create: bool = False) -> str: ...
6+
def set_ca_bundle_path(ca_bundle_path: Union[Path, str, bool, None] = None) -> None: ...
57
def _global_context_set_data_dir() -> None: ...
68
def set_use_global_context(active: Optional[bool] = None) -> None: ...
79
def set_global_context_network(active: Optional[bool] = None) -> None: ...

pyproj/_datadir.pyx

+43
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
22
import warnings
33
from distutils.util import strtobool
4+
from pathlib import Path
45

6+
import certifi
57
from libc.stdlib cimport free, malloc
68

79
from pyproj.compat import cstrencode, pystrdecode
@@ -111,6 +113,47 @@ def get_user_data_dir(create=False):
111113
return pystrdecode(proj_context_get_user_writable_directory(NULL, bool(create)))
112114

113115

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+
114157
cdef void pyproj_log_function(void *user_data, int level, const char *error_msg):
115158
"""
116159
Log function for catching PROJ errors.

pyproj/proj.pxi

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ cdef extern from "proj.h":
99
const char *dbPath,
1010
const char *const *auxDbPaths,
1111
const char* const *options)
12+
void proj_context_set_ca_bundle_path(PJ_CONTEXT *ctx, const char *path);
1213

1314
# projCtx has been replaced by PJ_CONTEXT *.
1415
# projPJ has been replaced by PJ *

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,5 @@ def get_long_description():
244244
ext_modules=get_extension_modules(),
245245
package_data=get_package_data(),
246246
zip_safe=False, # https://mypy.readthedocs.io/en/stable/installed_packages.html
247+
install_requires=["certifi"],
247248
)

0 commit comments

Comments
 (0)