Skip to content

Refine validation wording and simplify delete logs - #106

Merged
jlegrand62 merged 10 commits into
devfrom
hotfix/path_validation
Jul 28, 2026
Merged

jlegrand62 merged 10 commits into
devfrom
hotfix/path_validation

Conversation

@jlegrand62

Copy link
Copy Markdown
Member
  • Update src/commons/plantdb/commons/fsdb/validation.py to log “The provided path is not related to a directory.” instead of the previous message when a non‑directory path is supplied.
  • Remove the _is_safe_to_delete safety check from src/commons/plantdb/commons/fsdb/file_ops.py for both file and fileset deletions, eliminating the IOError for out‑of‑scope paths.
  • Change successful deletion log statements from logger.info to logger.debug for JSON metadata, file, fileset, and scan removals, and adjust related log calls to match the new debug level.

… level

- Remove `_is_safe_to_delete` validation in `src/commons/plantdb/commons/fsdb/file_ops.py` for both file and fileset deletions, eliminating the `IOError` for out‑of‑scope paths.
- Change successful deletion logs from `logger.info` to `logger.debug` for JSON metadata, file, fileset, and scan removals, updating the corresponding messages.
- Adjust related log calls to use the new debug level, cleaning up redundant error handling.
- Update `src/commons/plantdb/commons/fsdb/validation.py` to log "`The provided path is not related to a directory.`" instead of "`The provided path is not a directory.`" when the path is not a directory.
@jlegrand62 jlegrand62 self-assigned this Jul 26, 2026
# Check if the path is a directory
if not path.is_dir():
logger.error("The provided path is not a directory.")
logger.error("The provided path is not related to a directory.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change makes the error message less clear, after all, a fsdb is a dir and this message is only sent when path is not a dir. (What does "related to a directory" mean ?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...yep, you are right!

Comment on lines -566 to -569
if not _is_safe_to_delete(file_path):
logger.error(f"File {file.filename} is not in the current database.")
logger.debug(f"File path: '{file_path}'")
raise IOError("Cannot delete files or directories outside of a local DB.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the motivation for removing the checks ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lazyness i guess... the _is_safe_to_delete was not properly implemented. It was testing if the given file_path is an FSDB, and was thus throwing a lot of error messages that should not exists. Instead of fixing _is_safe_to_delete , I chose to bypass it as its internal use did not make it essential.

But now I will fix it.

- Extend `_is_safe_to_delete` signature to `def _is_safe_to_delete(path, db_path) -> bool`.
- Resolve both `path` and `db_path` to absolute paths and verify `db_path` is a valid FSDB.
- Ensure the deletion target is a sub‑path of the FSDB and not the FSDB root itself; log errors for invalid cases.
- Return `True` only after all safety validations succeed.
- Update docstring to include `db_path` parameter, revised safety notes, and example usage.
… `file_ops.py`

- Replace `_load_scans` example with `_load_scan` usage and add `db.disconnect()` cleanup in doctests.
- Append `db.disconnect()` calls to all doctest sections for proper temporary DB teardown.
- Introduce new doctest examples for:
  - `_load_file`
  - `_load_measures` and `_load_scan_measures`
  - `_delete_file`, `_delete_fileset`, and `_delete_scan`
  - `_make_fileset`, `_make_scan`, and `_store_scan`
- Enhance delete functions (`_delete_file`, `_delete_fileset`, `_delete_scan`) with `_is_safe_to_delete(path, db.path())` validation to prevent out‑of‑scope deletions.
- Update docstrings to incorporate the added examples and safety notes.
- Introduce new test module `src/commons/tests/test_file_ops.py` covering the public helpers in `plantdb.commons.fsdb.file_ops`
- Validate loading functions with empty DB, existing scans, and various scan‑fileset‑file scenarios (`_load_scans`, `_load_scan`, `_load_scan_filesets`, `_load_fileset`, `_load_fileset_files`, `_load_file`)
- Test measures handling (`_load_measures`, `_load_scan_measures`) including malformed JSON and non‑dict data cases
- Ensure deletion safety when a file has no `filename` attribute (`_delete_file`)
- Verify directory creation helpers (`_make_fileset`, `_make_scan`) create the expected paths
- Provide fixtures `db_with_fileset`, `db_with_file`, and `db_with_fileset_and_file` for isolated dummy database setups.
- Introduce new test module `src/commons/tests/test_validation.py`
- Cover `_is_valid_id` with varied inputs and error‑log checks
- Test `_is_fsdb` handling of empty DB, non‑directory paths, missing marker, valid/invalid scans, and extra directories
- Validate `_is_scan_dataset` for missing metadata, missing or malformed `files.json`, missing `filesets` key, and both with and without fileset validation
- Verify `_is_valid_fileset` behavior for missing directory, missing files, and fully valid filesets
- Add tests for `_fileset_files_exists` with empty, partially invalid, and mixed file entries
- Ensure `_is_safe_to_delete` correctly rejects paths outside the DB, invalid DBs, root DB path, and accepts valid sub‑paths
- Use fixtures `empty_db_path`, `db_with_scan`, `db_with_fileset`, and `db_with_file` to provide isolated dummy databases
- Include logging assertions to confirm appropriate error messages are emitted.
@jlegrand62
jlegrand62 merged commit 7350326 into dev Jul 28, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants