
    hD>                         d dl Z d dlZd dl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 d dlmZ d dlmZmZ d	Zd
 Zd Zd Z G d dej.                        ZddZy)    N)wraps)WampWebSocketClientFactory)WampRawSocketClientFactory)	component)TransportLost)Session)create_transport_serializerscreate_transport_serializer)	Componentrunc                 n    t               }| D cg c]  }||vs|j                  |      r| c}S c c}w )zM
    Return a list with unique elements from sequence, preserving order.
    )setadd)seqseenxs      V/var/www/Befach/backend/env/lib/python3.12/site-packages/autobahn/asyncio/component.py_unique_listr   .   s/     5D@!atmDHHQKA@@@s   	222c                 f    | j                  d      }|d   dj                  d |dd  D              z   S )N_r    c              3   <   K   | ]  }|j                           y wN)
capitalize).0ss     r   	<genexpr>z._camel_case_from_snake_case.<locals>.<genexpr>8   s     @alln@s      )splitjoin)snakepartss     r   _camel_case_from_snake_caser#   6   s4    KKE8bgg@eABi@@@@    c           
      B   |j                   dk(  r/t        |      }t        ||j                  ||j                        }n<|j                   dk(  r&t        |j                  d         }t        ||      }nJ d       |j                  j                         D ]  \  }}	  |j                  di ||i  |S # t        t        f$ r[ 	  |j                  di t        |      |i n9# t        t        f$ r' t        dj                  |j                   ||            w xY wY w xY w)	z3
    Create a WAMP-over-XXX transport factory.
    	websocket)urlserializersproxy	rawsocketr   )
serializershould not arrive herez"Unknown {} transport option: {}={} )typer	   r   r'   r)   r
   r(   r   optionsitemssetProtocolOptions	TypeErrorKeyErrorr#   
ValueErrorformat)loop	transportsession_factoryr(   factoryr+   kvs           r   _create_transport_factoryr<   ;   s3    ~~$29=,#//	
 
;	&01F1Fq1IJ
,_T 	0//u !!'')1	&G&&0!Q0 * N 8$ 	*** 215q9 x(  8??	PQSTU 	s*   B44DC"!D"6DDDc                   X    e Zd ZdZ ej
                         ZeZ	 d Z	d Z
d Zd ZddZy)	r   z
    A component establishes a transport and attached a session
    to a realm using the transport for communication.

    The transports a component tries to use can be configured,
    as well as the auto-reconnect strategy.
    c                 6    t        |t        j                        S )z"
        Internal helper.
        )
isinstancesslSSLError)selfes     r   _is_ssl_errorzComponent._is_ssl_errorr   s     !S\\**r$   c                     t        |t              rGd|v rB|d   }t        |t        t        f      ry t        |t        j                        ry t        d      y t        d      )Ntlsz?'tls' configuration must be a dict, bool or SSLContext instancezI'endpoint' configuration must be a dict or IStreamClientEndpoint provider)r?   dictboolr@   
SSLContextr4   )rB   endpointrF   s      r   _check_native_endpointz Component._check_native_endpointx   sj    h% uocD$<0S^^4$.  !  r$   c                 >   t        |||      }|j                  r|j                  j                  dd      }t	        |      t
        k7  r#t        dj                  t	        |                  |j                  ||j                  d   |j                  d         }t        j                  t        j                  ||            }| j                  |||      S |j                  d   d	k(  r|j                  j                  d
d      }	|	dvrt        dj                  |	            |j                  d   }
t	        |
      t        k7  r#t        dj                  t	        |
                  |j                  d   }t	        |      t
        k7  r#t        dj                  t	        |                  |j                  j                  dd      }t	        |      t
        k7  r#t        dj                  t	        |                  |j                  j                  dd      }d}|rt        |t              r|j!                         D ]   }|dvst        dj                  |             |j                  d|
      }t	        |      t        k7  rt        dj                  |            |j                  dd      }|}d}|yt#        j$                  t"        j&                  j(                  |      }nIt        |t"        j*                        r|
}n,|dv r|r&|
}n#t-        dj                  t	        |                  |j                  ||
|||      }t        j                  t        j                  ||            }| j                  |||      S |j                  d   dk(  r|j                  d   }t        |j                  j                  dd            }|j/                  ||      }t        j                  t        j                  ||            }| j                  |||      S J d       ) z?
        Create and connect a WAMP-over-XXX transport.
        timeout
   z<invalid type {} for timeout in client endpoint configurationhostport)protocol_factoryrO   rP   )rM   r.   tcpversion   )rT      z6invalid IP version {} in client endpoint configurationz9invalid type {} for host in client endpoint configurationz9invalid type {} for port in client endpoint configurationrF   N)hostname
