A Python library for managing exit handlers with enhanced features like decorators, UUID tracking, and LIFO execution.
Project description
easy-exit-calls
A Python library for managing exit handlers with enhanced features like decorators, UUID tracking, and LIFO execution.
Features
- Decorator Support: Easily register functions as exit handlers using a decorator.
- LIFO Execution: Handlers execute in Last-In-First-Out order by default (configurable).
- UUID Tracking: Each handler is assigned a unique UUID for easy management.
- Thread Safety: Uses threading locks to ensure safe registration/unregistration.
- Detailed Logging: Integrated with a logging engine for debugging and tracking.
- Error Handling: Captures exceptions during exit and logs them with tracebacks.
Installation
pip install easy-exit-calls
Usage
Basic Decorator Usage
from easy_exit_calls import register_exit_handler
@register_exit_handler
def cleanup():
print("Cleaning up resources...")
# Exiting the program will automatically trigger this handler.
Decorator with Arguments
from easy_exit_calls import register_exit_handler
@register_exit_handler("arg1", key="value")
def cleanup_with_args(arg1, key=None):
print(f"Cleaning up with {arg1} and {key}")
# Handler called with provided args/kwargs on exit.
Manual Registration
from easy_exit_calls import ExitCallHandler
def manual_cleanup():
print("Manual cleanup")
ExitCallHandler().register_handler(manual_cleanup)
# Exiting the program will manually trigger this handler.
Unregistering Handlers
from easy_exit_calls import ExitCallHandler
def cleanup():
print("Cleaning up resources...")
handler_uuid = ExitCallHandler().register_handler(cleanup)
# Unregister the handler by UUID.
ExitCallHandler().unregister_by_uuid(handler_uuid)
Execution Order
from easy_exit_calls import register_exit_handler
@register_exit_handler
def first_handler():
print("First handler")
@register_exit_handler
def second_handler():
print("Second handler")
# Output on exit:
# Second handler
# First handler
# (LIFO execution order)
API Reference
ExitCallHandler
Singleton class managing exit handlers.
Methods:
register_handler(func: Callable, *args, **kwargs) -> str: Register a new exit handler with optional args/kwargs. Returns the UUID of the handler.unregister_by_uuid(uuid): Unregister a handler by UUID.unregister_handler(func, *args, **kwargs): Unregister a handler by function reference and optional args/kwargs.call_handlers(): Manually call all registered handlers.
register_exit_handler
Decorator for registering a function as an exit handler.
Parameters:
*handler_args: Optional arguments to pass to the handler function.**handler_kwargs: Optional keyword arguments to pass to the handler function.
Contributing
Contributions are welcome! Here's how you can get started:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch). - Make your changes.
- Commit your changes (
git commit -m 'Add feature'). - Push your changes to your fork (
git push origin feature-branch). - Create a pull request.
License
This project is released under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file easy_exit_calls-1.0.0.dev1.tar.gz.
File metadata
- Download URL: easy_exit_calls-1.0.0.dev1.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.12.7 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0602f9e4b8611a5666c8a79637598f50b840111447bc7b2b26629e022dc39051
|
|
| MD5 |
7b58d988f8e41120e0acb4eb75dbf595
|
|
| BLAKE2b-256 |
3a1b043387dd334739b92c695c28ff3c0ff5e897da8123a5b235e5043237075b
|
File details
Details for the file easy_exit_calls-1.0.0.dev1-py3-none-any.whl.
File metadata
- Download URL: easy_exit_calls-1.0.0.dev1-py3-none-any.whl
- Upload date:
- Size: 18.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.12.7 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5d586f64d50d742031045c8f2dd960aa3f93b000601495644c1dec5bc466112
|
|
| MD5 |
0a0302c2ee146573a59e3a396683793d
|
|
| BLAKE2b-256 |
5d1b08917cd5ef9465a824020834233fdf67cdcb3d2cab3d24850d4f8a15f3de
|