-
-
Notifications
You must be signed in to change notification settings - Fork 68
feat: Implement open_data_file() function for file handling - Closes #251 #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: Implement open_data_file() function for file handling - Closes #251 #252
Conversation
…ata_file() function to shared library - Issue creativecommons#251 Added a function to open and read a data file with error handling.
|
@harsha08-2k6 harsha08-2k6PRs with incomplete descriptions or that don't otherwise follow the included directions can't be accepted. |
| try: | ||
| if not os.path.exists(file_path): | ||
| raise QuantifyingException( | ||
| f"Data file not found: {file_path}", | ||
| exit_code=1 | ||
| ) | ||
|
|
||
| with open(file_path, 'r', encoding='utf-8') as f: | ||
| return f.read() | ||
|
|
||
| except QuantifyingException: | ||
| raise | ||
| except FileNotFoundError as e: | ||
| raise QuantifyingException( | ||
| f"Data file not found: {file_path}", | ||
| exit_code=1 | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The duplicate file not found logic is nonsensical.
This appears to be AI slop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback! You're absolutely right - the duplicate file existence check is redundant. I apologize for the oversight.
The os.path.exists() check makes the FileNotFoundError exception handler unreachable. I'll simplify this by removing the pre-check and letting Python's built-in exception handling work naturally:
try:
with open(file_path, 'r', encoding='utf-8') as f:
return f.read()
except FileNotFoundError:
raise QuantifyingException(
f"Data file not found: {file_path}",
exit_code=1
)
except QuantifyingException:
raiseI'll push this fix shortly. Thanks for catching this!
Removed redundant os.path.exists() check before file open. The FileNotFoundError exception handler already covers this case, making the explicit check unnecessary.
Fixes
Description
This PR implements the
open_data_file()function in the shared library (scripts/shared.py) to handle file operations with proper error handling. The function catches anticipated exceptions and converts them into helpful error messages usingQuantifyingException, preventing raw exceptions from being displayed when process or report scripts attempt to open non-existent files.Technical details
open_data_file(file_path)function toscripts/shared.pyFileNotFoundError: When file doesn't existPermissionError: When file cannot be accessed due to permissionsException: For other unexpected errorsQuantifyingExceptionwith descriptive error messages and exit code 1Tests
Manually tested the function locally by:
No automated tests added as this follows the pattern of other utility functions in the shared library.
Screenshots
N/A - Backend function with no UI changes
Checklist
Update index.md).mainormaster).