Skip to content

Commit a32fe05

Browse files
authored
Moved global network settings into the network module (#695)
1 parent acc29ee commit a32fe05

14 files changed

+74
-75
lines changed

.isort.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[settings]
22
line_length=88
33
multi_line_output=3
4-
known_third_party=mock,numpy,pkg_resources,pytest,setuptools,xarray,pandas
4+
known_third_party=mock,numpy,pkg_resources,pytest,setuptools,xarray,pandas,certifi
55
known_first_party=pyproj,test
66
include_trailing_comma=true

docs/api/global_context.rst

-12
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,3 @@ pyproj.set_use_global_context
2121
-----------------------------
2222

2323
.. autofunction:: pyproj.set_use_global_context
24-
25-
26-
pyproj.set_global_context_network
27-
-----------------------------------
28-
29-
.. autofunction:: pyproj.set_global_context_network
30-
31-
32-
pyproj.is_global_context_network_enabled
33-
------------------------------------------
34-
35-
.. autofunction:: pyproj.is_global_context_network_enabled

docs/api/network.rst

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ PROJ Network Settings
44
======================
55

66

7+
pyproj.network.set_network_enabled
8+
-----------------------------------
9+
10+
.. autofunction:: pyproj.network.set_network_enabled
11+
12+
13+
pyproj.network.is_network_enabled
14+
----------------------------------
15+
16+
.. autofunction:: pyproj.network.is_network_enabled
17+
18+
719
pyproj.network.set_ca_bundle_path
820
----------------------------------
921

docs/history.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +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.network.set_ca_bundle_path` (pull #691)
25+
* ENH: Added :ref:`network` (#675, #691, #695)
2626

2727
2.6.1
2828
~~~~~

pyproj/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@
4949
import pyproj.network
5050
from pyproj._datadir import ( # noqa: F401
5151
_pyproj_global_context_initialize,
52-
is_global_context_network_enabled,
53-
set_global_context_network,
5452
set_use_global_context,
5553
)
5654
from pyproj._list import ( # noqa: F401

pyproj/_datadir.pyi

-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,3 @@ def _pyproj_global_context_initialize() -> None: ...
44
def get_user_data_dir(create: bool = False) -> str: ...
55
def _global_context_set_data_dir() -> None: ...
66
def set_use_global_context(active: Optional[bool] = None) -> None: ...
7-
def set_global_context_network(active: Optional[bool] = None) -> None: ...
8-
def is_global_context_network_enabled() -> bool: ...

pyproj/_datadir.pyx

+1-42
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ def set_use_global_context(active=None):
2828
through the duration of each python session and is closed
2929
once the program terminates.
3030
31-
.. note:: You can change the network settings with
32-
:func:`pyproj.set_global_context_network`.
31+
.. note:: To modify network settings see: :ref:`network`.
3332
3433
Parameters
3534
----------
@@ -46,46 +45,6 @@ def set_use_global_context(active=None):
4645
proj_context_set_autoclose_database(NULL, not _USE_GLOBAL_CONTEXT)
4746

4847

49-
def set_global_context_network(active=None):
50-
"""
51-
.. versionadded:: 3.0.0
52-
53-
Manages whether PROJ network is enabled on the global context.
54-
55-
.. note:: You can activate the global context with
56-
:func:`pyproj.set_use_global_context` or with
57-
the PYPROJ_GLOBAL_CONTEXT environment variable.
58-
59-
Parameters
60-
----------
61-
active: bool, optional
62-
Default is None, which uses the system defaults for networking.
63-
If True, it will force the use of network for grids regardless of
64-
any other network setting. If False, it will force disable use of
65-
network for grids regardless of any other network setting.
66-
"""
67-
if active is None:
68-
# in the case of the global context, need to reset network
69-
# setting based on the environment variable every time if None
70-
# because it could have been changed by the user previously
71-
active = strtobool(os.environ.get("PROJ_NETWORK", "OFF"))
72-
pyproj_context_set_enable_network(NULL, bool(active))
73-
74-
75-
def is_global_context_network_enabled():
76-
"""
77-
.. versionadded:: 3.0.0
78-
79-
.. note:: You can activate the global context with
80-
:func:`pyproj.set_use_global_context` or with
81-
the PYPROJ_GLOBAL_CONTEXT environment variable.
82-
83-
bool:
84-
If the network is enabled on the global context.
85-
"""
86-
return proj_context_is_network_enabled(NULL) == 1
87-
88-
8948
def get_user_data_dir(create=False):
9049
"""
9150
.. versionadded:: 3.0.0

pyproj/_network.pyi

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
from typing import Optional
2+
3+
def set_network_enabled(active: Optional[bool] = None) -> None: ...
4+
def is_network_enabled() -> bool: ...
15
def _set_ca_bundle_path(ca_bundle_path: str) -> None: ...

pyproj/_network.pyx

+36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
include "proj.pxi"
22

3+
import os
4+
from distutils.util import strtobool
5+
36
from pyproj.compat import cstrencode
47

58

@@ -14,3 +17,36 @@ def _set_ca_bundle_path(ca_bundle_path):
1417
The path to the CA Bundle.
1518
"""
1619
proj_context_set_ca_bundle_path(NULL, cstrencode(ca_bundle_path))
20+
21+
22+
def set_network_enabled(active=None):
23+
"""
24+
.. versionadded:: 3.0.0
25+
26+
Set whether PROJ network is enabled by default. This has the same
27+
behavior as the `PROJ_NETWORK` environment variable.
28+
29+
Parameters
30+
----------
31+
active: bool, optional
32+
Default is None, which uses the system defaults for networking.
33+
If True, it will force the use of network for grids regardless of
34+
any other network setting. If False, it will force disable use of
35+
network for grids regardless of any other network setting.
36+
"""
37+
if active is None:
38+
# in the case of the global context, need to reset network
39+
# setting based on the environment variable every time if None
40+
# because it could have been changed by the user previously
41+
active = strtobool(os.environ.get("PROJ_NETWORK", "OFF"))
42+
proj_context_set_enable_network(NULL, bool(active))
43+
44+
45+
def is_network_enabled():
46+
"""
47+
.. versionadded:: 3.0.0
48+
49+
bool:
50+
If PROJ network is enabled by default.
51+
"""
52+
return proj_context_is_network_enabled(NULL) == 1

pyproj/network.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77

88
import certifi
99

10-
from pyproj._network import _set_ca_bundle_path
10+
from pyproj._network import ( # noqa: F401
11+
_set_ca_bundle_path,
12+
is_network_enabled,
13+
set_network_enabled,
14+
)
1115

1216

1317
def set_ca_bundle_path(ca_bundle_path: Union[Path, str, bool, None] = None) -> None:
1418
"""
1519
.. versionadded:: 3.0.0
1620
1721
Sets the path to the CA Bundle used by the `curl`
18-
built into PROJ.
22+
built into PROJ when PROJ network is enabled..
1923
2024
Environment variables that can be used with PROJ 7.2+:
2125

test/conftest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ def proj_network_env():
1919
if not pyproj._datadir._USE_GLOBAL_CONTEXT:
2020
yield
2121
else:
22-
network = pyproj.is_global_context_network_enabled()
22+
network = pyproj.network.is_network_enabled()
2323
try:
2424
yield
2525
finally:
26-
pyproj.set_global_context_network(network)
26+
pyproj.network.set_network_enabled(network)
2727

2828

2929
@contextmanager

test/test_doctest_wrapper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_doctests():
1717

1818
with warnings.catch_warnings(), proj_network_env():
1919
if pyproj._datadir._USE_GLOBAL_CONTEXT:
20-
pyproj.set_global_context_network(active=True)
20+
pyproj.network.set_network_enabled(active=True)
2121
warnings.filterwarnings(
2222
"ignore",
2323
"You will likely lose important projection information when",

test/test_proj.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ def test_numpy_bool_kwarg_true():
537537
def test_network__disable():
538538
with proj_network_env():
539539
if pyproj._datadir._USE_GLOBAL_CONTEXT:
540-
pyproj.set_global_context_network(active=False)
540+
pyproj.network.set_network_enabled(active=False)
541541
transformer = Proj(3857, network=False)
542542
assert transformer.is_network_enabled is False
543543

@@ -546,15 +546,15 @@ def test_network__disable():
546546
def test_network__enable():
547547
with proj_network_env():
548548
if pyproj._datadir._USE_GLOBAL_CONTEXT:
549-
pyproj.set_global_context_network(active=True)
549+
pyproj.network.set_network_enabled(active=True)
550550
transformer = Proj(3857, network=True)
551551
assert transformer.is_network_enabled is True
552552

553553

554554
def test_network__default():
555555
with proj_network_env():
556556
if pyproj._datadir._USE_GLOBAL_CONTEXT:
557-
pyproj.set_global_context_network()
557+
pyproj.network.set_network_enabled()
558558
transformer = Proj(3857)
559559
assert transformer.is_network_enabled == (
560560
os.environ.get("PROJ_NETWORK") == "ON"

test/test_transformer.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ def test_pipeline_itransform(pipeline_str):
793793
def test_network__disable(transformer):
794794
with proj_network_env():
795795
if pyproj._datadir._USE_GLOBAL_CONTEXT:
796-
pyproj.set_global_context_network(active=False)
796+
pyproj.network.set_network_enabled(active=False)
797797
trans = transformer(network=False)
798798
assert trans.is_network_enabled is False
799799

@@ -812,7 +812,7 @@ def test_network__disable(transformer):
812812
def test_network__enable(transformer):
813813
with proj_network_env():
814814
if pyproj._datadir._USE_GLOBAL_CONTEXT:
815-
pyproj.set_global_context_network(active=True)
815+
pyproj.network.set_network_enabled(active=True)
816816
trans = transformer(network=True)
817817
assert trans.is_network_enabled is True
818818

@@ -830,7 +830,7 @@ def test_network__enable(transformer):
830830
def test_network__default(transformer):
831831
with proj_network_env():
832832
if pyproj._datadir._USE_GLOBAL_CONTEXT:
833-
pyproj.set_global_context_network()
833+
pyproj.network.set_network_enabled()
834834
trans = transformer()
835835
assert trans.is_network_enabled == (os.environ.get("PROJ_NETWORK") == "ON")
836836

@@ -839,7 +839,7 @@ def test_network__default(transformer):
839839
def test_transformer_group__network_enabled():
840840
with proj_network_env():
841841
if pyproj._datadir._USE_GLOBAL_CONTEXT:
842-
pyproj.set_global_context_network(active=True)
842+
pyproj.network.set_network_enabled(active=True)
843843
trans_group = TransformerGroup(4326, 2964, network=True)
844844
assert len(trans_group.unavailable_operations) == 0
845845
assert len(trans_group.transformers) == 10
@@ -855,7 +855,7 @@ def test_transformer_group__network_enabled():
855855
def test_transformer_group__network_disabled():
856856
with proj_network_env():
857857
if pyproj._datadir._USE_GLOBAL_CONTEXT:
858-
pyproj.set_global_context_network(active=False)
858+
pyproj.network.set_network_enabled(active=False)
859859
trans_group = TransformerGroup(4326, 2964, network=False)
860860
for transformer in trans_group.transformers:
861861
assert transformer.is_network_enabled is False
@@ -957,7 +957,7 @@ def test_transformer_group__download_grids(get_user_data_dir_mock, tmp_path, cap
957957
get_user_data_dir_mock.return_value = str(tmp_path)
958958
with proj_network_env():
959959
if pyproj._datadir._USE_GLOBAL_CONTEXT:
960-
pyproj.set_global_context_network(active=False)
960+
pyproj.network.set_network_enabled(active=False)
961961
trans_group = TransformerGroup(4326, 2964, network=False)
962962
trans_group.download_grids(verbose=True)
963963
captured = capsys.readouterr()
@@ -1005,7 +1005,7 @@ def test_transformer_group__download_grids__directory(
10051005
):
10061006
with proj_network_env():
10071007
if pyproj._datadir._USE_GLOBAL_CONTEXT:
1008-
pyproj.set_global_context_network(active=False)
1008+
pyproj.network.set_network_enabled(active=False)
10091009
trans_group = TransformerGroup(4326, 2964, network=False)
10101010
trans_group.download_grids(directory=tmp_path)
10111011
get_user_data_dir_mock.assert_not_called()

0 commit comments

Comments
 (0)