a
    K56aa                     @   sL  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	Z	d dl
Z
d dlmZ zd dlZW n ey   dZY n0 d dlmZmZ d dlmZmZmZmZmZmZ d dlmZ d dlZe j ZdZz2e d! \Z"Z#e$e"pde$e#pd	 Z%Z&W n   d
\Z%Z&Y n0 dd Z'e'Z(e'Z)dd Z*dDddZ+edurte,d ze-d W n   Y n0 dd Z.G dd de/Z0dEddZ1dFddZ2dGddZ3d d! Z4e4Z5dHd"d#Z6d$d% Z7dId&d'Z8d(d) Z9dJd*d+Z:dKd,d-Z;dLd.d/Z<dMd1d2Z=dNd3d4Z>d5d6 Z?d7d8 Z@d9d: ZAdOd;d<ZBdPd>d?ZCd@dA ZDdBdC ZEdS )Q    N)reduce)debbugs	hiermatch)NoReport	NoPackageNoBugs	NoNetworkQuertBTSErrorInvalidRegex)launch_browserzus-asciiz	stty size   O   )r   r   c                 G   s.   t sd S |r| | } tj|  tj  d S N)ISATTYsysstderrwriteflush)messageargs r   6/usr/lib/python3/dist-packages/reportbug/ui/text_ui.pyewrite9   s    r   c                 C   s4   zt  }W n ty(   t d Y n0 t | S )N/)osgetcwdOSErrorchdirsystem)Zcmdlinexr   r   r   r   G   s
    r    c                 C   s`   |st d }|rd| }nd}d|  } | s8|d S tj| |||d}|drX|S |d S )z.Wrapper for textwrap.fill to the existing API.    r    
)widthZinitial_indentZsubsequent_indent)columnsjoinsplittextwrapfillendswith)text	starttextindentlinelenZsioutputr   r   r   indent_wrap_textO   s    


r0   ztab: completer"   c
           
      C   s   z^t |}||vr:d|  kr*t|kr:n n||d  }tj| t||||||||	 W n tyz   td| Y n0 d S )Nr!   Invalid report number: %s
)	intlen	reportbugutilslaunch_mbox_readerr   get_report_url
ValueErrorr   )
mbox_reader_cmdbtsbugsnumbermirrorsarchivedmboxZ	mboxmaint
http_proxytimeoutr   r   r   _launch_mbox_readerm   s    $rB   c                   @   s   e Zd ZdddZdd ZdS )our_completerNc                 C   s   d | _ |rttt|| _ d S r   )completionstuplemapstr)selfrD   r   r   r   __init__|   s    zour_completer.__init__c                    s:   | j s
d S  fdd| j D }|t|k r2|| S d S d S )Nc                    s   g | ]}|  r|qS r   )
startswith.0r   r+   r   r   
<listcomp>       z*our_completer.complete.<locals>.<listcomp>)rD   r3   )rH   r+   iZmatchingr   rM   r   complete   s    zour_completer.complete)N)__name__
__module____qualname__rI   rQ   r   r   r   r   rC   {   s   
rC   c                 C   s   t j }|st j|  t j  trH|r:|s:t|j}|rHt	| z|rXt
| }nt
 }W n ty~   td tY n0 trt	d  | S )Nz
User interrupt (^D).
)r   stdoutisattyr   r   r   readlinerC   rQ   Zset_completerinputEOFErrorr   
SystemExitstrip)promptrD   	completerZisttyZretr   r   r   our_raw_input   s&    







r^   Fc              	   C   s.  d}|D ]}|t jv r|} q q|s(i }d|vr8|d }|  } | d dkrX| d d } |rx| d d| d d }n"t| d d| d  d }t||}	|rT|	r|	d d	kr|	d
d  }	qt|trzt	t|	W S  t
y   Y n0 n^zDt|	}
|
|v rt	|
W S t|}|  ddtt	| }W n t
tfyR   Y n0 |	s^|}	|	d }	|	dkrd|d< |D ]P}	|	t jv rd}nd}|||	||	 d7 }tt|d d|	 d q|t| ||||S |	 |v s|	 |v r|	 S |rtt| ntd t| ||||S )Nr    ?z [|z]?r"   r   #r!   z(Only the following entries are allowed: , zDisplay this help.z
(default) zNo help for this option.r#   z%s -    zInvalid selection.
)stringZascii_uppercaserstripr&   r0   r[   r^   
isinstancer2   rG   r8   listsortrF   	TypeErrorgetlowerr   select_optionsupper)msgokhelpallow_numbersnowrapZerr_messageoptiondefaultZlongmsgZchr<   Znumsdescr   r   r   rm      sn    
"




rm   Tc                 C   s:   |r
d}nd}t | |||dd|d}|dkr2t|dkS )z"Return True for yes, False for no.ZYnqZyNqzQuit.)ynq)rs   ry   rw   )rm   rZ   )ro   ZyeshelpZnohelpru   rs   rp   resr   r   r   yes_no   s    r{   c                    sN   |r| | }n| }t d  d fdd| D }| d }t| d S )Nr!   r#   c                 3   s   | ]}t j| d V  qdS ))r$   N)r(   r)   )rL   parr.   r   r   	<genexpr>   rO   zlong_message.<locals>.<genexpr>)r%   r&   
splitlinesrf   r   )r+   r   Zmtextr   r}   r   long_message   s    
r   c                 C   s   | rLt | dt d k rL|sL|r>d| |f } t| ||p:|}qt| ||}n6| r\tt|  |rvtd| ||pr|}ntd||}|S )N      z	%s [%s]: z[%s]> z> )r3   r%   r^   r   r0   )r\   optionstitleempty_okforce_promptru   r]   responser   r   r   
get_string  s    r   c                 C   sJ   t d t t| d  g }tddd }|s2q>|| qt d |S )Nr#   z+  Press ENTER on a blank line to continue.
r    T)r   )r   r0   r   r[   append)r\   lentryr   r   r   get_multiline  s    r   c                 C   s
   t  | S r   )getpass)r\   r   r   r   get_password'  s    r   c                 C   sb   t j| } t j| } t| d }|s.d S |t|k rZ|| }t j|rV|d S |S d S d S )N*r   )r   path
expanduser
expandvarsglobr3   isdir)r+   rP   pathsr   r   r   r   FilenameCompleter+  s    r   c                 C   s   t | |||tdS )N)r   r   ru   r]   )r   r   )r\   r   r   ru   r   r   r   get_filename;  s    r   c              
   C   s   t | |||||dddS )NTF)r   orderextrasmultipler   )menu)r|   r   r\   r   r   r   r   r   r   select_multiple@  s    r   c
              
   C   s  i }
|sg }nt |}|r&t|d  tt| tdd  t|tr| }|rg }|D ]$}||v rX|||| f ||= qXt | }|	  |D ]}|| q|}nt | }|	  |r|d d}|dg7 }dd |D }|| }t
tt tt|td	 }tttt|d
 }d
}|D ]T\}}t||| d	 d|||||f d}t| t|dk r|td |d
7 }q2t|dkrtd |r|d7 }|r|d|  }n|}t||}|s|}z<t|}d
|  krt|krn n||d
  d }W n ttfy,   Y n0 ||v sH||kr|r|r|dkrdt |
 S |dkrrg S |
|r|
|= nd
|
|< tddt |
   t|
rd}nd}qn|S |r|r|S |	r|sd S td qd S )Nz

r}   r#   )noner    r   donec                 S   s   g | ]}|d  qS )r   r   rK   r   r   r   rN   l  rO   zmenu.<locals>.<listcomp>r   r!   z%*d %-*.*s  )r-   r,      z(one at a time) z[%s] r   z- selected: %s
rc   zInvalid entry.
)rh   r   r0   r%   rg   dictcopyr   itemsri   minmaxrF   r3   r2   mathZceilZlog10r^   r8   rj   keysrk   r&   )r|   r   r\   ru   r   Zany_okr   r   r   r   ZselectedZolistkeyrt   allowedZmaxlen_namedigitsrP   namerv   r+   Zapromptr   Znumr   r   r   r   E  s    



 


r   noc
                 C   sJ  t j| }
td| |
d  zt j| |||d||d}W n   d }Y n0 |sZtd|  t|\}}|js|jrtd td d}d	}|rd
||j	|| f }nd|j	|d f }|s4z@t
jdt
jdd}|| W d    n1 s0    Y  W n8 ty2 } z|jtjkrn W Y d }~n
d }~0 0 d	}d}|d t|k rZd|  }|rhd| }td|dddddddddttdt|d d}|dkr|S |dkrtq|dkrtt || || d }q|d!krtj|	t || ||d d || d }q|d"kr qFq|d#kr4|d7 }q|dkr|d8 }qd S )$Nz5Retrieving report #%d from %s bug tracking system...
r   r!   )r=   Z	followupsr@   r>   zNo report available: #%s
z*Cannot retrieve bug's package, exiting...
r`   r   FzFollowup %d - %s

