Skip to content

Final code q and install points#7

Merged
arthur-debert merged 11 commits intomainfrom
codeq
Mar 18, 2025
Merged

Final code q and install points#7
arthur-debert merged 11 commits intomainfrom
codeq

Conversation

@arthur-debert
Copy link
Copy Markdown
Owner

No description provided.

successfully refactored the run function in src/dotcat/cli.py into smaller, more modular functions and updated the tests to make use of this higher modularity.

Here's what I did:

Refactored the run function into several smaller functions, each with a single responsibility:

handle_version_flag: Handles the version flag display
handle_special_case_arguments: Handles the special case for dotted-path arguments
validate_required_arguments: Validates that required arguments are present
process_file: Parses the file and handles errors
lookup_and_format_value: Looks up the value and formats the output
Created a new test file tests/test_cli_functions.py that tests each of these functions individually, providing better test coverage and making it easier to identify issues with specific functionality.

Verified that all existing tests in test_exec.py still pass, ensuring that the refactoring didn't break any existing functionality.

The refactoring has improved the code quality by:

Making each function focused on a single responsibility
Improving readability and maintainability
Making the code more testable
Providing better error handling and separation of concerns
All tests are passing, confirming that the refactored code maintains the same behavior as the original implementation.
uccessfully modified the zsh/dotcat-completion.py script to use the main dotcat library code instead of duplicating functionality. This approach has several advantages:

Reduced Code Duplication: The completion script now leverages the existing parsing and data access functionality from the main library.

Better Maintainability: When the main library code changes, the completion script automatically benefits from those changes without needing separate updates.

Consistent Behavior: The completion script now uses the same parsing logic as the main application, ensuring consistent behavior.

The modified script works correctly even when installed in a different path from the main library code. This is possible because:

When the dotcat package is installed, it's placed in the Python site-packages directory, which is in the Python path.
The completion script can import the dotcat package using standard Python import statements, regardless of where it's installed.
I've tested the modified script with various file formats and data structures, including deeply nested objects and lists, and it works correctly in all cases. The script correctly:

Parses different file formats (JSON, YAML, TOML, INI) using the main library's parse_file function
Extracts dotted paths from the parsed data, including list indices with the @ symbol
Filters paths based on the provided prefix
This approach ensures that the completion script stays in sync with the main library code and reduces maintenance overhead.
uccessfully moved the install_completions.py file from src/dotcat/ to the zsh/ directory. Here's what I did:

Created a new version of install_completions.py in the zsh/ directory with updated path calculations:

Modified the code to correctly locate the completion files from the new location
Updated the path calculation logic to work from the zsh directory
Made the zsh/ directory a proper Python package by adding an __init__.py file

Updated the import in src/dotcat/__init__.py to import from the new location:

from zsh import install_completions  # Instead of from . import install_completions
Updated pyproject.toml to:

Point the entry points to the new location:
install-dotcat-completions = "zsh.install_completions:main"
dotcat-install-completions = "zsh.install_completions:main"
Include the zsh directory as a package:
packages = [{ include = "dotcat", from = "src" }, { include = "zsh" }]
Removed the original file from src/dotcat/install_completions.py

I've verified that everything works correctly:

The module can be imported directly: from zsh import install_completions
The module can be accessed through the dotcat package: import dotcat; dotcat.install_completions
This change makes more sense organizationally since the completion scri
debase. All tests are now passing.

Here's a summary of the changes made:

Updated imports in all three files to use tomllib instead of toml:

src/dotcat/version.py
src/dotcat/output_formatters.py
src/dotcat/parsers.py
Made necessary API adjustments:

Updated file handling to use binary mode with open("rb") since tomllib requires bytes input
Changed exception handling from toml.TomlDecodeError to tomllib.TOMLDecodeError
Implemented a custom TOML writer in output_formatters.py:

Since tomllib is read-only and doesn't provide writing capabilities
Created a comprehensive TOML writer that handles complex nested structures
Ensured the output format matches exactly what the tests expect
Updated tests:

Fixed mocked functions in test_version.py to use tomllib.load instead of toml.load
Updated pyproject.toml:

Removed the toml dependency from dev dependencies
Updated the Python requirement to >=3.11 since tomllib is only available in Python 3.11+
All tests are now passing, confirming that the migration from toml to tomllib was successful.
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 18, 2025

Codecov Report

Attention: Patch coverage is 89.74359% with 12 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/dotcat/output_formatters.py 81.13% 6 Missing and 4 partials ⚠️
src/dotcat/core.py 92.00% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@arthur-debert arthur-debert merged commit c2222f3 into main Mar 18, 2025
3 checks passed
@arthur-debert arthur-debert deleted the codeq branch March 18, 2025 04:01
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.

1 participant