
    5                         S r  SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKJ	r	  SSK
JrJr  SSK7  SSKJr  S/r " S	 S
5      r " S S5      r " S S\5      rg)aL  
websocket - WebSocket client library for Python

Copyright (C) 2010 Hiroki Ohtani(liris)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA  02110-1335  USA

    N   )ABNF)	WebSocketgetdefaulttimeout)*)_loggingWebSocketAppc                        \ rS rSrS rS rSrg)
Dispatcher+   c                     Xl         X l        g Nappping_timeoutselfr   r   s      !lib/third_party/websocket/_app.py__init__Dispatcher.__init__,       (    c                 ^   U R                   R                  R                  (       a  [        R                  " U R                   R                  R                  4SSU R                  5      u  pEnU(       a  U" 5       (       d  g U" 5         U R                   R                  R                  (       a  M  g g N )r   sock	connectedselectr   )r   r   read_callbackcheck_callbackrwes          r   readDispatcher.read0   so    hhmm%%mmXX]]''*BD4E4EGGA!$ hhmm%%%r   r   N)__name__
__module____qualname____firstlineno__r   r$   __static_attributes__r   r   r   r   r   +   s    )r   r   c                   &    \ rS rSrS rS rS rSrg)SSLDispacther9   c                     Xl         X l        g r   r   r   s      r   r   SSLDispacther.__init__:   r   r   c                     U R                   R                  R                  (       aS  U R                  5       nU(       a  U" 5       (       d  g U" 5         U R                   R                  R                  (       a  MR  g g r   )r   r   r   r   )r   r   r   r    r!   s        r   r$   SSLDispacther.read>   sG    hhmm%%A$ hhmm%%%r   c                     U R                   R                  R                  nUR                  5       (       a  U/$ [        R                  " U4SSU R                  5      u  p#nU$ r   )r   r   pendingr   r   )r   r   r!   r"   r#   s        r   r   SSLDispacther.selectF   sL    xx}}!!<<>>7N--"b$2C2CDar   r   N)r&   r'   r(   r)   r   r$   r   r*   r   r   r   r,   r,   9   s    )r   r,   c                       \ rS rSrSr       SS jr\R                  4S jrS r	S r
       SS jrS	 rS
 rS rSrg)r	   N   zW
Higher level of APIs are provided.
The interface is like JavaScript WebSocket object.
Nc                     Xl         Ub  UO/ U l        Xl        X0l        X@l        Xl        XPl        X`l        Xpl        Xl	        Xl
        SU l        Xl        SU l        SU l        SU l        Xl        g)a  
url: websocket url.
header: custom header for websocket handshake.
on_open: callable object which is called at opening websocket.
  this function has one argument. The argument is this class object.
on_message: callable object which is called when received data.
 on_message has 2 arguments.
 The 1st argument is this class object.
 The 2nd argument is utf-8 string which we get from the server.
on_error: callable object which is called when we get error.
 on_error has 2 arguments.
 The 1st argument is this class object.
 The 2nd argument is exception object.
on_close: callable object which is called when closed the connection.
 this function has one argument. The argument is this class object.
on_cont_message: callback object which is called when receive continued
 frame data.
 on_cont_message has 3 arguments.
 The 1st argument is this class object.
 The 2nd argument is utf-8 string which we get from the server.
 The 3rd argument is continue flag. if 0, the data continue
 to next frame data
on_data: callback object which is called when a message received.
  This is called before on_message or on_cont_message,
  and then on_message or on_cont_message is called.
  on_data has 4 argument.
  The 1st argument is this class object.
  The 2nd argument is utf-8 string which we get from the server.
  The 3rd argument is data type. ABNF.OPCODE_TEXT or ABNF.OPCODE_BINARY will be came.
  The 4th argument is continue flag. if 0, the data continue
keep_running: this parameter is obsolete and ignored.
get_mask_key: a callable to produce new mask keys,
  see the WebSocket.set_mask_key's docstring for more information
subprotocols: array of available sub protocols. default is None.
NFr   )urlheadercookieon_open
on_messageon_dataon_erroron_closeon_pingon_pongon_cont_messagekeep_runningget_mask_keyr   last_ping_tmlast_pong_tmsubprotocols)r   r8   r9   r;   r<   r>   r?   r@   rA   rB   rC   rD   r:   rG   r=   s                  r   r   WebSocketApp.__init__T   sq    T  & 2f$  .!(	(r   c                 z    U R                   (       a  U R                   R                  X5      S:X  a  [        S5      eg)z
send message.
data: message to send. If you set opcode to OPCODE_TEXT,
      data must be utf-8 string or unicode.
opcode: operation code of data. default is OPCODE_TEXT.
r   zConnection is already closed.N)r   send"WebSocketConnectionClosedException)r   dataopcodes      r   rJ   WebSocketApp.send   s4     yyDIINN48A=4/1 1 >r   c                 |    SU l         U R                  (       a$  U R                  R                  " S0 UD6  SU l        gg)z
close websocket connection.
FNr   )rC   r   close)r   kwargss     r   rP   WebSocketApp.close   s2     "99IIOO%f%DI r   c                 f   UR                  U5      (       d_  [        R                  " 5       U l        U R                  (       a   U R                  R	                  5         UR                  U5      (       d  M^  g g ! [
         a/  n[        R                  " SR                  U5      5         S nAg S nAff = f)Nz send_ping routine terminated: {})	waittimerE   r   ping	Exceptionr   warningformat)r   intervaleventexs       r   
_send_pingWebSocketApp._send_ping   sy    **X&& $		DyyIINN$	 **X&&
 ! $$%G%N%Nr%RSs   A7 7
B0%B++B0c                   ^ ^^^^ Tb  TS::  a  SmT(       a  U(       a  UT::  a  [        S5      eU(       d  / nU(       d  0 nT R                  (       a  [        S5      eSmST l        ST l        ST l        SUU U4S jjm [        T R                  XT R                  SLU	U(       a  SOSS9T l        T R                  R                  [        5       5        T R                  R                  T R                  T R                  T R                  UXgUT R                  XUUS	9  U(       d  T R                  T5      nT R!                  T R"                  5        U(       aW  [$        R&                  " 5       m[$        R(                  " T R*                  UT4S
9mTR-                  S5        TR/                  5         U U4S jnUU 4S jnUR1                  T R                  R                  UU5        g! [2        [4        [6        4 aX  nT R!                  T R8                  U5        [;        U[6        5      (       a  e T" 5         [;        U[4        5      (       + s SnA$ SnAff = f)a  
run event loop for WebSocket framework.
This loop is infinite loop and is alive during websocket is available.
sockopt: values for socket.setsockopt.
    sockopt must be tuple
    and each element is argument of sock.setsockopt.
sslopt: ssl socket optional dict.
ping_interval: automatically send "ping" command
    every specified period(second)
    if set to 0, not send automatically.
ping_timeout: timeout(second) if the pong message is not received.
http_proxy_host: http proxy host name.
http_proxy_port: http proxy port. If not set, set to 80.
http_no_proxy: host names, which doesn't use proxy.
skip_utf8_validation: skip utf8 validation.
host: update host header.
origin: update origin header.
dispatcher: customize reading data from socket.
suppress_origin: suppress outputting origin header.

Returns
-------
False if caught KeyboardInterrupt
True if other exception was raised during a loop
Nr   z#Ensure ping_interval > ping_timeoutzsocket is already openedTc                 r  > T(       a5  TR                  5       (       a   TR                  5         TR                  5         STl        TR                  (       a  TR                  R                  5         TR                  U (       a  U R                  OS5      nTR                  " TR                  /UQ76   STl        g)z
Tears down the connection.
If close_frame is set, we will invoke the on_close handler with the
statusCode and reason from there.
FN)
isAlivesetjoinrC   r   rP   _get_close_argsrL   	_callbackr?   )close_frame
close_argsr[   r   threads     r   teardown*WebSocketApp.run_forever.<locals>.teardown   s|     &..**		 %Dyy		!--$/  T;JNN4==6:6DIr   F)sockoptssloptfire_cont_frameskip_utf8_validationenable_multithread)r9   r:   http_proxy_hosthttp_proxy_porthttp_no_proxyhttp_proxy_authrG   hostoriginsuppress_origin
proxy_type)targetargsc                     > TR                   (       d  T" 5       $ TR                  R                  S5      u  pU [        R                  :X  a  T" U5      $ U [        R
                  :X  a'  TR                  TR                  UR                  5        gU [        R                  :X  aA  [        R                  " 5       Tl        TR                  TR                  UR                  5        gU [        R                  :X  a  TR                  (       an  TR                  TR                  UR                  UR                   UR"                  5        TR                  TR                  UR                  UR"                  5        gUR                  n[$        R&                  (       a%  U [        R(                  :X  a  UR+                  S5      nTR                  TR                  X!R                   S5        TR                  TR,                  U5        g)NTutf-8)rC   r   recv_data_framer   OPCODE_CLOSEOPCODE_PINGre   r@   rL   OPCODE_PONGrU   rF   rA   OPCODE_CONTrB   r=   rM   finsixPY3OPCODE_TEXTdecoder<   )op_codeframerL   r   ri   s      r   r$   &WebSocketApp.run_forever.<locals>.read  sa   ((#:%!%!:!:4!@d///#E?* 0 00NN4<<<    0 00(,		D%NN4<<<   0 00T5I5INN4<<#(<<<NN4#7#7#(::uyy:  !::Dww7d.>.>#>#{{73NN4<<||TJNN4??D9r   c                  0  > T(       a  [         R                   " 5       TR                  -
  T:  n TR                  TR                  -
  S:  nTR                  TR                  -
  T:  nTR                  (       a   U (       a  U(       d  U(       a  [        S5      eg)Nr   zping/pong timed outT)rU   rE   rF   WebSocketTimeoutException)has_timeout_expired$has_pong_not_arrived_after_last_pinghas_pong_arrived_too_later   r   s      r   check'WebSocketApp.run_forever.<locals>.check%  s}     *.))+8I8I*IL*X';?;L;LtO`O`;`cd;d8040A0ADDUDU0UXd0d-)) 3!EIb78MNNr   r   )WebSocketExceptionr   rC   rE   rF   r   rD   rB   
settimeoutr   connectr8   r9   r:   rG   create_dispatcherre   r;   	threadingEventThreadr]   	setDaemonstartr$   rW   KeyboardInterrupt
SystemExitr>   
isinstance)r   rk   rl   ping_intervalr   rp   rq   rr   rs   rn   rt   ru   
dispatcherrv   rw   r$   r   r#   r[   ri   rh   s   `   `             @@@r   run_foreverWebSocketApp.run_forever   s   B #(9LMm|.K$%JKKGF99$%?@@ 	 	"G	8!!!7 $ 4 4D @%9+84e	EDI
 II  !2!45IIT[[ / / /d>O>O/%  ' !33LA
NN4<<(!)"))??-1GI  &4
 OODIINND%8,j9 	8NN4==!,!Z((J!!%6777	8s     E$G% %I:AIIIc                     U=(       d    SnU R                   R                  5       (       a  [        X5      $ [        X5      $ )N
   )r   is_sslr,   r   )r   r   timeouts      r   r   WebSocketApp.create_dispatcher:  s5    $"99 //$((r   c                 "   [         R                  S:  aK  U R                  (       a7  [        [        R
                  " U R                  5      R                  5      S:w  a  / $ OJU R                  (       a7  [        [        R                  " U R                  5      R                  5      S:w  a  / $ U(       a\  [        U5      S:  aM  S[        R                  " USS 5      -  [        R                  " USS 5      -   nUSS R                  S5      nX#/$ SS/$ )	z}this functions extracts the code, reason from the close body
if they exists, and if the self.on_close except three arguments )   r   r         r   r   Nr{   )sysversion_infor?   leninspect
getargspecry   getfullargspecr   byte2intr   )r   rL   codereasons       r   rd   WebSocketApp._get_close_argsA  s     f$==C(:(:4==(I(N(N$OST$T	 %U ==C(>(>t}}(M(R(R$SWX$X	CINd1Qi003<<Qq	3JJD!"X__W-F>!d|r   c                 |   U(       a,   [         R                  " U5      (       a  U" U6   g U" U /UQ76   g g ! [         a|  n[        R                  " SR                  X5      5        [        R                  " 5       (       a3  [        R                  " 5       u    pE[        R                  " U5         S nAg  S nAg S nAff = f)Nzerror from callback {}: {})r   ismethodrW   r   errorrY   isEnabledForDebugr   exc_info	tracebackprint_tb)r   callbackry   r#   _tbs         r   re   WebSocketApp._callbackS  s    
+##H--dOT)D)   +;BB8OP--//"||~HAq&&r** 0+s    5 	5 
B;A-B66B;)r:   rD   r9   rC   rE   rF   r?   rB   r=   r>   r<   r;   r@   rA   r   rG   r8   )NNNNNNNNTNNNN)NNr   NNNNNFNNNFN)r&   r'   r(   r)   __doc__r   r   r   rJ   rP   r]   r   r   rd   re   r*   r   r   r   r	   r	   N   sk    
 $(9=6:!%>B";)z !% 0 0 
1 0426:>8<).7;8<H8T)$+r   )r   r   r   r   r   rU   r   r   _abnfr   _corer   r   _exceptions r   __all__r   r,   objectr	   r   r   r   <module>r      s^   ,   
    
  /   
  *Q+6 Q+r   