
    G@d&                     >   d Z 	 ddlmZmZmZmZ ddlmZ ddl	m	Z	 ddl
mZ d%dZn%#  ddlmZmZmZmZ ddlmZ ddlm	Z	 ddlmZ Y nxY wddlZddlZddlZddlZddlZddlZddlZddlZddlZdd	lmZmZmZ d
ZdZ ed d d d d           ZdZ dZ!d Z"d Z#d Z$ G d de%          Z& G d de%          Z' G d de%          Z( G d de          Z) G d de*          Z+ G d  d!e+          Z, G d" d#e+          Z-e.d$k    rddl/Z/ e/j0                     dS dS )&a  
GitHub API Python SDK. (Python >= 2.6)

Apache License

Michael Liao (askxuefeng@gmail.com) https://github.com/michaelliao/githubpy

Usage:

>>> gh = GitHub(username='githubpy', password='test-githubpy-1234')
>>> L = gh.users('githubpy').followers.get()
>>> L[0].id
470058
>>> L[0].login == u'michaelliao'
True
>>> x_ratelimit_remaining = gh.x_ratelimit_remaining
>>> x_ratelimit_limit = gh.x_ratelimit_limit
>>> x_ratelimit_reset = gh.x_ratelimit_reset
>>> L = gh.users('githubpy').following.get()
>>> L[0].url == u'https://api.github.com/users/michaelliao'
True
>>> L = gh.repos('githubpy')('testgithubpy').issues.get(state='closed', sort='created')
>>> L[0].title == u'sample issue for test'
True
>>> L[0].number
1
>>> I = gh.repos('githubpy')('testgithubpy').issues(1).get()
>>> I.url == u'https://api.github.com/repos/githubpy/testgithubpy/issues/1'
True
>>> gh = GitHub(username='githubpy', password='test-githubpy-1234')
>>> r = gh.repos('githubpy')('testgithubpy').issues.post(title='test create issue', body='just a test')
>>> r.title == u'test create issue'
True
>>> r.state == u'open'
True
>>> gh.repos.thisisabadurl.get()
Traceback (most recent call last):
    ...
ApiNotFoundError: https://api.github.com/repos/thisisabadurl
>>> gh.users('github-not-exist-user').followers.get()
Traceback (most recent call last):
    ...
ApiNotFoundError: https://api.github.com/users/github-not-exist-user/followers
    )build_openerHTTPSHandlerRequest	HTTPError)quote)StringIO)IterableNc                      t          |           S Nstr)stringencodings     <lib/python3.11/site-packages/spyder/utils/external/github.pybytesr   7   s    6{{    )r   r   r   r   )datetime	timedeltatzinfo<   zhttps://api.github.comc                      dS )NGET r   r   r   <lambda>r   G       E r   c                      dS )NPUTr   r   r   r   r   r   H   r   r   c                      dS )NPOSTr   r   r   r   r   r   I   s    V r   c                      dS )NPATCHr   r   r   r   r   r   J   s    g r   c                      dS )NDELETEr   r   r   r   r   r   K   s    x r   )r   r   r   r!   r#   z&user,public_repo,repo,repo:status,gistc                 :   g }|                                  D ]p\  }}	 t          |t                    r|                    d          nt	          |          }n	#  |}Y nxY w|                    |dt          |                     qd                    |          S )z
    Encode parameters.
    utf-8=&)items
isinstanceunicodeencoder   appendurlquotejoin)kwargskvqvs        r   _encode_paramsr4   P   s     D

 1 11	&0G&<&<H'"""#a&&BB	BBBqqq(2,,,/000088D>>s   9AAc                 4    d }t          j        | |          S )z$
    Encode object as json str.
    c                     t          | t                    r| S t                      }t          |           D ]*}|                    d          st	          | |          ||<   +|S )N_)r)   dictdir
startswithgetattr)objdr1   s      r   	_dump_objz_encode_json.<locals>._dump_objb   sb    c4   	JFFS 	' 	'A<<$$ 'sA!r   )default)jsondumps)r<   r>   s     r   _encode_jsonrB   ^   s(       :c9----r   c                 4    d }t          j        | |          S )Nc                 z    t                      }|                                 D ]\  }}||t          |          <   |S r   )
JsonObjectr(   r   )pairsor1   r2   s       r   	_obj_hookz_parse_json.<locals>._obj_hookm   s<    LLKKMM 	 	DAqAc!ffIIr   )object_hook)r@   loads)jsonstrrH   s     r   _parse_jsonrL   l   s(      
 :g95555r   c                   $    e Zd Zd Zd Zd ZeZdS )_Executablec                 0    || _         || _        || _        d S r   )_gh_method_path)selfrP   rQ   rR   s       r   __init__z_Executable.__init__v   s    


