ó
‹²,]c           @   s  d  Z  d d l Z d d l Z d d l Z y d d l Z Wn e k
 rM n Xd d l m Z d d l m	 Z	 m
 Z
 d d l m Z d d l m Z m Z d d l m Z d d l m Z d d	 l m Z d
 „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   sº   
Backend to open issues automatically on Github.

Adapted from qcrash/backends/base.py and qcrash/backends/github.py of the
`QCrash Project <https://github.com/ColinDuquesnoy/QCrash>`_.
iÿÿÿÿN(   t   Qt(   t   qAppt   QMessageBox(   t   CONF(   t   _t   running_under_pytest(   t   PY2(   t   github(   t   DlgGitHubLoginc           C   s   t  j t ƒ S(   N(   t   loggingt	   getLoggert   __name__(    (    (    s<   lib/python2.7/site-packages/spyder/widgets/github/backend.pyt   _logger%   s    t   BaseBackendc           B   s2   e  Z d  Z d e d „ Z d „  Z d d „ Z RS(   s"  
    Base class for implementing a backend.

    Subclass must define ``button_text``, ``button_tooltip``and ``button_icon``
    and implement ``send_report(title, description)``.

    The report's title and body will be formatted automatically by the
    associated :attr:`formatter`.
    c         C   s:   | |  _  | |  _ | |  _ | |  _ | |  _ d |  _ d S(   sQ  
        :param formatter: the associated formatter (see :meth:`set_formatter`)
        :param button_text: Text of the associated button in the report dialog
        :param button_icon: Icon of the associated button in the report dialog
        :param button_tooltip: Tooltip of the associated button in the report
            dialog
        :param need_review: True to show the review dialog before submitting.
            Some backends (such as the email backend) do not need a review
            dialog as the user can already review it before sending the final
            report
        N(   t	   formattert   button_textt   button_tooltipt   button_icont   need_reviewt   Nonet   parent_widget(   t   selfR   R   R   R   R   (    (    s<   lib/python2.7/site-packages/spyder/widgets/github/backend.pyt   __init__3   s    					c         C   s   | |  _  d S(   sÊ   
        Sets the formatter associated with the backend.

        The formatter will automatically get called to format the report title
        and body before ``send_report`` is being called.
        N(   R   (   R   R   (    (    s<   lib/python2.7/site-packages/spyder/widgets/github/backend.pyt   set_formatterG   s    c         C   s
   t  ‚ d S(   s<  
        Sends the actual bug report.

        :param title: title of the report, already formatted.
        :param body: body of the reporit, already formtatted.
        :param application_log: Content of the application log.
        Default is None.

        :returns: Whether the dialog should be closed.
        N(   t   NotImplementedError(   R   t   titlet   bodyt   application_log(    (    s<   lib/python2.7/site-packages/spyder/widgets/github/backend.pyt   send_reportP   s    N(   R   t
   __module__t   __doc__R   t   TrueR   R   R   (    (    (    s<   lib/python2.7/site-packages/spyder/widgets/github/backend.pyR   )   s   			t   GithubBackendc           B   sY   e  Z d  Z d d „ Z d d „ Z d „  Z e d „ Z e d „ Z	 d „  Z
 d „  Z RS(	   sî   
    This backend sends the crash report on a github issue tracker::

        https://github.com/gh_owner/gh_repo

    Usage::

        github_backend = spyder.widgets.github.backend.GithubBackend(
            'spyder-ide', 'spyder')
    c         C   s>   t  t |  ƒ j | d d d ƒ | |  _ | |  _ t |  _ d S(   sˆ   
        :param gh_owner: Name of the owner of the github repository.
        :param gh_repo: Name of the repository on github.
        s   Submit on githubs/   Submit the issue on our issue tracker on githubN(   t   superR    R   R   t   gh_ownert   gh_repoR   t   _show_msgbox(   R   R"   R#   R   (    (    s<   lib/python2.7/site-packages/spyder/widgets/github/backend.pyR   i   s    
		c         C   sG  t  ƒ  j d | | ƒ |  j ƒ  } | d } | d } | d } | d } | d }	 | d  k r| | d  k r| | d  k r| t St  ƒ  j d ƒ | r² |  j | ƒ }
 | d |
 7} n  ym | rÐ t j d	 | ƒ } n t j d | d | ƒ } | j |  j	 ƒ |  j
 ƒ } | j j d
 | d | ƒ } Wn¨ t j k
 rÉ} t  ƒ  j d | j ƒ | j j d k r|  j rÅt j |  j t d ƒ t d ƒ ƒ qÅn8 |  j rÅt j |  j t d ƒ t d ƒ | j j ƒ n  t SX| d } |  j rt j |  j t d ƒ t d ƒ ƒ } n  | t j t j g k r?t j d |  j	 |  j
 | f ƒ n  t Sd  S(   Ns-   sending bug report on github
title=%s
body=%st   usernamet   passwordt   remembert   tokent   remember_tokens   got user credentialss   
Application log: %st   access_tokenR   R   s0   Failed to send bug report on Github. response=%ri‘  s   Invalid credentialss5   Failed to create Github issue, invalid credentials...s   Failed to create issues'   Failed to create Github issue. Error %dt   numbers   Issue created on GithubsQ   Issue successfully created. Would you like to open the issue in your web browser?s"   https://github.com/%s/%s/issues/%d(   R   t   debugt   get_user_credentialsR   t   Falset   upload_log_fileR   t   GitHubt   reposR"   R#   t   issuest   postt   ApiErrort   warningt   responset   codeR$   R   R   R   t   questiont   Yest   Okt
   webbrowsert   openR   (   R   R   R   R   t   credentialsR%   R&   R'   R(   R)   t   urlt   ght   repot   rett   et	   issue_nbr(    (    s<   lib/python2.7/site-packages/spyder/widgets/github/backend.pyR   u   sZ    





$
				
	c         C   sU   t  j d d ƒ } t  j d d ƒ } t  j d d d ƒ } | sH d } n  | | | f S(   s"   Get the stored credentials if any.t   mains   report_error/remember_mes   report_error/remember_tokens   report_error/usernamet    (   R   t   get(   R   t   remember_meR)   R%   (    (    s<   lib/python2.7/site-packages/spyder/widgets/github/backend.pyt   _get_credentials_from_settings°   s    	c         C   sž   | r‡ | r‡ | r‡ t  j d d | ƒ y t j d | | ƒ Wq‡ t k
 rƒ |  j rz t j |  j t	 d ƒ t	 d ƒ ƒ n  t
 } q‡ Xn  t  j d d | ƒ d S(   s!   Store credentials for future use.RD   s   report_error/usernameR   s   Failed to store passwords‹   It was not possible to securely save your password. You will be prompted for your Github credentials next time you want to report an issue.s   report_error/remember_meN(   R   t   sett   keyringt   set_passwordt	   ExceptionR$   R   R5   R   R   R.   (   R   R%   R&   R'   (    (    s<   lib/python2.7/site-packages/spyder/widgets/github/backend.pyt   _store_credentialsº   s    		c         C   s…   | rn | rn y t  j d d | ƒ Wqn t k
 rj |  j ra t j |  j t d ƒ t d ƒ ƒ n  t } qn Xn  t	 j
 d d | ƒ d S(   s   Store token for future use.R   R(   s   Failed to store tokens‚   It was not possible to securely save your token. You will be prompted for your Github token next time you want to report an issue.RD   s   report_error/remember_tokenN(   RJ   RK   RL   R$   R   R5   R   R   R.   R   RI   (   R   R(   R'   (    (    s<   lib/python2.7/site-packages/spyder/widgets/github/backend.pyt   _store_tokenÌ   s    		c         C   sù  d } d } |  j ƒ  \ } } } t o6 t j j d ƒ } | r§ | r§ | r§ y t j d | ƒ } Wq§ t k
 r£ |  j	 r¤ t
 j |  j t d ƒ t d ƒ ƒ q¤ q§ Xn  | r| ry t j d d ƒ } Wqt k
 r
|  j	 rt
 j |  j t d ƒ t d ƒ ƒ qqXn  t ƒ  sÐt j |  j | | | | | ƒ } | d r‹| d	 r‹| r‹|  j | d | d	 | d
 ƒ t j d d | d
 ƒ n  | d rõ| rõ|  j | d | d ƒ t j d d | d ƒ qõn% t d | d	 | d d d
 | d | ƒ S| S(   s+   Get user credentials with the login dialog.t   linuxR   s   Failed to retrieve passwordsI   It was not possible to retrieve your password. Please introduce it again.R(   s   Failed to retrieve tokensF   It was not possible to retrieve your token. Please introduce it again.R%   R&   R'   RD   s   report_error/remember_meR)   s   report_error/remember_tokenRE   N(   R   RH   R   t   syst   platformt
   startswithRJ   t   get_passwordRL   R$   R   R5   R   R   R   R   t   loginRM   R   RI   RN   t   dict(   R   R&   R(   R%   RG   R)   t   valid_py_osR=   (    (    s<   lib/python2.7/site-packages/spyder/widgets/github/backend.pyR-   Þ   sT    							c      
   C   s   t  j ƒ  } yM t j t j ƒ | j j d d d t d i i | d 6d 6ƒ } t j	 ƒ  Wn% t  j
 k
 r€ t ƒ  j d ƒ d SX| d	 Sd  S(
   Nt   descriptions   SpyderIDE logt   publict   filest   contents   SpyderIDE.logs%   Failed to upload log report as a gists%   "Failed to upload log file as a gist"t   html_url(   R   R0   R   t   setOverrideCursorR    t
   WaitCursort   gistsR3   R   t   restoreOverrideCursorR4   R   R5   (   R   t   log_contentR?   RA   (    (    s<   lib/python2.7/site-packages/spyder/widgets/github/backend.pyR/     s    N(   R   R   R   R   R   R   RH   R.   RM   RN   R-   R/   (    (    (    s<   lib/python2.7/site-packages/spyder/widgets/github/backend.pyR    ^   s   
;	
	;(   R   R	   RP   R;   RJ   RL   t   qtpy.QtCoreR    t   qtpy.QtWidgetsR   R   t   spyder.config.mainR   t   spyder.config.baseR   R   t   spyder.py3compatR   t   spyder.utils.externalR   t   spyder.widgets.github.gh_loginR   R   t   objectR   R    (    (    (    s<   lib/python2.7/site-packages/spyder/widgets/github/backend.pyt   <module>   s"   	5