3
d                 @   sN   d Z ddlZddlZddlmZ dddZG dd dZdd	d
Zdd ZdS )z8
Authentication utilities to accompany `twitterclient`.
    N)TwythonFc             C   s   t  j| ||dS )z1
    Convenience function for authentication
    )
creds_filesubdirverbose)Authenticate
load_creds)r   r   r    r   1/tmp/pip-build-v9q4h5k9/nltk/nltk/twitter/util.pycredsfromfile   s    r
   c               @   s,   e Zd ZdZdd Zd
ddZddd	ZdS )r   z2
    Methods for authenticating with Twitter.
    c             C   sP   d| _ d | _i | _ytjd | _| j| _W n  tk
rJ   d | _d | _Y nX d S )Nzcredentials.txtZTWITTER)r   creds_fullpathoauthosenvironZtwitter_dircreds_subdirKeyError)selfr   r   r	   __init__!   s    zAuthenticate.__init__NFc       	      C   s   |dk	r|| _ |dkr.| jdkr4d	}t|n|| _tjjtjj| j| j | _tjj| jsnt	d| j t
| jR}|rtd| j  x6|D ].}d|kr|jdd\}}|j | j|j < qW W dQ R X | j|d | jS )
a  
        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'`
        Nz+Supply a value to the 'subdir' parameter orz& set the TWITTER environment variable.zCannot find file zReading credentials file =   )r   zQSupply a value to the 'subdir' parameter or set the TWITTER environment variable.)r   r   
ValueErrorr   pathnormpathjoinr   isfileOSErroropenprintsplitstripr   _validate_creds_file)	r   r   r   r   msginfilelinenamevaluer   r   r	   r   -   s*    
 

 zAuthenticate.load_credsc                s   d}ddddg}d}dddg}t  fdd|D r:d	}nt  fd
d|D rTd	}|pZ|sd j d}|tj j7 }t|n|rtd j d dS )z%Check validity of a credentials file.Fapp_key
app_secretZoauth_tokenZoauth_token_secretaccess_tokenc             3   s   | ]}| j kV  qd S )N)r   ).0k)r   r   r	   	<genexpr>i   s    z4Authenticate._validate_creds_file.<locals>.<genexpr>Tc             3   s   | ]}| j kV  qd S )N)r   )r(   r)   )r   r   r	   r*   k   s    z Missing or incorrect entries in 
zCredentials file "z" looks goodN)allr   pprintpformatr   r   r   )r   r   Zoauth1Zoauth1_keysoauth2Zoauth2_keysr    r   )r   r	   r   c   s    

z!Authenticate._validate_creds_file)NNF)F)__name__
__module____qualname____doc__r   r   r   r   r   r   r	   r      s   
6r   c       	      C   s   | dkr"t jjt}t jj|d} t| d}|d }|d }t||dd}|j }d| d	}t| d
}t	||d W dQ R X dS )zc
    For OAuth 2, retrieve an access token for an app and append it to a
    credentials file.
    Nzcredentials2.txt)r   r%   r&      )Zoauth_versionzaccess_token=r+   a)file)
r   r   dirname__file__r   r
   r   Zobtain_access_tokenr   r   )	r   r   r/   r%   r&   Ztwitterr'   tokr!   r   r   r	   add_access_tokenv   s    
r:   c             C   s*   t jj| r| S t jjt jjd| S dS )z
    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
    ~N)r   r   isabs
expanduserr   )pthr   r   r	   
guess_path   s    r?   )NNF)N)	r3   r   r-   Ztwythonr   r
   r   r:   r?   r   r   r   r	   <module>   s   
	Z
