Skip to content

Commit 997238d

Browse files
committed
small logging changes. For lpsdata if no directories are prefixed by digits it will simply create an Uploaded directory
1 parent 2b7403f commit 997238d

3 files changed

Lines changed: 31 additions & 16 deletions

File tree

mokelumne/dags/copy_files.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ def rename_temp(destination: str) -> None:
239239

240240
@task.short_circuit()
241241
def sourcedir_is_lpsdata(source_dir: str) -> bool:
242-
logger.info("Checking if %s volume is and lpsdata volume. If so will move files to Uploaded directory", source_dir)
243-
return source_dir.lower().endswith("ready_to_upload") and source_dir.lower().startswith("/srv/lpsdata")
242+
logger.info("Checking if %s volume is an lpsdata volume. If so will move files to Uploaded directory", source_dir)
243+
return source_dir.lower().startswith("/srv/lpsdata")
244244

245245
@task
246246
def move_lpsdata_to_uploaded(source_dir: str) -> None:

mokelumne/util/file_transfer.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,13 @@ def _all_subdirs(upload_dir: Path) -> list:
207207
return all_subdirs
208208

209209
def _get_next_subdir_index(parent_dir: Path) -> int:
210-
"""Source directories on lpsdata* is prefixed by digits. Find the highest prefixed digits"""
210+
"""Source directories on lpsdata* are prefixed by digits. Find the highest prefixed digits"""
211211

212212
sub_dirs = _all_subdirs(parent_dir)
213213
numbers = [int(s.name.split('_')[0]) for s in sub_dirs if s.name.split('_')[0].isdigit()]
214214

215215
if not numbers:
216-
raise FileNotFoundError(f"Could not find directories prepended with numeric in: {parent_dir.name}")
216+
return None
217217

218218
highest = max(numbers) + 1
219219

@@ -227,7 +227,7 @@ def _find_uploaded_dir(parent: Path) -> Path | None:
227227
)
228228

229229

230-
def _move_files(source_dir, dest_dir) -> int:
230+
def _move_files(source_dir: Path | str, dest_dir: Path | str) -> int:
231231
"""Move files from one directory to another"""
232232
source = Path(source_dir)
233233
dest = Path(dest_dir)
@@ -253,9 +253,11 @@ def move_to_uploaded(source_dir: str) -> tuple[str, int]:
253253
# e.g. if there's a directory 03_Files_to_Review.
254254
# It would be 04_Uploaded
255255
if not uploaded_dir:
256-
all_sub_dirs = _all_subdirs(parent_dir)
257256
highest = _get_next_subdir_index(parent_dir)
258-
uploaded_dir = f"{parent_dir}/{highest:02}_Uploaded"
257+
if highest is None:
258+
uploaded_dir = f"{parent_dir}/Uploaded"
259+
else:
260+
uploaded_dir = f"{parent_dir}/{highest:02}_Uploaded"
259261

260262
move_count = _move_files(source_dir, uploaded_dir)
261263

test/unit/test_file_transfer.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,23 +204,36 @@ def test_move_to_uploaded_uses_existing_uploaded_directory(self, tmp_path: Path)
204204
assert not source_file.exists()
205205
assert (uploaded_dir / source_file.name).exists()
206206

207-
def test_move_to_uploaded_creates_new_uploaded_directory(self, tmp_path: Path):
208-
"""Ensure move_to_uploaded creates a numbered uploaded directory when needed."""
207+
@pytest.mark.parametrize(
208+
"existing_dir_name, expected_uploaded_name",
209+
[
210+
("03_Files_to_Review", "04_Uploaded"), # Case 1: Digit prefix exists -> Increment
211+
("No_Digits_Here", "Uploaded"), # Case 2: No digit prefix -> Use default fallback
212+
]
213+
)
209214

215+
def test_move_to_uploaded_creates_new_uploaded_directory(
216+
self, tmp_path: Path, existing_dir_name: str, expected_uploaded_name: str
217+
):
218+
"""Ensure move_to_uploaded creates the correct uploaded directory based on context."""
210219
parent_dir = tmp_path / "review"
211220
source_dir = parent_dir / "source"
212221
source_dir.mkdir(parents=True)
213-
214-
review_dir = parent_dir / "03_Files_to_Review"
222+
223+
# Dynamically create the environment based on the parameter
224+
review_dir = parent_dir / existing_dir_name
215225
review_dir.mkdir()
216-
226+
227+
# Set up a dummy file to trace the movement
217228
source_file = source_dir / "some_file.tif"
218229
source_file.write_text("some contents", encoding="utf-8")
219-
230+
231+
# Execute the function under test
220232
result_path, moved_count = file_transfer.move_to_uploaded(str(source_dir))
221-
222-
expected_uploaded_dir = parent_dir / "04_Uploaded"
223-
233+
234+
# Verify outcomes using the parameterized target name
235+
expected_uploaded_dir = parent_dir / expected_uploaded_name
236+
224237
assert result_path == str(expected_uploaded_dir)
225238
assert moved_count == 1
226239
assert not source_file.exists()

0 commit comments

Comments
 (0)