
    Qd                     V    d Z ddlZddlZddlmZ d
dZ G d d          ZddZd	 ZdS )z8
Authentication utilities to accompany `twitterclient`.
    N)TwythonFc                 J    t                                          | ||          S )z1
    Convenience function for authentication
    
creds_filesubdirverbose)Authenticate
load_credsr   s      1lib/python3.11/site-packages/nltk/twitter/util.pycredsfromfiler      s,     >>$$fg %       c                   (    e Zd ZdZd ZddZddZdS )	r	   z2
    Methods for authenticating with Twitter.
    c                     d| _         d | _        i | _        	 t          j        d         | _        | j        | _        d S # t          $ r d | _        d | _        Y d S w xY w)Nzcredentials.txtTWITTER)r   creds_fullpathoauthosenvirontwitter_dircreds_subdirKeyError)selfs    r   __init__zAuthenticate.__init__!   sq    +"
	%!z)4D $ 0D 	% 	% 	%#D $D	%s   #< AANFc                    ||| _         || j        d}t          |          n|| _        t          j                            t          j                            | j        | j                             | _        t          j                            | j                  st          d| j                   t          | j                  5 }|rt          d| j                    |D ]M}d|v rG|                    dd          \  }}|                                | j        |                                <   N	 ddd           n# 1 swxY w Y   |                     |           | 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'`
        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openprintsplitstripr   _validate_creds_file)	r   r   r   r   msginfilelinenamevalues	            r   r
   zAuthenticate.load_creds-   s   * !(DO> (?  !oo% ) !'D g..GLL*DO<<
 
 w~~d122 	ECd.ACCDDD$%&& 	=& IG$2EGGHHH = =$;;"&**S!"4"4KD%/4{{}}DJtzz||,=		= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	!!'!222zs   A*D>>EEc                 B    d}g d}d}g d}t           fd|D                       rd}nt           fd|D                       rd}|s8|s6d j         d}|t          j         j                  z  }t          |          |rt          d	 j         d
           dS dS )z%Check validity of a credentials file.F)app_key
app_secretoauth_tokenoauth_token_secret)r.   r/   access_tokenc              3   *   K   | ]}|j         v V  d S Nr   .0kr   s     r   	<genexpr>z4Authenticate._validate_creds_file.<locals>.<genexpr>i   s)      441qDJ444444r   Tc              3   *   K   | ]}|j         v V  d S r4   r5   r6   s     r   r9   z4Authenticate._validate_creds_file.<locals>.<genexpr>k   s)      66Qdj666666r   z Missing or incorrect entries in 
zCredentials file "z" looks goodN)allr   pprintpformatr   r   r$   )r   r   oauth1oauth1_keysoauth2oauth2_keysr(   s   `      r   r'   z!Authenticate._validate_creds_filec   s    TTT???444444444 	FF6666+66666 	F 	F& 	FHT_HHHC6>$*---CS//! 	FDtDDDEEEEE	F 	Fr   NNF)F)__name__
__module____qualname____doc__r   r
   r'    r   r   r	   r	      s\         
% 
% 
%4 4 4 4lF F F F F Fr   r	   c                    | Dt           j                            t                    }t           j                            |d          } t          |           }|d         }|d         }t          ||d          }|                                }d| d	}t          | d
          5 }t          ||           ddd           dS # 1 swxY w Y   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/      )oauth_versionzaccess_token=r;   a)file)
r   r   dirname__file__r    r   r   obtain_access_tokenr#   r$   )	r   r   rA   r.   r/   twitterr2   tokr)   s	            r   add_access_tokenrS   v   s   
 wx((W\\$(:;;
j111FYG%Jgz;;;G..00L
*,
*
*
*C	j#		  &c                                   s   #CC	Cc                     t           j                            |           r| S t           j                            t           j                            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
    ~)r   r   isabs
expanduserr    )pths    r   
guess_pathrY      sE     
w}}S :
w!!"',,sC"8"8999r   rC   r4   )	rG   r   r=   twythonr   r   r	   rS   rY   rH   r   r   <module>r[      s     
			          WF WF WF WF WF WF WF WFt       &
: 
: 
: 
: 
:r   