Skip to content

Refactor core/chdman.py for improved logging, structure, and safety#1

Merged
Soundchazer2k merged 1 commit intodev-1.2.0from
chdman-core-refactor
May 22, 2025
Merged

Refactor core/chdman.py for improved logging, structure, and safety#1
Soundchazer2k merged 1 commit intodev-1.2.0from
chdman-core-refactor

Conversation

@Soundchazer2k
Copy link
Copy Markdown
Owner

This commit implements a series of improvements to the CHDMAN wrapper module based on a detailed code review.

Key changes include:

  1. Logging: Replaced all print() statements and direct file writes (error.log) with Python's standard logging module. Implemented basic logging configuration.
  2. Refactoring CHDManWorker.run(): Broke down the large run() method into smaller, more manageable private helper methods (_perform_pre_flight_checks, _build_chdman_command, _execute_and_monitor_process, _handle_process_completion) to enhance readability and maintainability.
  3. Consolidated Compression Validation: Centralized compression algorithm validation and correction logic into a static method CHDManWorker._sanitize_compression_algorithms, removing duplicated code.
  4. Refactored CHDMan Instance Creation: Introduced a _create_and_start_instance helper method in CHDMan to streamline the creation and setup of CHDManWorker instances.
  5. Concurrency Safety:
    • Made the get_chd_manager() singleton initialization thread-safe using threading.Lock.
    • Added threading.Lock to protect access to CHDMan.active_instances.
  6. Style and Minor Fixes (PEP 8):
    • Addressed overly long lines.
    • Moved local imports to the module's top level where appropriate.
    • Standardized string quote usage.
    • Replaced os.system() calls with subprocess.run().
    • Removed an unused find_chdman() call in CHDManager.execute_all_operations().
    • Refined error handling for compression algorithm sanitization to consistently raise CHDManError.

These changes aim to make the module more robust, easier to debug, and more maintainable, while preserving existing functionality, including operations relevant to the compression tab, batch processor, and batch tab.

This commit implements a series of improvements to the CHDMAN wrapper module based on a detailed code review.

Key changes include:

1.  **Logging:** Replaced all `print()` statements and direct file writes (`error.log`) with Python's standard `logging` module. Implemented basic logging configuration.
2.  **Refactoring `CHDManWorker.run()`:** Broke down the large `run()` method into smaller, more manageable private helper methods (`_perform_pre_flight_checks`, `_build_chdman_command`, `_execute_and_monitor_process`, `_handle_process_completion`) to enhance readability and maintainability.
3.  **Consolidated Compression Validation:** Centralized compression algorithm validation and correction logic into a static method `CHDManWorker._sanitize_compression_algorithms`, removing duplicated code.
4.  **Refactored `CHDMan` Instance Creation:** Introduced a `_create_and_start_instance` helper method in `CHDMan` to streamline the creation and setup of `CHDManWorker` instances.
5.  **Concurrency Safety:**
    *   Made the `get_chd_manager()` singleton initialization thread-safe using `threading.Lock`.
    *   Added `threading.Lock` to protect access to `CHDMan.active_instances`.
6.  **Style and Minor Fixes (PEP 8):**
    *   Addressed overly long lines.
    *   Moved local imports to the module's top level where appropriate.
    *   Standardized string quote usage.
    *   Replaced `os.system()` calls with `subprocess.run()`.
    *   Removed an unused `find_chdman()` call in `CHDManager.execute_all_operations()`.
    *   Refined error handling for compression algorithm sanitization to consistently raise `CHDManError`.

These changes aim to make the module more robust, easier to debug, and more maintainable, while preserving existing functionality, including operations relevant to the compression tab, batch processor, and batch tab.
Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hi @Soundchazer2k! 👋

Your private repo does not have access to Sourcery.

Please upgrade to continue using Sourcery ✨

@Soundchazer2k Soundchazer2k merged commit 384ca47 into dev-1.2.0 May 22, 2025
1 check failed
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