r   c                 >     | j         j        | j        | j        fi |S r   )rP   _httprQ   rR   )rS   r/   s     r   __call__z_Executable.__call__{   s$    tx~dlDJ=="===r   c                 (    d| j         d| j        dS )Nz_Executable ( ))rQ   rR   rS   s    r   __str__z_Executable.__str__~   s     (,djjjAAr   N)__name__
__module____qualname__rT   rW   r\   __repr__r   r   r   rN   rN   t   sE          
> > >B B B HHHr   rN   c                   *    e Zd Zd Zd Zd Zd ZeZdS )	_Callablec                 "    || _         || _        d S r   )rP   _name)rS   rP   rd   s      r   rT   z_Callable.__init__   s    


r   c                     t          |          dk    r| S | j        dd                    d |D                       }t          | j        |          S )Nr   /c                 ,    g | ]}t          |          S r   r   ).0args     r   
<listcomp>z&_Callable.__call__.<locals>.<listcomp>   s    /I/I/ISC/I/I/Ir   )lenrd   r.   rb   rP   )rS   r0   names      r   rW   z_Callable.__call__   sV    t99a<<K***chh/I/ID/I/I/I&J&J&JK4(((r   c                    |dk    rt          | j        d| j                  S |dk    rt          | j        d| j                  S |dk    rt          | j        d| j                  S |dk    rt          | j        d| j                  S |d	k    rt          | j        d
| j                  S | j        d|}t          | j        |          S )Ngetr   putr   postr   patchr!   deleter#   rf   )rN   rP   rd   rb   )rS   attrrl   s      r   __getattr__z_Callable.__getattr__   s    ;;tx
;;;;;tx
;;;<<tx<<<==tx$*===>>tx4:>>>***dd+4(((r   c                     d| j         z  S )Nz_Callable (%s))rd   r[   s    r   r\   z_Callable.__str__   s    $*,,r   N)r]   r^   r_   rT   rW   rt   r\   r`   r   r   r   rb   rb      sQ          ) ) )) ) )- - - HHHr   rb   c                   <    e Zd ZdZd	dZd
dZd
dZd Zd Zd Z	dS )GitHubz
    GitHub client.
    Nc                 $   d| _         d| _        d| _        d | _        |rI|rGt	          j        t          |d|d                    }|                    d          }d|z  | _        n|r
d|z  | _        || _        || _	        || _
        || _        d S )N:r%   asciizBasic %sztoken %s)x_ratelimit_remainingx_ratelimit_limitx_ratelimit_reset_authorizationbase64	b64encoder   decode
_client_id_client_secret_redirect_uri_scope)	rS   usernamepasswordaccess_token	client_idclient_secretredirect_uriscopeuserandpasss	            r   rT   zGitHub.__init__   s    &(""$"$" 	< 	< *5HHHhh1OQX+Y+YZZK%,,W55K",{":D 	<",|";D#+)r   c                     | j         st          d          t          | j                   }| j        r
| j        |d<   | j        r
| j        |d<   |r||d<   dt          |          z  S )z
        Generate authorize_url.

        >>> GitHub(client_id='3ebf94c5776d565bcf75').authorize_url()
        'https://github.com/login/oauth/authorize?client_id=3ebf94c5776d565bcf75'
        zNo client id.)r   r   r   statez+https://github.com/login/oauth/authorize?%s)r   ApiAuthErrorr8   r   r   r4   )rS   r   r/   s      r   authorize_urlzGitHub.authorize_url   s      	0///DO,,, 	4!%!3B~; 	&+BwK 	 BwK<~b?Q?QQQr   c                 N   t          | j        | j        |          }| j        r
| j        |d<   |r||d<   t	          t
                    }t          dt          |                    }t          d         |_	        |
                    dd           	 |                    |t          	          }t          |                                          }d
|v r!t          t!          |j                            t!          |j                  S # t&          $ r}t          d          d}~ww xY w)z~
        In callback url: http://host/callback?code=123&state=xyz

        use code and state to get an access token.
        )r   r   coder   r   z+https://github.com/login/oauth/access_tokendatar   Acceptapplication/jsontimeouterrorzHTTPError when get access tokenN)r8   r   r   r   r   r   r   r4   _METHOD_MAP
get_method
add_headeropenTIMEOUTrL   readr   r   r   r   r   )	rS   r   r   r/   openerrequestresponseres	            r   get_access_tokenzGitHub.get_access_token   s%    DO4;NUYZZZ 	4!%!3B~ 	 BwKl++Gn]_N`N`aaa(08%7888	B{{7G{<<HHMMOO,,A!||"3qw<<000q~&&& 	B 	B 	B@AAA	Bs   A5D 
D$DD$c                 (    t          | d|z            S )Nz/%s)rb   )rS   rs   s     r   rt   zGitHub.__getattr__   s    ut|,,,r   c                    d }d }|dk    r|r|dt          |          }|dv rt          t          |          d          }t          |}t	          t
                    }t          ||          }t          |         |_        | j	        r|
                    d| j	                   |dv r|
                    dd           	 |                    |t          	          }	|                     |	j                  }
|
r4t          |	                                                    d                    S d S # t$          $ r}|                     |j                  }
|
r5t          |                                                    d                    }n'|                                                    d          }t'          ||
          }t'          |j        |          }|j        dk    rt+          |||          t-          |||          d }~ww xY w)Nr   ?)r   r!   r   r%   r   AuthorizationzContent-Typez!application/x-www-form-urlencodedr   )methodurl)r   r@   i  )r4   r   rB   _URLr   r   r   r   r   r   r   r   r   _process_respheadersrL   r   r   r   rE   r   ApiNotFoundErrorApiError)rS   rQ   rR   r/   r   paramsr   r   r   r   is_jsonr   r@   reqresps                  r   rV   zGitHub._http   s   E>>b>$uunR&8&8&89E...b))733Dee$l++#D)))(1 	E0CDDD...~/RSSS	+{{7G{<<H(()9::G D"8==??#9#9'#B#BCCCD D 
	+ 
	+ 
	+((33G 0"16688??7#;#;<<vvxxw//G555C16555Dy#~~&sC6663T***
	+s   A+D/ /
H
9CHH
c                 H   d}|r|D ]}|                                 }|dk    rt          ||                   | _        7|dk    rt          ||                   | _        X|dk    rt          ||                   | _        y|dk    r||                             d          }|S )NFzx-ratelimit-remainingzx-ratelimit-limitzx-ratelimit-resetzcontent-typer   )lowerintr|   r}   r~   r:   )rS   r   r   r1   hs        r   r   zGitHub._process_resp  s     
	H 	H 	HGGII---14WQZD..+++-0__D**+++-0__D**&&%aj334FGGGr   )NNNNNNNr   )
r]   r^   r_   __doc__rT   r   r   rt   rV   r   r   r   r   rw   rw      s            "R R R R$B B B B0- - -+ + +@    r   rw   c                       e Zd ZdZd Zd ZdS )rE   zN
    general json object that can bind any fields but also act as a dict.
    c                 T    	 | |         S # t           $ r t          d|z            w xY w)Nz#'Dict' object has no attribute '%s')KeyErrorAttributeError)rS   keys     r   rt   zJsonObject.__getattr__  sE    	O9 	O 	O 	O !G#!MNNN	Os   
 'c                     || |<   d S r   r   )rS   rs   values      r   __setattr__zJsonObject.__setattr__  s    T


r   N)r]   r^   r_   r   rt   r   r   r   r   rE   rE     s?         O O O    r   rE   c                        e Zd Z fdZ xZS )r   c                 t    t          t          |                               |           || _        || _        d S r   )superr   rT   r   r   )rS   r   r   r   	__class__s       r   rT   zApiError.__init__$  s3    h&&s+++ r   r]   r^   r_   rT   __classcell__r   s   @r   r   r   "  s8        ! ! ! ! ! ! ! ! !r   r   c                        e Zd Z fdZ xZS )r   c                 \    t          t          |                               |d d            d S r   )r   r   rT   )rS   msgr   s     r   rT   zApiAuthError.__init__+  s+    lD!!**3d;;;;;r   r   r   s   @r   r   r   )  s8        < < < < < < < < <r   r   c                       e Zd ZdS )r   N)r]   r^   r_   r   r   r   r   r   .  s        Dr   r   __main__r   )1r   urllib2r   r   r   r   urllibr   r-   r   collectionsr	   r   urllib.requesturllib.parseiocollections.abcreostimehmacr   hashlib	mimetypesr@   r   r   r   r   r   r8   r   DEFAULT_SCOPERW_SCOPEr4   rB   rL   objectrN   rb   rw   rE   	Exceptionr   r   r   r]   doctesttestmodr   r   r   <module>r      sQ  + +Z)FFFFFFFFFFFF((((((!!!!!!$$$$$$    )MMMMMMMMMMMM......(((((((( C C C C C C C C C C C C C C C C C C C C C C C C C C C C C C C C C C C C 0 0 0 0 0 0 0 0 0 0
dMM^o! ! ! 3  . . .6 6 6    &          >q q q q qV q q qf       ! ! ! ! !y ! ! !< < < < <8 < < <
	 	 	 	 	x 	 	 	 zNNNGO s	   "'  A	