%szOriginal report - %s

%ssensible-pagerignorestdinerrorsZxOrbeqNpzWhat do you want to do now?zProvide extra information.z/Show other bug reports (return to bug listing).zShow next message (followup).z!Show previous message (followup).zRedisplay this message.z&Launch e-mail client to read full log.z$Launch web browser to read full log.I'm bored; quit please.)r   orx   r   rebry   rr   r   ry   r   Tr   r   rx   )r   SYSTEMSr   
get_reportr   packagesourcer   exitsubject
subprocessPopenPIPEcommunicateIOErrorerrnoZEPIPEr3   rl   rm   rh   ranger   r   r7   r4   r5   r6   )r<   r   r=   r@   rA   screen	queryonlyr   r>   r9   ZsysinfoinfoZbuginfoZmessagesZcurrent_messageZ
skip_pagerr+   fdr   r   r   r   r   show_report  s    




,








r   c                    s  t j| d}|s,tdt j| d  d S d}|	r8d}t| tr\tdt j| d | | n(tdt j| d ddd	 | D | g }z$t j| ||||
|	||d
\}}}W nF t	y } z.dt j| d t
|f }t|W Y d }~n
d }~0 0 z|s|d u rtntg }|rNg }|D ]"}|d D ]}||j q0q$|W S |r|D ].}|d D ]}d|j|jf }t| qdqXtd |D ]}|d }i  g }|d D ]:}d}|jdkrd}d|j|f  |j< ||j q|| fdd	tt  |dD f |t||d q|}|sP|d u rJtntn |dkrftd| n
td| t||||||||||| |	|W S  ty   td tY n0 d S )NZbtsrootz7%s bug tracking system has no web URL; bypassing query
r   r    z	 (source)z'Querying %s BTS for reports on %s%s...
r"   c                 S   s   g | ]}t |qS r   )rG   rK   r   r   r   rN     rO   z$handle_bts_query.<locals>.<listcomp>)r=   versionr   r@   r>   z+Unable to connect to %s BTS (error: "%s"); r!   #%d  %sr   r   z  [RESOLVED]z%s%sc                    s   g | ]}d | | f qS )r   r   )rL   kZbugs_newr   r   rN   M  rO   )reversez%d bug report found:

