B
    >?[                 @   s\   d Z ddlmZ ddlZddlZddlmZ dddZG dd	 d	eZ	dd
dZ
dd ZdS )z@
Authentication utilities to accompany :module:`twitterclient`.
    )print_functionN)TwythonFc             C   s   t  j| ||dS )z1
    Convenience function for authentication
    )
creds_filesubdirverbose)Authenticate
load_creds)r   r   r    r	   0lib/python3.7/site-packages/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tj| j| j | _tj| jsnt	d
| jt| jR}|rtd
| j x6|D ].}d|kr|dd\}}| | j| < qW W dQ R X | j|d | jS )ag  
        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'`
        NzQSupply a value to the 'subdir' parameter or set the TWITTER environment variable.zCannot find file {}zReading credentials file {}=   )r   )r   r   
ValueErrorr   pathnormpathjoinr   isfileOSErrorformat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	}|s|sd j}|t j7 }t|n|rtd j 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>n   s    z4Authenticate._validate_creds_file.<locals>.<genexpr>Tc             3   s   | ]}| j kV  qd S )N)r   )r*   r+   )r   r	   r
   r,   p   s    z#Missing or incorrect entries in {}
z Credentials file "{}" looks goodN)allr   r   pprintZpformatr   r   r   )r   r   Zoauth1Zoauth1_keysoauth2Zoauth2_keysr"   r	   )r   r
   r!   h   s    

z!Authenticate._validate_creds_file)NNF)F)__name__
__module____qualname____doc__r   r   r!   r	   r	   r	   r
   r      s   
9r   c       	   	   C   s   | dkr"t jt}t j|d} t| d}|d }|d }t||d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={}
a)file)r   r   dirname__file__r   r   r   Zobtain_access_tokenr   r   r   )	r   r   r/   r'   r(   Ztwitterr)   tokr#   r	   r	   r
   add_access_token{   s    

r:   c             C   s*   t j| r| S t jt 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   )Zpthr	   r	   r
   
guess_path   s    r>   )NNF)N)r3   Z
__future__r   r   r.   Ztwythonr   r   objectr   r:   r>   r	   r	   r	   r
   <module>   s   
	]