trust_rootz Invalid key '{}' in 'tls' configrV   zAinvalid type {} for hostname in TLS client endpoint configurationrW   T)purposecafile)FTz4unknown type {} for "tls" configuration in transport)rQ   rO   rP   r@   server_hostnameunixpath)rQ   r\   r,   )r<   r)   rJ   getr.   intr4   r5   create_connectionasyncioensure_futurewait_for_wrap_connection_futurestrr?   rG   keysr@   create_default_contextPurposeSERVER_AUTHrI   RuntimeErrorcreate_unix_connection)rB   r6   r7   r8   doner9   rM   ftime_frS   rO   rP   rF   tls_hostnamer:   rV   
cert_fnamer\   s                     r   _connect_transportzComponent._connect_transport   s    ,D)_M
 ??((,,Y;GG}# !_!f!fgklsgt!uvv &&!(__V,__V, ' A
 **7+;+;Aw+OPF//	4HH'50((,,Y:Gf$ !Y!`!`ah!ijj%%f-DDzS  !\!c!cdhimdn!opp%%f-DDzS  !\!c!cdhimdn!opp((,,Y;GG}# !_!f!fgklsgt!uvv$$((5CL c4( XXZ$>>",-O-V-VWX-Y"ZZ (  #wwz48HH~,()l)s)st|)}~~!$t!<J#+LC!-!88$'KK$;$;#-
  S^^4#'LM)'+ '']'d'deijmen'opp&&!( , ' A **7+;+;Aw+OPF//	4HH'61%%f-D),,00B?@G++!( , A **7+;+;Aw+OPF//	4HH 4335r$   c                 |    fd}fd}t        j                  ||d        t        j                  |d |       |S )Nc                    | \  }}|j                         rBt        j                        s,t        |dd      }t        j                  t        |             y |j                  t              fd       }||_        y )N_onclose_reasonzConnection already closedc                      |       }t        j                        s#| t        d      } t        j                  |        |S )Nzfailed to complete connection)txaio	is_calledr   reject)failrtnrk   origs     r   lostzKComponent._wrap_connection_future.<locals>.on_connect_success.<locals>.lost  s=    4jt, |,-LMLLt,
r$   )
is_closingru   rv   getattrrw   r   connection_lostr   )resultr7   protoreasonr{   rz   rk   s        @r   on_connect_successz=Component._wrap_connection_future.<locals>.on_connect_success   sz    %Iu ##%t,$U,=?Z[FLL}V'<= ((D4[	 	 %)E!r$   c                 \    xj                   dz  c_         t        j                  |        y )Nr   )connect_failuresru   rw   )errrk   r7   s    r   on_connect_failurez=Component._wrap_connection_future.<locals>.on_connect_failure  s"    &&!+&LLs#r$   )ru   add_callbacks)rB   r7   rk   conn_fr   r   s    ``   r   rc   z!Component._wrap_connection_future   s=    &	)P	$
 	F$6= 	FD*<=r$   Nc                     |/| j                   j                  d       t        j                         }| j	                  |      S )a~  
        This starts the Component, which means it will start connecting
        (and re-connecting) to its configured transports. A Component
        runs until it is "done", which means one of:
        - There was a "main" function defined, and it completed successfully;
        - Something called ``.leave()`` on our session, and we left successfully;
        - ``.stop()`` was called, and completed successfully;
        - none of our transports were able to connect successfully (failure);

        :returns: a Future which will resolve (to ``None``) when we are
            "done" or with an error if something went wrong.
        zUsing default loop)r6   )logwarnr`   get_event_loop_start)rB   r6   s     r   startzComponent.start$  s9     <HHMM./))+D{{{%%r$   r   )__name__
__module____qualname____doc__ru   make_loggerr   r   r8   rD   rK   rp   rc   r   r-   r$   r   r   r   b   s?     %


CO+(`4D3l&r$   r   c                    |t        j                  |       t        j                         j	                         rPt        j
                  t        j                                t        j                         t         j                  _        t        j                         fd	 j                  t        j                  fd       j                  t        j                  fd       d }t        j                   | |       |r"	 j#                          j'                          yy# t        $ r Y Jw xY w# t        j$                  $ r Y 6w xY w)a.  
    High-level API to run a series of components.

    This will only return once all the components have stopped
    (including, possibly, after all re-connections have failed if you
    have re-connections enabled). Under the hood, this calls

    XXX fixme for asyncio

    -- if you wish to manage the loop yourself, use the
    :meth:`autobahn.asyncio.component.Component.start` method to start
    each component yourself.

    :param components: the Component(s) you wish to run
    :type components: instance or list of :class:`autobahn.asyncio.component.Component`

    :param start_loop: When ``True`` (the default) this method
        start a new asyncio loop.
    :type start_loop: bool

    :param log_level: a valid log-level (or None to avoid calling start_logging)
    :type log_level: string
    N)levelc                   K   j                  d|        	 t        j                  j                         }|D ]6  }	 t        j                  j                         }||us'|j                          8 fd}t        j                  | }|j                  |       y # t        $ r t        j                         }Y w xY w# t        $ r t        j
                         }Y w xY ww)NzShutting down due to {signal})signalc                    	 | j                          j                          y # t        j                  $ r j                  d       Y 7t        $ r}j                  d|       Y d }~Wd }~ww xY w# j                          w xY w)NzAll task cancelledz&Error while shutting down: {exception})	exception)r   r`   CancelledErrordebug	Exceptionerrorstop)futrC   r   r6   s     r   cancel_all_callbackz5run.<locals>.nicely_exit.<locals>.cancel_all_callbacky  sk    

 		 )) 0		./ Q		Ba	PPQ 		s3   $ $A/A2 