z%d bug reports found:

z No record of this package found.)r   r   rk   r   rg   rG   r   r&   Zget_reports	Exceptionreprr	   r   r   r   Zbug_numr   printr   r   pendingsortedrh   r   extendbrowse_bugs)r   r:   rA   r=   r@   r   r   r   r>   r   r   r?   Zbuglistr9   Zlatest_firstrootZsrcstrr;   count	hierarchyr   errmsgZhierarchy_newZmboxbuglistr   bugro   Z	entry_newZbugs_numbersr   r   r   r   handle_bts_query  s    



,



r   c           ,      C   sp  zt  }W n8 t jyD } zt| td W Y d }~n
d }~0 0 d }}d }}| d }g }ttt|}dt| d }|rl|d7 }|dd \}}|dr|d d }qt|}t|r2|d t	d  }|t	d d  }t|}d }}|dkrd	}|dkrd	}|r2|
d
|||f  n|
|d  || }}|D ]8} |d }|d }|
||| d t| d  f  qN|| d ks|t	t| |d  d  d krd}!||krd	}!d}"|rd}"d|||f }#|#d }$|r|#d }$|ttd|d  }%dddddddddd	}&|!rF|&d d d d |&d< |D ]}'tj|' qJt|$|"|&|%d}(|(dkrg }q$qF|(d kr|rtd!|
  ntd"|
  qFqF|(d#krȐqFqF|(d$krtqF|(dkrd S |(d%kr|rd S t|dkrd&})n
td'|%})|)r>|)d d(kr>|)dd  })q|)rzPt|)})|)|vrd|)  krtt|krn n||)d  })t|)|d W S  ty   td)|) Y n0 ntqF|(d*krt| |||||||	|
|
}*t|*tr|*d+v rqFn|*S qF|(d,kr6td-|%})t||||)|d.d/d/||
 n|(d0ksJ|(d1krjt|dkr^d&})n
