This library still works with some duct tape and love, but it has been unmaintained for about 5 years already and it's showing its age quite a bit.
Not only it's unmaintained, but the code is so old that it's pretty much unmaintainable, so it's time for a new library.
This library has quite a few outstanding bugs, like a badly indented line that subtly breaks a with block; like the fact that it still only supports ASCII; and even the name of the library itself, mega.py is not great, since with such a generic name, it's bound to cause conflicts with software that tries to import the library but that also contain a file with the same name. So it's time for a rewrite (not a refactor).
Please write me a very detailed plan on how to rewrite this library to be in line with modern python, modern design patterns, and a more organized and maintainable codebase.
Assistant
Okay, I understand. The mega.py library is showing its age and needs a complete rewrite, not just a refactor. Here's a detailed plan to create a modern, maintainable, and robust replacement, addressing the issues you've highlighted:
I. Goals and Principles
Modern Python: Use Python 3.8+ features, type hints, f-strings, pathlib, and async/await.
Maintainability:
Clear, concise, and well-documented code.
Modular design with well-defined responsibilities.
Comprehensive unit tests with high coverage.
Adherence to PEP 8 style guidelines.
Robustness:
Proper error handling with custom exceptions.
Retry mechanisms for transient API failures.
Input validation to prevent unexpected behavior.
Performance:
Efficient data handling and minimal memory usage.
Asynchronous operations for network I/O.
Usability:
Intuitive API design that is easy to use.
Clear and helpful error messages.
Avoid Name Clashes: Choose a unique and descriptive name for the library and its modules. e.g., megapy_client, megaclient, or py_mega. For this plan, let's call it megaclient.
Full Unicode support: Ensure that the library can handle any file name and other string data.
Ensure that all these functions have full unicode support and are fully tested for edge cases.
megaclient/core/ Layer:
auth.py:
Implement login methods (user and anonymous) using the api.client.py.
Manage session IDs, master keys, and other credentials.
Handle the RSA key exchange and session key derivation.
file_manager.py:
Implement file upload, download, and deletion logic.
IV. Key Improvements Over Original Library
Asynchronous Operations: Network operations using httpx async.
Type Hints: Static type checking with MyPy will catch errors early.
Modern Crypto: Use pycryptodome which is actively maintained.
Modular Design: Each part of the library has its specific role.
Clear Error Handling: Custom exceptions with detailed error messages.
Unicode support: Full Unicode compatibility
Well-defined API: Modern API, leveraging dataclasses for data types.
V. Development Process
Setup Project: Use Poetry or similar to create a new project, set up dependencies, and create the initial directory structure.
API Layer: Start by implementing the megaclient/api layer, focusing on client.py, exceptions.py, and models.py. Write tests for test_api.py.
Core Layer: Implement megaclient/core/auth.py, file_manager.py, and node_manager.py with tests in test_core.py.
Utils Layer: Implement the megaclient/utils/helpers.py with tests in test_utils.py.
Integration and Testing: Continuously test and integrate modules as they are developed.
Documentation: Generate API documentation with Sphinx or similar. Write user guides and examples for common use cases.
VI. Addressing Specific Issues
Badly Indented Line: This will be avoided by strictly following PEP8 style guidelines (e.g., using a linter like flake8 or pylint) and doing a thorough code review.
ASCII Only: Full unicode support will be implemented at all levels by properly encoding and decoding strings.
mega.py Name: A different, more descriptive name (megaclient or similar) has been chosen to avoid namespace conflicts.
VII. Continuous Integration/Continuous Deployment (CI/CD)
GitHub Actions: Create workflows for testing (running tests on each commit) and releasing the library to PyPI.
Code Analysis: Use linters and type checkers in the CI workflow to ensure code quality.
VIII. Timeline (Rough Estimate)
Phase 1 (API Layer): 2-3 weeks
Phase 2 (Core Layer): 3-4 weeks
Phase 3 (Utilities & Testing): 1-2 weeks
Phase 4 (Documentation, CI/CD): 1-2 weeks
This is a challenging task, but with a well-structured plan and a commitment to quality, you can create a modern, maintainable, and robust library to replace the old mega.py package. This plan is quite detailed and takes all the major pain points of the old library into consideration, while also trying to be flexible enough for future changes.
Utilize httpx for asynchronous file transfers and progress tracking.
Include methods for getting a public link.
node_manager.py:
Implement node listing, searching, creating folders, moving nodes, renaming nodes, and other metadata operations.
Ensure that folder creation works with all kinds of paths.
megaclient/utils/ Layer:
helpers.py:
Implement helper functions, such as URL parsing, base64 encoding/decoding, and other shared utilities (_parse_url, base64_url_decode, base64_url_encode, a32_to_base64, get_chunks, make_id).
Implement a proper file name sanitization function for multiple OS compatibility.
megaclient/tests/:
Use pytest for unit testing.
Use pytest-mock for mocking API responses.
Implement tests for all core and helper functions.
Strive for high test coverage and use parametrized tests and fixtures.