ó
ù`]c           @  s   d  Z  d d l m Z d d l Z d d l Z d d l m Z d d e d „ Z	 d e
 f d „  ƒ  YZ d d „ Z d	 „  Z d S(
   s@   
Authentication utilities to accompany :module:`twitterclient`.
iÿÿÿÿ(   t   print_functionN(   t   Twythonc         C  s   t  ƒ  j d |  d | d | ƒ S(   s1   
    Convenience function for authentication
    t
   creds_filet   subdirt   verbose(   t   Authenticatet
   load_creds(   R   R   R   (    (    s0   lib/python2.7/site-packages/nltk/twitter/util.pyt   credsfromfile   s    R   c           B  s5   e  Z d  Z d „  Z d d e d „ Z e d „ Z RS(   s2   
    Methods for authenticating with Twitter.
    c         C  se   d |  _  d  |  _ i  |  _ y  t j d |  _ |  j |  _ Wn# t k
 r` d  |  _ d  |  _ n Xd  S(   Ns   credentials.txtt   TWITTER(	   R   t   Nonet   creds_fullpatht   oautht   ost   environt   twitter_dirt   creds_subdirt   KeyError(   t   self(    (    s0   lib/python2.7/site-packages/nltk/twitter/util.pyt   __init__#   s    				c   	      C  sQ  | d k	 r | |  _ n  | d k rO |  j d k rX d d } t | ƒ ‚ qX n	 | |  _ t j j t j j |  j |  j ƒ ƒ |  _ t j j	 |  j ƒ s² t
 d j |  j ƒ ƒ ‚ n  t |  j ƒ w } | rã t d j |  j ƒ ƒ n  xN | D]F } d | k rê | j d d ƒ \ } } | j ƒ  |  j | j ƒ  <qê qê WWd QX|  j d | ƒ |  j S(	   sg  
        Read OAuth credentials from a text file.

        ::
           File format for OAuth 1
           =======================
           app_key=YOUR_APP_KEY
           app_secret=YOUR_APP_SECRET
           oauth_token=OAUTH_TOKEN
           oauth_token_secret=OAUTH_TOKEN_SECRET


        ::
           File format for OAuth 2
           =======================

           app_key=YOUR_APP_KEY
           app_secret=YOUR_APP_SECRET
           access_token=ACCESS_TOKEN

        :param str file_name: File containing credentials. ``None`` (default) reads        data from `TWITTER/'credentials.txt'`
        s+   Supply a value to the 'subdir' parameter ors&    set the TWITTER environment variable.s   Cannot find file {}s   Reading credentials file {}t   =i   NR   (   R	   R   R   t
   ValueErrorR   t   patht   normpatht   joinR
   t   isfilet   OSErrort   formatt   opent   printt   splitt   stripR   t   _validate_creds_file(	   R   R   R   R   t   msgt   infilet   linet   namet   value(    (    s0   lib/python2.7/site-packages/nltk/twitter/util.pyR   /   s*    		!&c           sÝ   t  } d d d d g } t  } d d d g } t ‡  f d †  | Dƒ ƒ rR t } n% t ‡  f d †  | Dƒ ƒ rw t } n  | p€ | sº d j ˆ  j ƒ } | t j ˆ  j ƒ 7} t | ƒ ‚ n | rÙ t	 d	 j ˆ  j ƒ ƒ n  d
 S(   s%   Check validity of a credentials file.t   app_keyt
   app_secrett   oauth_tokent   oauth_token_secrett   access_tokenc         3  s   |  ] } | ˆ  j  k Vq d  S(   N(   R   (   t   .0t   k(   R   (    s0   lib/python2.7/site-packages/nltk/twitter/util.pys	   <genexpr>n   s    c         3  s   |  ] } | ˆ  j  k Vq d  S(   N(   R   (   R*   R+   (   R   (    s0   lib/python2.7/site-packages/nltk/twitter/util.pys	   <genexpr>p   s    s#   Missing or incorrect entries in {}
s    Credentials file "{}" looks goodN(
   t   Falset   allt   TrueR   R   t   pprintt   pformatR   R   R   (   R   R   t   oauth1t   oauth1_keyst   oauth2t   oauth2_keysR    (    (   R   s0   lib/python2.7/site-packages/nltk/twitter/util.pyR   h   s    		N(   t   __name__t
   __module__t   __doc__R   R	   R,   R   R   (    (    (    s0   lib/python2.7/site-packages/nltk/twitter/util.pyR      s   	9c   	      C  sµ   |  d
 k r6 t j j t ƒ } t j j | d ƒ }  n  t d |  ƒ } | d } | d } t | | d d ƒ} | j ƒ  } d j	 | ƒ } t
 |  d ƒ  } t | d	 | ƒWd
 QXd
 S(   sc   
    For OAuth 2, retrieve an access token for an app and append it to a
    credentials file.
    s   credentials2.txtR   R%   R&   t   oauth_versioni   s   access_token={}
t   at   fileN(   R	   R   R   t   dirnamet   __file__R   R   R   t   obtain_access_tokenR   R   R   (	   R   R   R3   R%   R&   t   twitterR)   t   tokR!   (    (    s0   lib/python2.7/site-packages/nltk/twitter/util.pyt   add_access_token{   s    

c         C  s9   t  j j |  ƒ r |  St  j j t  j j d |  ƒ ƒ Sd S(   sÃ   
    If the path is not absolute, guess that it is a subdirectory of the
    user's home directory.

    :param str pth: The pathname of the directory where files of tweets should be written
    t   ~N(   R   R   t   isabst
   expanduserR   (   t   pth(    (    s0   lib/python2.7/site-packages/nltk/twitter/util.pyt
   guess_pathŽ   s    (   R7   t
   __future__R    R   R/   t   twythonR   R	   R,   R   t   objectR   R@   RE   (    (    (    s0   lib/python2.7/site-packages/nltk/twitter/util.pyt   <module>   s   	]