td2|%})n|(})|)r|)d d(kr|)dd  })qn|)rFzht|)})|)|vrd|)  krt|krn n||)d  })t|)|||||||	|d3	}+|+r|+W S W n ty   td)|) Y n0 qF|d }d}q|| d krDql|d }| | }|r|
d |d }qd S )4Nr!   r     %d) %s
r   :r`   sr    z %s: %d remain%s
r#   r    (s to skip rest)Z	yNbmrqsfeZNbmrqfe(%d-%d/%d) !Is the bug you found listed aboveWhat would you like to do next;Problem already reported; optionally add extra information..Problem not listed above; possibly check more.-Open the complete bugs list in a web browser.`Get more information about a bug (you can also enter a number
     without selecting "m" first).Redisplay the last bugs shown.r   7Skip remaining problems; file a new report immediately.'Open the report using an e-mail client.z Filter bug list using a pattern.)	rw   rx   r   mr   ry   r   r   frx    (skip to Next page).r   r   zhttps://bugs.debian.org/src:%shttps://bugs.debian.org/%sr   ry   rw   1[Enter the number of the bug report you want to give more info on,
or press ENTER to exit: #rb   r1   r   	FilterEndTopr   <Please enter the number of the bug you would like to view: #r   Tr   rP   APlease enter the number of the bug you would like more info on: #)r   r   r   r9   )localegetpreferredencodingErrorr   r   r   r3   rG   r*   rowsr   r%   rh   r   r   r   rm   r   r   r^   r2   r   r   r8   r   search_bugsrg   rB   r   ),r   r   r;   r:   r   r=   r@   rA   r   r   r   r   r9   output_encodingro   endcountcatcountscount
startcountcategorylastpager   linefmtcatnamereportstotaltheseremaintpluralrplural	oldscount	oldecountreportskipmsgr   rstrpstrr   helptextliner   r<   retvalrz   r   r   r   r   f  s0    




&






*




*

r   c                 C   sz   dd | D }|r"t dd |}ndS g }td}| p:g D ]4}|d D ]&}||}|rH|t|d qHq<||fS )z.Find out bug count and bug # in the hierarchy.c                 S   s   g | ]}t |d  qS )r!   )r3   )rL   rP   r   r   r   rN     rO   z"proc_hierarchy.<locals>.<listcomp>c                 S   s   | | S r   r   )r   rw   r   r   r   <lambda>  rO   z proc_hierarchy.<locals>.<lambda>)r   r   z\#(\d+)[ :]r!   )r   recompilematchr   r2   group)r   Zlenlistr   r;   expr   r   r  r   r   r   proc_hierarchy  s    

r  c
           .      C   s  zt  }
W n8 t jyD } zt| td W Y d}~n
d}~0 0 td}|sVdS zt| |}W n t	y   td Y dS 0 t
|\}}td}|std dS d }}d }}|d }g }ttt|}d	t| d
 }|r|d }|dd \}}|dr |dd }qt|}t|r|dtd  }|td d }t|}d }}|dkrpd}|dkr~d}|r|d|||||f  n|d|||f  || } }!|D ]8}"|d }|d }||||"dt| d  f  q||d ks*|tt||d  d  d kr(d}#||kr<d}#d}$|rJd}$d|||f }%|%d }&|rn|%d }&|ttd|d  }'dddddddd d!d"d#d$}(|#r|(d% dd d& |(d%< |D ]})tj|) qt|&|$|(|'d'}*|*d%krg }qq|*d(krtd)|  q|*d*kr(qq|*d+kr:tq|*dkrHdS |*d,kr|r\dS t|dkrpd-}+n
td.|'}+|+r|+d d/kr|+dd }+qz|+rzPt|+}+|+|vrd|+  krt|krn n||+d  }+t|+|d W S  ty   td0|+ Y n0 ntq|*d1krft||||||||||	
},t |,tr^|,d2v r^qn|,S q|*d3krtdS |*d4krd5S |*d6krtd7|'}+t!|	|||+|d8d9d9||
 n|*d:ks|*d;krtd<|'}+n|*}+|+r|+d d/kr|+dd }+q|+rzft|+}+|+|vr@d|+  kr0t|kr@n n||+d  }+t"|+|||||||d=}-|-rd|-W S W n ty   td0|+ Y n0 q|d }d}q(||d krq|d7 }|| }|r|d> |d7 }qdS )?z(Search for the bug list using a pattern.r!   NzXEnter the search pattern (a Perl-compatible regular expression)
