Skip to content

Commit

Permalink
BUG: get_data_dir support for conda Windows enviroments
Browse files Browse the repository at this point in the history
  • Loading branch information
snowman2 committed Mar 6, 2022
1 parent 8f0e1f7 commit 30bf677
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Latest
- BUG: Reorder deps in show_versions for setuptools issue (issue #1017)
- BUG: remove CustomConstructorCRS @abstractmethod decorator (pull #1018)
- BUG: Correct type annotation for AreaofUse.bounds (issue #1012)
- BUG: :func:`pyproj.datadir.get_data_dir` support for conda Windows (issue #1029)

3.3.0
-------
Expand Down
9 changes: 8 additions & 1 deletion pyproj/datadir.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def get_data_dir() -> str:
internal_datadir = Path(__file__).absolute().parent / "proj_dir" / "share" / "proj"
proj_lib_dirs = os.environ.get("PROJ_LIB", "")
prefix_datadir = Path(sys.prefix, "share", "proj")
windows_prefix_datadir = Path(sys.prefix, "Library", "share", "proj")

def valid_data_dir(potential_data_dir):
if (
Expand All @@ -98,14 +99,20 @@ def valid_data_dirs(potential_data_dirs):
_VALIDATED_PROJ_DATA = proj_lib_dirs
elif valid_data_dir(prefix_datadir):
_VALIDATED_PROJ_DATA = str(prefix_datadir)
elif valid_data_dir(windows_prefix_datadir):
_VALIDATED_PROJ_DATA = str(windows_prefix_datadir)
else:
proj_exe = shutil.which("proj", path=sys.prefix)
if proj_exe is None:
proj_exe = shutil.which("proj")
if proj_exe is not None:
system_proj_dir = Path(proj_exe).parent.parent / "share" / "proj"
system_dir = Path(proj_exe).parent.parent
system_proj_dir = system_dir / "share" / "proj"
windows_system_proj_dir = system_dir / "Library" / "share" / "proj"
if valid_data_dir(system_proj_dir):
_VALIDATED_PROJ_DATA = str(system_proj_dir)
elif valid_data_dir(windows_system_proj_dir):
_VALIDATED_PROJ_DATA = str(windows_system_proj_dir)

if _VALIDATED_PROJ_DATA is None:
raise DataDirError(
Expand Down
22 changes: 22 additions & 0 deletions test/test_datadir.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ def test_get_data_dir__from_prefix(tmp_path):
assert get_data_dir() == str(proj_dir)


def test_get_data_dir__from_prefix__windows(tmp_path):
with proj_env(), patch.dict(os.environ, {}, clear=True), patch(
"pyproj.datadir.Path.absolute", return_value=_INVALID_PATH
), patch("pyproj.datadir.sys.prefix", str(tmp_path)):
proj_dir = tmp_path / "Library" / "share" / "proj"
proj_dir.mkdir(parents=True)
create_projdb(proj_dir)
assert get_data_dir() == str(proj_dir)


def test_get_data_dir__from_path(tmp_path):
with proj_env(), patch.dict(os.environ, {}, clear=True), patch(
"pyproj.datadir.Path.absolute", return_value=_INVALID_PATH
Expand All @@ -156,6 +166,18 @@ def test_get_data_dir__from_path(tmp_path):
assert get_data_dir() == str(proj_dir)


def test_get_data_dir__from_path__windows(tmp_path):
with proj_env(), patch.dict(os.environ, {}, clear=True), patch(
"pyproj.datadir.Path.absolute", return_value=_INVALID_PATH
), patch("pyproj.datadir.sys.prefix", str(_INVALID_PATH)), patch(
"pyproj.datadir.shutil.which", return_value=str(tmp_path / "bin" / "proj")
):
proj_dir = tmp_path / "Library" / "share" / "proj"
proj_dir.mkdir(parents=True)
create_projdb(proj_dir)
assert get_data_dir() == str(proj_dir)


@pytest.mark.parametrize("projdir_type", [str, Path])
def test_append_data_dir__internal(projdir_type, tmp_path):
with proj_env(), patch.dict(os.environ, {}, clear=True), patch(
Expand Down

0 comments on commit 30bf677

Please sign in to comment.