Skip to content

Commit

Permalink
update core_utilities get_base_dir to work on windows, check for the …
Browse files Browse the repository at this point in the history
…CoastSeg directory on both the file and cwd paths and create the CoastSeg directory if not
  • Loading branch information
2320sharon committed Jun 11, 2024
1 parent d11b904 commit 0f6fdfd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
42 changes: 32 additions & 10 deletions src/coastseg/core_utilities.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pathlib
from sysconfig import get_python_version

import os

def is_interactive() -> bool:
"""
Expand All @@ -26,20 +26,42 @@ def get_base_dir(repo_name="CoastSeg") -> pathlib.Path:
A `pathlib.Path` object representing the project directory path.
"""

def resolve_repo_path(cwd: pathlib.Path, proj_name: str) -> pathlib.Path:
root = cwd.root
def resolve_repo_path(cwd: pathlib.Path, proj_name: str, max_depth: int = 100) -> pathlib.Path:
root = cwd.anchor if cwd.anchor else cwd.root # Handle root differently for Windows
# print(f"root: {root}")
proj_dir = cwd
# keep moving up the directory tree until the project directory is found or the root is reached
depth = 0

# Keep moving up the directory tree until the project directory is found or the root is reached
while proj_dir.name != proj_name:
if depth > max_depth:
raise ValueError(f"Reached maximum depth - cannot resolve project path. Could not find {proj_name} in {cwd}.")
proj_dir = proj_dir.parent
depth += 1
if str(proj_dir) == root:
msg = "Reached root depth - cannot resolve project path."
raise ValueError(msg)
# return the project directory path for example CoastSeg directory
raise ValueError(f"Reached root depth - cannot resolve project path. Could not find {proj_name} in {cwd}.")

# Return the project directory path, for example, CoastSeg directory
return proj_dir

cwd = pathlib.Path().resolve() if is_interactive() else pathlib.Path(__file__)

proj_dir = resolve_repo_path(cwd, proj_name=repo_name)
cwd = pathlib.Path().resolve() if is_interactive() else pathlib.Path(__file__)
try:
proj_dir = resolve_repo_path(cwd, proj_name=repo_name)
except ValueError as e:
try:
# see if where the script is running from contains the project directory CoastSeg
cwd = pathlib.Path(os.getcwd())
proj_dir = resolve_repo_path(cwd, proj_name=repo_name)
except ValueError as e:
# get the currentq working directory
cwd = os.getcwd()
proj_dir = os.path.join(cwd, repo_name)
# this means it was the first time creating it so print a message
if not os.path.exists(proj_dir):
print(f"Creating {repo_name} because it was not found. Created at {proj_dir}")
os.makedirs(proj_dir, exist_ok=True)
# convert to a pathlib.Path object
proj_dir = pathlib.Path(proj_dir)

return proj_dir

2 changes: 1 addition & 1 deletion tests/test_coastseg_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def test_save_config(coastseg_map_with_selected_roi_layer, tmp_path):
expected_config_geojson_path = tmp_path / "config_gdf.geojson"
assert expected_config_geojson_path.exists()

@pytest.mark.parametrize('named_temp_dir', [('CoastSeg',str(pathlib.Path(__file__).parent))], indirect=True)
@pytest.mark.parametrize('named_temp_dir', [('CoastSeg',None)], indirect=True)
def test_save_config_empty_roi_settings(coastseg_map_with_selected_roi_layer, named_temp_dir):
"""test_save_config_empty_roi_settings tests if save configs will save both a config.json and
config_gdf.geojson to the filepath directory when coastseg_map's rois do not have roi_settings.
Expand Down
6 changes: 3 additions & 3 deletions tests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_import_bounding_boxes():
except ImportError:
assert False, "Failed to import bounding_boxes"

@pytest.mark.parametrize('named_temp_dir', [('CoastSeg',str(pathlib.Path(__file__).parent))], indirect=True)
@pytest.mark.parametrize('named_temp_dir', [('CoastSeg',None)], indirect=True)
def test_import_coastseg_logs(named_temp_dir):
try:
# The named_temp_dir fixture created a temporary directory named 'CoastSeg'
Expand Down Expand Up @@ -54,8 +54,8 @@ def test_import_downloads():
assert False, "Failed to import downloads"


# Use the `pytest.mark.parametrize` to pass the parameter to the fixture
@pytest.mark.parametrize('named_temp_dir', [('CoastSeg',str(pathlib.Path(__file__).parent))], indirect=True)
# Use the `pytest.mark.parametrize` to pass the parameter to the fixture # create a temporary directory named 'CoastSeg'
@pytest.mark.parametrize('named_temp_dir', [('CoastSeg',None)], indirect=True)
def test_import_download_tide_model(named_temp_dir):
try:
# The named_temp_dir fixture created a temporary directory named 'CoastSeg'
Expand Down

0 comments on commit 0f6fdfd

Please sign in to comment.