or press ENTER to exit: r   z4Invalid regular expression, press ENTER to continue.z\#(\d+):z(No match found, press ENTER to continue.r   r   r   r   r   r`   r   r    z %s: %d report%s (%d remain%s)
z %s: %d report%s
r   r   ZyNbmrqsfuteZ	Nmbrqfuter   r   r   r   r   r   r   r   r   r   z)Filter (search) bug list using a pattern.zUp one level of filter.r   z)Top of the bug list (remove all filters).)rw   rx   r   r   r   ry   r   r   ur   trx   r   r   r   r   r   ry   rw   r   r   rb   r1   r   r   r  r  r   r   r   r   Tr   rP   r   )r   r   r   r#   )#r   r   r   r   r   r   r^   r   Zmatched_hierarchyr
   r  r  r  r3   rG   r*   r   r   r%   rh   r   r   r   rm   r   r   r2   r   r   r8   r   r   rg   rB   r   ).Zhierarchyfullr:   r   r=   r@   rA   r   r   r   r9   r   ro   patternr   r   r;   r  r   r   r   r   r  r  r   r  r  r  r  r  r  r	  r
  r  r  r  r  r   r  r  r   r  r  r   r<   r  rz   r   r   r   r   )  sP    




&






*




*


r   c                 C   s   |st |  d S |r| d7 } tjdd}z@tj|tjdd}||  W d    n1 s^0    Y  W n ty|   Y n0 d S )Nz[
(You may need to press 'q' to exit your pager and continue using
reportbug at this point.)ZPAGERr   r   r   )	r   r   environrk   r   r   r   r   r   )r+   Z	use_pagerZpresubjZpagerr   r   r   r   display_report  s    ,r  utf-8c                 C   s  |st d | dfS tj| d }d}t|dd6}t|D ]\}}|dkrB|sB|d }qBW d    n1 st0    Y  d}	d|v rd	}	|r|d
v rd| }	n<|dv sd|v rd| }	n"|dv rd| }	n|dkrd| }	d|v s|dkrt d| n
t d| td||	|f }
|
r,t d||
 tj|sNt d| t	
d d|v r\dS t|dd}| }W d    n1 s0    Y  || krt d ||| kfS )NzNo editor found!
r   backslashreplace)r   r#   r   r    vimz-f )ZviZnvir"  ZelvisZgvimZkvimz-c :%d)z
elvis-tinyZ	gnuclientZeeZpicoZnanoZzileZemacsz+%d)ZjedZxjedz-g %dZkatez	--line %d&zASpawning %s in background; please press Enter when done editing.
zSpawning %s...
z
%s %s '%s'z)Warning: possible error exit from %s: %d
zBug report file %s removed!r!   )Nr!   z$No changes were made in the editor.
)r   r   r   basenamer'   open	enumerater   existsr   r   read)r   filenameZeditorcharsetZednameZourliner   linenor  ZoptsresultZ
newmessager   r   r   spawn_editor  sR    (






(
r-  c                   C   s   dS )NTr   r   r   r   r   
initialize;  s    r.  c                   C   s
   t j S r   )r   r   rV   r   r   r   r   	can_input?  s    r/  )r    r   N)NNN)NF)TF)NNFFr    N)N)NFr    )NNN)NNFNNFF)NFr    r   N)Nr    Fr    Nr   FNFNNF)TF)r   )Fr   r   r   r  r   re   r   r   r   r(   r   	functoolsr   rW   ImportErrorr4   r   r   Zreportbug.exceptionsr   r   r   r   r	   r
   Zreportbug.urlutilsr   Zreportbug.utilsr   rV   r   r*  Z	getoutputr'   r   cr2   r   r%   r   Zlog_messageZdisplay_failurer   r0   parse_and_bindZset_completer_delimsrB   objectrC   r^   rm   r{   r   Zfinal_messager   r   r   r   r   r   r   r   r   r   r  r   r  r-  r.  r/  r   r   r   r   <module>   s   
 
 





D
  



  
j  
T    
d 0 K

9