
    ܖi("                        d Z ddlmZ ddlZddlmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZmZmZ dd
lmZ ddlmZ ddlmZ  G d d      Z G d de      Z G d d      Z G d de      Zy)z
Interfaces for for propagating feedback from the API to provide responsive progress indicators as
well as a progress spinner implementation for use with CLI applications.
    )annotationsN)ABCabstractmethod)Sequence)MemoryHandler)Any)	StyleType)ConsoleGroupRenderableType)Live)Panel)Spinnerc                  X    e Zd ZdZg dd
dZdddZddZddZddZ	 	 	 	 	 	 	 	 dd	Z	y)
AuditStatea  
    An object that handles abstract "updates" to `pip-audit`'s state.

    Non-UI consumers of `pip-audit` (via `pip_audit`) should have no need for
    this class, and can leave it as a default construction in whatever signatures
    it appears in. Its primary use is internal and UI-specific: it exists solely
    to give the CLI enough state for a responsive progress indicator during
    user requests.
    )membersc                   || _         y)zG
        Create a new `AuditState` with the given member list.
        N)_members)selfr   s     T/var/www/html/content-pipeline/venv/lib/python3.12/site-packages/pip_audit/_state.py__init__zAuditState.__init__    s    
      Nc                J    | j                   D ]  }|j                  ||        y)z
        Called whenever `pip_audit`'s internal state changes in a way that's meaningful to
        expose to a user.

        `message` is the message to present to the user.
        N)r   update_state)r   messagelogsmembers       r   r   zAuditState.update_state'   s'     mm 	/F.	/r   c                F    | j                   D ]  }|j                           y)zB
        Called when `pip-audit`'s state is initializing.
        N)r   
initializer   r   s     r   r   zAuditState.initialize2   s#    
 mm 	 F	 r   c                F    | j                   D ]  }|j                           y)zE
        Called when `pip_audit`'s state is "done" changing.
        N)r   finalizer    s     r   r"   zAuditState.finalize:   s!     mm 	FOO	r   c                &    | j                          | S )zb
        Create an instance of the `pip-audit` state for usage within a `with` statement.
        )r   r   s    r   	__enter__zAuditState.__enter__A   s    
 	r   c                $    | j                          y)z
        Helper to ensure `finalize` gets called when the `pip-audit` state falls out of scope of a
        `with` statement.
        N)r"   )r   	_exc_type
_exc_value_exc_tracebacks       r   __exit__zAuditState.__exit__I   s     	r   )r   zSequence[_StateActor]Nr   strr   
str | NonereturnNoner/   r0   )r/   r   )r'   r   r(   r   r)   r   r/   r0   )
__name__
__module____qualname____doc__r   r   r   r"   r%   r*    r   r   r   r      sJ     <>  	/ *-?B	r   r   c                  D    e Zd Zeddd       Zedd       Zedd       Zy)_StateActorNc                    t         r+   NotImplementedErrorr   r   r   s      r   r   z_StateActor.update_stateT   s    !!r   c                    t         )z
        Called when `pip-audit`'s state is initializing. Implementors should
        override this to do nothing if their state management requires no
        initialization step.
        r:   r$   s    r   r   z_StateActor.initializeX   
     "!r   c                    t         )z
        Called when the overlaying `AuditState` is "done," i.e. `pip-audit`'s
        state is done changing. Implementors should override this to do nothing
        if their state management requires no finalization step.
        r:   r$   s    r   r"   z_StateActor.finalizea   r>   r   r+   r,   r1   )r2   r3   r4   r   r   r   r"   r6   r   r   r8   r8   S   s<    " " " " " "r   r8   c                  ~    e Zd ZdZdZdddddd	 	 	 	 	 	 	 	 	 	 	 dd	Zedd
       Z	 	 	 	 	 	 ddZddZ	ddZ
ddZy)	StatusLogz
    Displays a status indicator with an optional log panel to display logs
    for external processes.

    This code is based off of Rich's `Status` component:
        https://github.com/Textualize/rich/blob/master/rich/status.py
    
   Ndotszstatus.spinnerg      ?g      )@)consolespinnerspinner_stylespeedrefresh_per_secondc                   t        ||||      | _        t        d| j                        | _        t        | j                  ||d      | _        y)a  
        Construct a new `StatusLog`.

        `status` is the status message to display next to the spinner.
        `console` is the Rich console to display the log status in.
        `spinner` is the name of the spinner animation (see python -m rich.spinner). Defaults to `dots`.
        `spinner_style` is the style of the spinner. Defaults to `status.spinner`.
        `speed` is the speed factor for the spinner animation. Defaults to 1.0.
        `refresh_per_second` is the number of refreshes per second. Defaults to 12.5.
        )textstylerG    )heightT)rD   rH   	transientN)r   _spinnerr   LOG_PANEL_HEIGHT
