Skip to main content

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

This project requires Python 3.12 or later.

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.

Running Tests

Run the test suite with pytest from the project root:

pytest

Contributing

Contributions are welcome! Here's how you can get started:

  1. Fork the repository.

  2. Create a new branch (git checkout -b feature-branch).

  3. Make your changes.

  4. Commit your changes (git commit -m 'Add feature').

  5. Push your changes to your fork (git push origin feature-branch).

  6. Create a pull request.

License

This project is released under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easy_exit_calls-1.0.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

easy_exit_calls-1.0.0-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file easy_exit_calls-1.0.0.tar.gz.

File metadata

  • Download URL: easy_exit_calls-1.0.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.14.5 Windows/11

File hashes

Hashes for easy_exit_calls-1.0.0.tar.gz
Algorithm Hash digest
SHA256 98be0323726a37b2481877a8a0c60a9e555f4495eeccbcade0953c504c0313e2
MD5 e0df21ea5523b57a7a619bb37bde1384
BLAKE2b-256 0cb6dc82ba808e714e2721fb157e3552b58e6c4ffa9dad94a2c781a6de07f400

See more details on using hashes here.

File details

Details for the file easy_exit_calls-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: easy_exit_calls-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.14.5 Windows/11

File hashes

Hashes for easy_exit_calls-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5c3336d8124cd8ae480e0b4f3e8debd72c40e7d46c556a58185fa64d16442bae
MD5 570c9db2833d8cd3e439c3c042f7bf3e
BLAKE2b-256 c140248643cb3c53b06043f0265b8a605316c57d74804f87a97d97c989287059

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page