A/A*%A2 *A//A2 2B)	infor`   Task	all_tasksAttributeErrorcurrent_taskcancelgatheradd_done_callback)r   taskstaskr   r   r   r   r6   s         r   nicely_exitzrun.<locals>.nicely_exite  s     0@	(LL**,E
 D6&||88: <' 	 nne$125  	(%%'E	( " 6&3356sP   C!B C!B>C!<C!B;8C!:B;;C!>CC!CC!c                  :    t        j                    d            S )NSIGINTr`   ra   r   s   r   <lambda>zrun.<locals>.<lambda>  s    w7L7L[YaMb7cr$   c                  :    t        j                    d            S )NSIGTERMr   r   s   r   r   zrun.<locals>.<lambda>  s    8M8MkZcNd8er$   c                 $    | j                          y r   )r   )r6   args     r   done_callbackzrun.<locals>.done_callback  s    		r$   )ru   start_loggingr`   r   	is_closedset_event_loopnew_event_loopconfigr6   r   add_signal_handlerr   r   r   NotImplementedErrorr   _runrun_foreverr   close)
components
start_loop	log_levelr   r   r6   r   s       @@@r   r   r   9  s   8 ),!!#D~~w5578%%' 



C3B/cd0ef
 NN4]3	 	

     %% 		s%   +AD0 D? 0	D<;D<?EE)Tr   )r`   r@   r   	functoolsr   ru   autobahn.asyncio.websocketr   autobahn.asyncio.rawsocketr   autobahn.wampr   autobahn.wamp.exceptionr   autobahn.asyncio.wampr   autobahn.wamp.serializerr	   r
   __all__r   r#   r<   r   r   r-   r$   r   <module>r      s\   6  
    A A # 1 ) ^ AA
$NT&	## T&nhr$   