You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

easy-exit-calls

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

easy-exit-calls

A Python library for managing exit handlers with enhanced features like decorators, UUID tracking, and LIFO execution.

1.0.0.dev1
pipPyPI
Maintainers
1

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.

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts