
    0j                         d Z ddlZddlmZmZmZmZmZ ddlm	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mZ ddlmZmZmZmZmZ  ej         e!          Z" G d d          Z#dS )z[
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.
    N)AnyListOptionalcastget_type_hints)	providers)ActivityInvokeResponse)EventEmitter   )EventManager)ActivityProcessor)	Container)ActivityEvent
ErrorEvent	EventTypeis_registered_event)DependencyMetadataEventMetadata
PluginBasePluginErrorEventget_metadatac            
           e Zd ZdZdededee         defdZ	de
e         de
e         fd	Zd
edee         fdZdeddfdZdedededdfdZdededededdf
dZdS )PluginProcessora  
    Processes plugins as apart of the Teams app.

    This class is responsible for initializing plugins, injecting dependencies, and handling events.
    It uses dependency injection to provide plugins with the necessary dependencies and event handlers.
    	containerevent_managerevent_emitteractivity_processorc                 L    g | _         || _        || _        || _        || _        d S N)pluginsr   r   r   r   )selfr   r   r   r   s        h/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/microsoft_teams/apps/app_plugins.py__init__zPluginProcessor.__init__$   s0     *,"**"4    r!   returnc                 X   |D ]}t          t          |                    }|j        }|j        j        }t
                              d|            |st          d| d          |                     |          rt          d| d          | j	        
                    |           | j                            |t          j        |                     ||k    r-| j                            |t          j        |                     t
                              d           | j	        S )z1Initializes and adds all the plugins for the app.zInitializing the plugin zPlugin z missing name in metadatazduplicate plugin z foundz$Successfully initialized all plugins)r   typename	__class____name__loggerdebug
ValueError
get_pluginr!   appendr   set_providerr   Object)r"   r!   pluginmetadatar)   
class_names         r#   initialize_pluginsz"PluginProcessor.initialize_plugins1   s*     	R 	RF#DLL11H=D)2JLL@J@@AAA R !P:!P!P!PQQQt$$ C !AT!A!A!ABBBL'''N''i.>v.F.FGGGT!!++J	8H8P8PQQQ;<<<|r%   r)   c                 p    | j         D ]-}t          t          |                    }|j        |k    r|c S .dS )zGets the plugin by name.N)r!   r   r(   r)   )r"   r)   r3   r4   s       r#   r/   zPluginProcessor.get_pluginJ   sK    l 	 	F#DLL11H}$$ %	 	r%   r3   Nc                    t          |j        d          }|                                D ]\  }}t          |dd          }t          |dd          }|D ]}t                              d| d|j        j                    t          |t                    r| 	                    |||           Yt          |t                    r|                     ||||           dS )	z0Injects dependencies and events into the plugin.T)include_extras
__origin__N__metadata__ zInitializing the dependency z for )r   r*   itemsgetattrr,   r-   r+   
isinstancer   _inject_eventr   _inject_dependency)r"   r3   hints
field_nameannotated_typeoriginr4   metas           r#   injectzPluginProcessor.injectQ   s    v/EEE*/++-- 
	N 
	N&J^\4@@F~~rBBH  N NhJhhVM]MfhhiiidM22 N&&tVZ@@@@&899 N++D&&*MMMN	
	N 
	Nr%   rF   rC   c                     |j         dk    r<t                              d           dt          ddf fd}t	          |||           dS |j         dk    rLt                              d           dt
          dt          t                   f fd	}t	          |||           dS |j         d
k    rCt                              d           dt          dt          ddf fd}t	          |||           dS dS )z=Injects event handler into the plugin based on metadata info.errorzInjecting the error eventeventr&   Nc                    K   t          t          | j                  }j                            t          | j        |          j                   d {V  d S )N)rI   activity)r   r	   rL   r   on_errorr   rI   r!   )rJ   rL   r"   s     r#   error_handlerz4PluginProcessor._inject_event.<locals>.error_handlerg   s]      %.99(11*5;Ya2b2b2bdhdpqqqqqqqqqqqr%   rL   zInjecting the activity eventc                    K   j                             |            d {V  j                            j        |            d {V S r    )r   on_activityr   process_activityr!   )rJ   r"   s    r#   activity_handlerz7PluginProcessor._inject_event.<locals>.activity_handlero   s^      (44U;;;;;;;;;!4EEdlTYZZZZZZZZZr%   customzInjecting the custom eventr)   c                    K   t          |           r t                              d|  d           d S j                            | |           d S )Nzevent zB is reserved by core app-events but an plugin is trying to emit it)r   r,   warningr   emit)r)   rJ   r"   s     r#   custom_handlerz5PluginProcessor._inject_event.<locals>.custom_handlerw   sY      &t,, NN#tD#t#t#tuuuF"''e44444r%   )	r)   r,   r-   r   setattrr   r
   r   str)r"   rF   r3   rC   rN   rR   rW   s   `      r#   r@   zPluginProcessor._inject_eventb   s[   9LL4555r+; r r r r r r r FJ66666Y*$$LL7888[m [s@S [ [ [ [ [ [ FJ(899999Y(""LL566653 5s 5t 5 5 5 5 5 5 FJ77777 #"r%   	type_namec                 b   d}|rt          | j        |j        d          }|st          | j        |d          }|s0|j        s't	          d| d| dd|j        j         dz             dS  |            }t          |||           t                              d| d|j        j                    dS )	z:Injects dependency into the plugin based on metadata info.Nzdependency of z of property z not found zbut plugin z depends on itz%Successfully injected the dependency z into )	r>   r   r+   optionalr.   r*   rX   r,   r-   )r"   rF   r3   rZ   rC   
dependencys         r#   rA   z"PluginProcessor._inject_dependency   s    
 	K 1CTJJJ 	C TBBJ 
	p=  TYTTZTTTMF$4$=MMMN    $JFJ
333LLnnnSYScSlnnooooor%   )r+   
__module____qualname____doc__r   r   r   r   r   r$   r   r   r6   rY   r   r/   rG   r   r@   r   r   rA   r<   r%   r#   r   r      sP        55 $5 $I.	5
 .5 5 5 5$z*: tJ?O    2s x
';    NZ ND N N N N"8- 8 8QT 8Y] 8 8 8 8:p'9 p: pZ] pkn psw p p p p p pr%   r   )$r`   loggingtypingr   r   r   r   r   dependency_injectorr   microsoft_teams.apir	   r
   microsoft_teams.commonr   
app_eventsr   app_processr   r   r   eventsr   r   r   r   r!   r   r   r   r   r   	getLoggerr+   r,   r   r<   r%   r#   <module>rj      sz   
  < < < < < < < < < < < < < < ) ) ) ) ) ) 8 8 8 8 8 8 8 8 / / / / / / $ $ $ $ $ $ * * * * * *             M M M M M M M M M M M M              
	8	$	$up up up up up up up up up upr%   