_log_panelr   
renderable_live)r   statusrD   rE   rF   rG   rH   s          r   r   zStatusLog.__init__y   sG    *  fMQVW4+@+@AOO1	

r   c                    | j                   j                  r t        | j                  | j                         S | j                  S )z
        Create a Rich renderable type for the log panel.

        If the log panel contains text, we should create a group and place the
        log panel underneath the spinner.
        )rQ   rR   r   rO   r$   s    r   rR   zStatusLog.renderable   s0     ??%%88}}r   c                   |d}n-dj                  |j                         | j                   d       }| j                  j	                  |       || j
                  _        | j                  j	                  | j                  d       y)z)
        Update status and logs.
        NrL   
)rJ   T)refresh)join
splitlinesrP   rO   updaterQ   rR   rS   )r   rT   r   s      r   r[   zStatusLog.update   sr     <D 99T__.0E0E/E/GHID&)%)"

$//48r   c                8    | j                   j                          y)z-
        Start the status animation.
        N)rS   startr$   s    r   r]   zStatusLog.start   s    
 	

r   c                8    | j                   j                          y)z-
        Stop the spinner animation.
        N)rS   stopr$   s    r   r_   zStatusLog.stop   s    
 	

r   c                    | j                   S )z4
        Convert to a Rich renderable type.
        )rR   r$   s    r   __rich__zStatusLog.__rich__   s    
 r   )rT   r-   rD   zConsole | NonerE   r-   rF   r	   rG   floatrH   rb   )r/   r   )rT   r-   r   r.   r/   r0   r1   )r2   r3   r4   r5   rP   r   propertyrR   r[   r]   r_   ra   r6   r   r   rA   rA   k   s      #'#3$(

  	

 
 !
 
 "
< 
 
99 9 
	9$r   rA   c                  4    e Zd ZdZdddZd	d
dZddZddZy)AuditSpinnerzg
    A progress spinner for `pip-audit`, using `rich.status`'s spinner support
    under the hood.
    c                    t               | _        t        || j                  dd      | _        t	        dt
        j                  dd      | _        g | _        y)z0
        Initialize the `AuditSpinner`.
        line   )rD   rE   rH   r   NF)
flushLeveltargetflushOnClose)	r
   _consolerA   rO   r   loggingERRORlog_handlerprev_handlers)r   r   s     r   r   zAuditSpinner.__init__   sL    
  	!T]]Fr
 )'--5
 57r   Nc                <    | j                   j                  ||       y)z-
        Update the spinner's state.
        N)rO   r[   r<   s      r   r   zAuditSpinner.update_state   s    
 	Wd+r   c                *   t         j                  }|j                  D ]  }| j                  j	                  |        | j                  D ]  }|j                  |        |j                  | j                         | j                  j                          y)z
        Redirect logging to an in-memory log handler so that it doesn't get mixed in with the
        spinner output.
        N)
rm   roothandlersrp   appendremoveHandler
addHandlerro   rO   r]   )r   root_loggerhandlers      r   r   zAuditSpinner.initialize   s     ll"++ 	/G%%g.	/)) 	/G%%g.	/ 	t//0r   c                   | j                   j                          t        j                  }t        j                         }|j                  t        j                  t        j                               | j                  j                  |       | j                  j                          |j                  | j                         | j                  D ]  }|j                  |        y)z
        Cleanup the spinner output so it doesn't get combined with subsequent `stderr` output and
        flush any logs that were recorded while the spinner was active.
        N)rO   r_   rm   rs   StreamHandlersetFormatter	FormatterBASIC_FORMATro   	setTargetflushrv   rp   rw   )r   rx   stream_handlerry   s       r   r"   zAuditSpinner.finalize   s     	 ll ..0##G$5$5g6J6J$KL"">2  	!!$"2"23)) 	,G""7+	,r   )rL   )r   r-   r/   r0   r+   r,   r1   )r2   r3   r4   r5   r   r   r   r"   r6   r   r   re   re      s    
7$,*,r   re   )r5   
__future__r   rm   abcr   r   collections.abcr   logging.handlersr   typingr   
rich.alignr	   rich.consoler
   r   r   	rich.liver   
rich.panelr   rich.statusr   r   r8   rA   re   r6   r   r   <module>r      sb   
 #  # $ *    7 7   ; ;|"# "0^ ^BF,; F,r   