
    Ti                     "   U d Z ddlmZmZ ddlmZ ddlmZmZm	Z	 	 ddl
mZmZmZ dZ ed       G d de             ZdefdZdae	e   ed<   defdZd Zy# e$ r/ dZ ed       G d	 d
             Z G d d      Z G d d      ZY dw xY w)z
Hook system integration for django-cronjob-utils.

This module provides hook context and hook manager for cronjob operations,
allowing external code to inject custom logic before and after cronjob execution.
    )	dataclassfield)date)AnyDictOptional)HookContextHookManagerHookRegistryTF)frozenc                   B    e Zd ZU dZeed<    ee      Ze	ee
f   ed<   y)r	   z<Stub HookContext when django-package-hooks is not installed.	operationdefault_factorymetadataN)__name__
__module____qualname____doc__str__annotations__r   dictr   r   r        !src/django_cronjob_utils/hooks.pyr	   r	      s!    J#(#>$sCx.>r   r	   c                       e Zd ZdZd Zd Zy)r   z=Stub HookRegistry when django-package-hooks is not installed.c                      y Nr   selfargskwargss      r   registerzHookRegistry.register       r   c                      y r   r   r   s      r   register_operationzHookRegistry.register_operation   r$   r   N)r   r   r   r   r#   r&   r   r   r   r   r      s    K		r   r   c                   "    e Zd ZdZd Zd Zd Zy)r
   z<Stub HookManager when django-package-hooks is not installed.c                 "    t               | _        y r   )r   registryr   s      r   __init__zHookManager.__init__#   s    (NDMr   c                      y r   r   r   s      r   execute_pre_hookszHookManager.execute_pre_hooks&   r$   r   c                      y r   r   r   s      r   execute_post_hookszHookManager.execute_post_hooks)   r$   r   N)r   r   r   r   r*   r,   r.   r   r   r   r
   r
   !   s    J	+		r   r
   c                       e Zd ZU dZdZeed<   dZeed<   dZe	e
   ed<   dZeed<   dZe	e   ed	<   d
Zeed<    ee      Zeeef   ed<   y)CronjobHookContexta  
    Context for cronjob hook execution.
    
    This context is passed to all hooks and contains information about
    the cronjob execution.
    
    Attributes:
        operation: The operation being performed ('execute')
        task_code: Unique code identifying the task
        task_name: Human-readable task name
        execution_date: Date for which the task is being executed
        execution_id: Database ID of the execution record (None for PRE hooks, set for POST hooks)
        execution_pattern: Execution pattern (STANDARD, ALWAYS, etc.)
        retry_count: Number of times this task has been retried
        options: Additional options passed to the task (force, rerun, etc.)
        metadata: Mutable dictionary for inter-hook communication
    
    Example:
        context = CronjobHookContext(
            operation='execute',
            task_code='A001',
            task_name='daily-report',
            execution_date=date(2024, 1, 15),
            execution_pattern='STANDARD'
        )
     	task_code	task_nameNexecution_dateSTANDARDexecution_patternexecution_idr   retry_countr   options)r   r   r   r   r2   r   r   r3   r4   r   r   r6   r7   intr8   r   r   r9   r   r   r   r   r   r0   r0   -   se    8 IsIs%)NHTN)'s'"&L(3-&K#D9GT#s(^9r   r0   returnc                  R    t         rt        dg      } t        |       S t               S )a#  
    Get the global hook manager for cronjob operations.
    
    This function returns a HookManager configured with the cronjob registry.
    The registry is initialized with the 'execute' operation.
    
    Returns:
        HookManager instance for executing cronjob hooks
    
    Example:
        manager = get_cronjob_hook_manager()
        context = CronjobHookContext(...)
        
        try:
            manager.execute_pre_hooks(context)
        except HookRejectionError as e:
            print(f"Task rejected: {e.error_code}")
    execute
operations)HOOKS_AVAILABLEr   r
   r)   s    r   get_cronjob_hook_managerrB   U   s(    * I;78$$ }r   N_cronjob_hook_managerc                  v    t         .t        rt        dg      } t        |       a t         S t               a t         S )a  
    Get the global singleton hook manager for cronjob operations.
    
    This ensures all cronjob tasks use the same hook registry, allowing
    hooks to be registered once and applied to all tasks.
    
    Returns:
        Global HookManager instance
    r=   r>   )rC   r@   r   r
   rA   s    r   get_global_cronjob_hook_managerrE   v   s<     $#	{;H$/$9! !  %0M!  r   c                  \    t         $t        rt         j                  j                          da y)z
    Clear all registered cronjob hooks.
    
    This is primarily useful for testing to ensure a clean slate between tests.
    N)rC   r@   r)   clearr   r   r   clear_cronjob_hooksrH      s$     (_&&,,. r   )r   dataclassesr   r   datetimer   typingr   r   r   django_package_hooksr	   r
   r   r@   ImportErrorr0   rB   rC   r   rE   rH   r   r   r   <module>rN      s    )  & &KKO< $$: $: $:N+ < 04 x, 3! !,	!y  Od? ? ?
 	 	#s   A 1BB