ó
Cª\c           @  sÎ  d  d l  m Z d  d l Z d  d l 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 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 Z e j d e j g ƒ j ƒ  Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l j Z d e j f d „  ƒ  YZ d e j  f d „  ƒ  YZ! d Z" d Z# d Z$ d d d „  ƒ  YZ% d d d „  ƒ  YZ& d „  Z' d „  Z( d „  Z) d S(   iÿÿÿÿ(   t   print_functionN(   t	   discovery(   t   client(   t   tools(   t   Storage(   t   BaseHTTPServer(   t   http_client(   t   input(   t   urllib(   t   _helperst   parentst   ClientRedirectServerc           B  s   e  Z i  Z RS(    (   t   __name__t
   __module__t   query_params(    (    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyR   '   s   t   ClientRedirectHandlerc           B  s   e  Z d  „  Z RS(   c         C  s°   |  j  t j ƒ |  j d d ƒ |  j ƒ  t j j |  j ƒ } t	 j
 | j ƒ } | |  j _ |  j j d ƒ d | k rŒ |  j j d ƒ n |  j j d ƒ |  j j d ƒ d S(	   s³   Handle a GET request.

        Parses the query parameters and prints a message
        if the flow has completed. Note that we can't detect
        if an error occurred.
        s   Content-types	   text/htmls¾  
<!Doctype HTML>
<html>
<head><title>Maya Type Tool Authentication</title></head>
<style>
html, body{
    background-color: rgba(52,52,52,255)
}
#wrapper{
    text-align:center;
    padding: 0;
    margin:0;
}
#message{
    padding: 10px 20px;
    background-color: rgba(75,75,75,255);
    color:rgba(234,234,234,255);
    margin: 20px auto;
    display:inline-block;
    border-radius:4px;
}
</style>
<body>
<div id="wrapper">
<div id="message">t   codese   <p style="text-align:center;">The authentication was successful.<br>Feel free to close this page.</p>s·   <p style="text-align:center;">The authentication was rejected.<br>Maya Type Tool has no permission to access the spreadsheet.<br>Please try again.<br>Feel free to close this page.</p>s   </div>
</div>
</body>
</html>N(   t   send_responseR   t   OKt   send_headert   end_headersR   t   parset   urlparset   pathR	   t   parse_unique_urlencodedt   queryt   serverR   t   wfilet   write(   t   selft   partsR   (    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyt   do_GET+   s    
		
		(   R   R   R   (    (    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyR   *   s   s5   https://www.googleapis.com/auth/spreadsheets.readonlys1   sheets.googleapis.com-python-maya-type-tool2.jsons   Maya Type Toolt   AuthenticationServerc           B  s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C  s:   | |  _  t |  _ t |  _ d  |  _ d |  _ d  |  _ d  S(   Ni    (   t   readert   flagst   Falset	   isRunningt   Nonet   httpdt   port_numbert   flow(   R   R!   (    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyt   __init__g   s    					c         C  sã   |  j  d  k r$ t j j ƒ  |  _  n  t } xa t  j D]V } | |  _ y t t  j	 | f t
 ƒ |  _ Wn t j k
 r‚ t d ƒ q4 Xt } Pq4 W| rÕ t d ƒ t |  _ d |  j _ t j d |  j ƒ } | j ƒ  n
 t d ƒ d  S(   Ns%   Error starting Authentication Server.s   Starting serveri   t   targets   Could not start server(   R"   R%   R   t	   argparsert
   parse_argsR#   t   auth_host_portR'   R   t   auth_host_nameR   R&   t   sockett   errort   printt   TrueR$   t   timeoutt	   threadingt   Threadt   servet   start(   R   t   successt   portt   t1(    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyt   startServero   s&    	
	c         C  s+   x |  j  r |  j ƒ  q W|  j j ƒ  d  S(   N(   R$   t   handleRequestR&   t   server_close(   R   (    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyR6   ‡   s    c         C  s   t  |  _ t d ƒ d  S(   Ns   Stopped Server(   R#   R$   R1   (   R   (    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyt
   stopServerŒ   s    	c         C  sï   |  j  j ƒ  d |  j  j k r6 t d ƒ |  j ƒ  nµ d |  j  j k rë |  j  j d } d  } y |  j j | ƒ } Wn) t j	 k
 rŸ } t d j
 | ƒ ƒ n X| d  k rº t d ƒ d  St | ƒ t d ƒ |  j ƒ  |  j j | ƒ n  d  S(   NR0   s$   Authentication request was rejected.R   s   Authentication has failed: {0}s   Authentication has failed.s   Authentication successful.(   R&   t   handle_requestR   R1   R>   R%   R(   t   step2_exchangeR   t   FlowExchangeErrort   formatt   storeCredentialsR!   t   getSheetData(   R   R   t
   credentialt   e(    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyR<      s$    




(   R   R   R)   R;   R6   R>   R<   (    (    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyR    f   s
   				t   SpreadSheetReaderc           B  s8   e  Z d d  „ Z d „  Z d „  Z d „  Z d „  Z RS(   c         C  s@   t  |  ƒ |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   R    R   t   sheetUrlt	   readyFunct   nodet   rangeFramest	   rangeText(   R   RJ   t   urlRK   RL   t   func(    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyR)   ¥   s    				c         C  s   |  j  j ƒ  d  S(   N(   R   R>   (   R   (    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyt   __del__­   s    c         C  s±   |  j  j sR t j d d d d d t d d d d	 d
 d ƒ |  j  _ |  j  j ƒ  n  d j d t j	 d |  j  j
 ƒ } | |  j  j _ |  j  j j ƒ  } t j | d d d t ƒd  S(   Nt	   client_idsH   930050415317-v8a1kmdorb4s82il0hbmgvorp54qf959.apps.googleusercontent.comt   client_secrett   sBEZ74vWNB_cTAhzbE1O1qFut   scopet   auth_uris)   https://accounts.google.com/o/oauth2/autht	   token_uris*   https://accounts.google.com/o/oauth2/tokent   redirect_uris   urn:ietf:wg:oauth:2.0:oobs   http://{host}:{port}/t   hostR9   t   newi   t	   autoraise(   R   R$   R   t   OAuth2WebServerFlowt   SCOPESR(   R;   RB   R"   R.   R'   RV   t   step1_get_authorize_urlt
   webbrowsert   openR2   (   R   t   oauth_callbackt   authorize_url(    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyt   renewCredentials°   s    	c         C  s¡  | j  t j ƒ  ƒ } t j d d d | ƒ} t j d |  j ƒ } | d  k	 r` | j	 d ƒ } n  t j d |  j ƒ } | d  k	 r“ | j	 d ƒ } n  d  } y" | j
 ƒ  j d | ƒ j ƒ  } Wn/ t d ƒ t ƒ  } | j ƒ  |  j ƒ  d  SX| d } d  } | d  k	 rOxC | D]8 }	 |	 d	 d
 }
 |	 d	 d } |
 | k r| } PqqWn  | d  k rp| d d	 d } n  g  } d  } d  } |  j r| j
 ƒ  j ƒ  j d | d | d |  j ƒ j ƒ  } | j d g  ƒ } t | ƒ t | d ƒ k rg  | D] } | d ^ qõ} q| d } n  |  j r´| j
 ƒ  j ƒ  j d | d | d |  j ƒ j ƒ  } | j d g  ƒ } t | ƒ t | d ƒ k r§g  | D] } | d ^ qŽ} q´| d } n  | sÈt d ƒ d  Sx’ t | ƒ D]„ \ } } d j g  | D] } d t | ƒ ^ qîƒ j ƒ  } d } | r>| t | ƒ k  r>| | } n  | j i | d 6| d 6ƒ qÕWt j | ƒ } t j |  j d | d d ƒt j ƒ  t d ƒ d  S(   Nt   sheetst   v4t   https	   /d/(\w+)/i   s   #gid=([0-9]+)t   spreadsheetIds$   No permission to access spreadsheet.t
   propertiest   sheetIdt   titlei    t   ranget   !t   valuess   Invalid data. Aborting.t    s   %02X t   hext   frames   .animatedTypet   typet   strings   Done Importing SpreadSheet(    t	   authorizet   httplib2t   HttpR   t   buildt   ret   searchRH   R%   t   groupt   spreadsheetst   gett   executeR1   t   getCredentialsStoret   deleteRa   RK   Rk   t   lenRL   t	   enumeratet   joint   ordt   stript   appendt   jsont   dumpst   cmdst   setAttrRJ   t   refreshEditorTemplates(   R   t   credentialsRd   t   serviceRe   Rg   Rb   t   storet   selectedSheett   sheett   sIdRh   t   datat
   framesDatat   textDatat   resultRk   t   xt   it   vRn   t   jstring(    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyRD   Á   st    "
	


	  	  
2
c         C  sC   t  ƒ  } | j ƒ  } | s% | j r2 |  j ƒ  n |  j | ƒ d  S(   N(   R{   Ry   t   invalidRa   RD   (   R   RŠ   Rˆ   (    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyt   processSheet	  s
    	N(   R   R   R%   R)   RO   Ra   RD   R—   (    (    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyRG   ¤   s
   			Hc          C  sn   t  j j d ƒ }  t  j j |  d ƒ } t  j j | ƒ sI t  j | ƒ n  t  j j | t ƒ } t | ƒ } | S(   Nt   ~s   .credentials(   t   osR   t
   expanduserR   t   existst   makedirst   CREDENTIALS_FILENAMER   (   t   home_dirt   credential_dirt   credential_pathRŠ   (    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyR{     s    c         C  s'   t  ƒ  } | j |  ƒ |  j | ƒ d  S(   N(   R{   t   putt	   set_store(   RE   RŠ   (    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyRC     s    	c         C  s8   t  |  | | | ƒ } t j d | j ƒ } | j ƒ  d  S(   NR*   (   RG   R4   R5   R—   R7   (   t   nameRM   RK   RL   R!   R:   (    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyt	   loadSheet  s    (    (    (*   t
   __future__R    Rr   R™   t	   apiclientR   t   oauth2clientR   R   t   oauth2client.fileR   R/   t   syst	   six.movesR   R   R   R   R	   t   argparset   ArgumentParserR+   R,   R"   Ru   R4   R]   Rƒ   t	   maya.cmdsR…   t
   HTTPServerR   t   BaseHTTPRequestHandlerR   R[   R   t   APPLICATION_NAMER    RG   R{   RC   R¤   (    (    (    sH   S:/Maya_2019_DI/build/Release/runTime/plug-ins/MASH/scripts/sheetsapi.pyt   <module>	   s>   8>m			