
    Pi                     V   d Z ddlmZ ddl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  ed	           G d
 d                      Z ed	           G d d                      Z G d de          ZdddddZd"d#dZd$dZ e            Zd%dZ ed          d&d!            ZdS )'z
CLI-specific command handlers for Dippy.

Each handler module exports:
- COMMANDS: list[str] - command names this handler supports
- classify(ctx: HandlerContext) -> Classification - classify command for approval
    )annotationsN)	dataclass)	lru_cache)Path)LiteralOptionalProtocolT)frozenc                      e Zd ZU dZded<   dS )HandlerContextzContext passed to handlers.	list[str]tokensN)__name__
__module____qualname____doc____annotations__     3/root/projects/gits/Dippy/src/dippy/cli/__init__.pyr   r      s$         %%r   r   c                  V    e Zd ZU dZded<   dZded<   dZded<   dZd	ed
<   dZded<   dS )ClassificationzResult of classifying a command.

    Handlers return this to indicate:
    - allow: command is safe, no further checking needed
    - ask: command needs user confirmation
    - delegate: check inner_command to determine safety
    z#Literal['allow', 'ask', 'delegate']actionNz
str | Noneinner_commanddescriptionr   ztuple[str, ...]redirect_targetsFboolremote)	r   r   r   r   r   r   r   r   r   r   r   r   r   r      s}           0/// $M$$$$"K"""" 	      Fr   r   c                      e Zd ZdZddZdS )	
CLIHandlerz!Protocol for CLI handler modules.ctxr   returnr   c                    dS )zClassify command for approval.

        Args:
            ctx: Handler context containing command tokens

        Returns Classification with action and optional description.
        Nr   )selfr!   s     r   classifyzCLIHandler.classify/   s	     	r   N)r!   r   r"   r   )r   r   r   r   r%   r   r   r   r    r    ,   s.        ++     r   r          )awsgcloudgsutilazr   r   handler_namestrr"   c                    | sdS | d         }t          |p|          }|r%t          |d          r|                    |           S t                              |p|d          }d                    | d|                   S )z6Compute description from tokens based on handler type.unknownr   get_descriptionr'    N)get_handlerhasattrr0   DESCRIPTION_DEPTHgetjoin)r   r,   basehandlerdepths        r   r0   r0   D   s     y !9D,.$//G /77$566 /&&v...!!,"6$::E88F6E6N###r   dict[str, str]c                 6   i } t          t                    j        }|                    d          D ]g}|j                            d          r|j        }	 t          j        d| d          }t          |dg           D ]}|| |<   X# t          $ r Y dw xY w| S )z=Discover handler modules and build command -> module mapping.z*.py_.	dippy.clipackageCOMMANDS)r   __file__parentglobname
startswithstem	importlibimport_modulegetattrImportError)handlerscli_dirfilemodule_namemodulecmds         r   _discover_handlersrR   S   s    H8nn#GV$$ 	 	9$$ 	i	,->->->TTTFvz266 , , +, 	 	 	H	Os   2B		
BBcommand_nameOptional[CLIHandler]c                \    t                               |           }|sdS t          |          S )zk
    Get the handler module for a CLI command.

    Returns None if no handler exists for the command.
    N)KNOWN_HANDLERSr5   _load_handler)rS   rO   s     r   r2   r2   h   s2     !$$\22K t%%%r       )maxsizerO   c                X    	 t          j        d|  d          S # t          $ r Y dS w xY w)z:Load a CLI handler module by name (cached within process).r=   r>   r?   N)rH   rI   rK   )rO   s    r   rW   rW   u   sH    &'8;'8'8+NNNN   tts    
)))N)r   r   r,   r-   r"   r-   )r"   r:   )rS   r-   r"   rT   )rO   r-   r"   rT   )r   
__future__r   rH   dataclassesr   	functoolsr   pathlibr   typingr   r   r	   r   r   r    r4   r0   rR   rV   r2   rW   r   r   r   <module>r`      s    # " " " " "     ! ! ! ! ! !             . . . . . . . . . . $        $       $       " 
	  $ $ $ $ $   $ $#%%
& 
& 
& 
& 2     r   