
CWj.                 @   sL  d  d l  m Z y d  d l j Z Wn e k
 rC d  d l Z Yn Xd  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 e j d k r d Z n d Z e e j d d d	 e  Z Gd
 d   d e  Z Gd d   d e  Z Gd d   d e  Z d d   Z Gd d   d e  Z d S)    )print_functionN)
cmp_to_key3gettextZugettextzcommand-not-foundZfallbackTc               @   s(   e  Z d  Z d d   Z d d   Z d S)BinaryDatabasec             C   s}   d  |  _  | j d  ry y t j | d  |  _  WnE t j k
 rx } z" t d | | f d t j WYd  d  } ~ Xn Xd  S)Nz.dbrz%Unable to open binary database %s: %sfile)dbendswithgdbmopenerrorprintsysstderr)selffilenameerr r   A/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py__init__   s    	zBinaryDatabase.__init__c             C   sU   t  | t  s! | j d d  } |  j rM | |  j k rM |  j | j d  Sd  Sd  S)Nzutf-8xmlcharrefreplace)
isinstancebytesencoder	   decode)r   keyr   r   r   lookup%   s
    zBinaryDatabase.lookupN)__name__
__module____qualname__r   r   r   r   r   r   r      s   r   c               @   s@   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 S)
FlatDatabasec             C   sZ   g  |  _  t |  > } x4 d d   | D D] } |  j  j | j d   q, WWd  QRXd  S)Nc             s   s   |  ] } | j    Vq d  S)N)strip).0liner   r   r   	<genexpr>5   s    z(FlatDatabase.__init__.<locals>.<genexpr>|)rowsr   appendsplit)r   r   Zdbfiler$   r   r   r   r   2   s    	zFlatDatabase.__init__c             C   s;   g  } x. |  j  D]# } | | | k r | j |  q W| S)N)r'   r(   )r   columntextresultrowr   r   r   r   8   s
    zFlatDatabase.lookupc             C   s/   x( |  j  D] } | j | | |   q
 Wd  S)N)r'   r(   )r   cbr*   r-   r   r   r   createColumnByCallback?   s    z#FlatDatabase.createColumnByCallbackc             C   s>   g  } x1 |  j  D]& } | | | |  r | j |  q W| S)N)r'   r(   )r   r*   r.   r+   r,   r-   r   r   r   lookupWithCallbackC   s
    zFlatDatabase.lookupWithCallbackN)r   r   r    r   r   r/   r0   r   r   r   r   r!   0   s   r!   c               @   s:   e  Z d  Z e d  \ Z Z d d   Z d d   Z d S)ProgramDatabase   c             C   sM   t  j j |  } | j d  d j d  \ |  _ |  _ t |  |  _ d  S)N.r   -)ospathbasenamer)   Zarch	componentr   r	   )r   r   r7   r   r   r   r   O   s    (zProgramDatabase.__init__c             C   s-   |  j  j |  } | r% | j d  Sg  Sd  S)Nr&   )r	   r   r)   )r   commandr,   r   r   r   r   T   s    zProgramDatabase.lookupN)r   r   r    rangeZPACKAGEZBASENAME_PATHr   r   r   r   r   r   r1   K   s   r1   c                s   d    f d d   t  t   d  D } d d   | D } d d   | D }   f d d   | D }   f d d   | D } t | | | |  S)	zw
    return a set with spelling1 distance alternative spellings

    based on http://norvig.com/spell-correct.html
    z&abcdefghijklmnopqrstuvwxyz-_0123456789c                s0   g  |  ]& }   d  |    | d   f  q S)Nr   )r#   i)wordr   r   
<listcomp>c   s   	 z!similar_words.<locals>.<listcomp>   c             S   s0   g  |  ]& \ } } | r | | d  d   q S)r>   Nr   )r#   abr   r   r   r=   d   s   	 c             S   sL   g  |  ]B \ } } t  |  d  k r | | d  | d | d d   q S)r>   r   r2   N)len)r#   r?   r@   r   r   r   r=   e   s   	 c                sA   g  |  ]7 \ } }   D]$ } | r | | | d  d   q q S)r>   Nr   )r#   r?   r@   c)alphabetr   r   r=   f   s   	 c                s1   g  |  ]' \ } }   D] } | | |  q q Sr   r   )r#   r?   r@   rB   )rC   r   r   r=   g   s   	 )r:   rA   set)r<   sZdeletesZ
transposesZreplacesZinsertsr   )rC   r<   r   similar_words\   s    )rF   c               @   s   e  Z d  Z d Z d Z d	 d
 d  Z d d d d  Z d d   Z d d   Z d d   Z	 d d   Z
 d d   Z d d d  Z d S)CommandNotFoundz
programs.d/bin/usr/bin/usr/local/bin/sbin	/usr/sbin/usr/local/sbin
/usr/gamesz/usr/share/command-not-foundc             C   sf  g  |  _  g  |  _ t j j | d  } t j j |  re t |   } d d   | D |  _ Wd  QRXd d d d d d	 g |  _ |  j j   |  j	   |  _
 x` t j t j j j | |  j g   D]7 } |  j  j t t j j j | |  j | g    q WyE t j d
  d t j   k p=t j d  d t j   k |  _ Wn t k
 rad |  _ Yn Xd  S)Nzpriority.txtc             S   s   g  |  ] } | j     q Sr   )r"   )r#   r$   r   r   r   r=   ~   s   	 z,CommandNotFound.__init__.<locals>.<listcomp>mainZuniverseZcontribZ
restrictedznon-freeZ
multiverseZsudor2   ZadminF)programspriority_overridesr5   r6   joinexistsr   
componentsreverse_getSourcesListsources_listlistdirsepprograms_dirr(   r1   grpZgetgrnamposix	getgroupsuser_can_sudoKeyError)r   Zdata_dirpZpriority_filer   r   r   r   r   x   s     		.5EzCommandNotFound.__init__      c             C   s   t  |  | k  r d Sg  } xP t |  D]B } |  j |  } x* | D]" \ } } | j | | | f  qE Wq) Wt  |  | k r t t d  | t  |  f d t j no t  |  d k rt t d  | d t j x= | D]5 \ } }	 }
 t t d  | |	 |
 f d t j q Wd S)z try to correct the spelling Nz4No command '%s' found, but there are %s similar onesr   r   z$No command '%s' found, did you mean:z$ Command '%s' from package '%s' (%s))rA   rF   getPackagesr(   r   _r   r   )r   r<   Zmin_lenZmax_lenZpossible_alternativeswpackagespackagecompr`   rB   r   r   r   print_spelling_suggestion   s    ,z)CommandNotFound.print_spelling_suggestionc                sP   t    } x: |  j D]/   | j   f d d     j |  D  q Wt |  S)Nc                s   g  |  ] } |   j  f  q Sr   )r8   )r#   pkg)r	   r   r   r=      s   	 z/CommandNotFound.getPackages.<locals>.<listcomp>)rD   rP   updater   list)r   r9   r,   r   )r	   r   rc      s    	-zCommandNotFound.getPackagesc             C   sf   yI t  t j j t j d d  d f    } d d   | D SWd  QRXWn t k
 ra g  SYn Xd  S)NHOMEz/rootz.command-not-found.blacklistc             S   s.   g  |  ]$ } | j    d  k r | j     q S) )r"   )r#   r$   r   r   r   r=      s   	 z0CommandNotFound.getBlacklist.<locals>.<listcomp>)r   r5   rY   rR   getenvIOError)r   Z	blacklistr   r   r   getBlacklist   s
    -zCommandNotFound.getBlacklistc             C   s   y* d d  l  } d d l m } | j   Wn t t f k
 rH g  SYn Xt g   } xL | d d  D]; } | j re | j re x | j	 D] } | j
 |  q Wqe W| S)Nr   )SourcesListZwithMatcherF)apt_pkgZaptsources.sourceslistrr   ZinitSystemErrorImportErrorrD   ZdisabledZinvalidcompsadd)r   rs   rr   rW   sourcer8   r   r   r   rV      s    	zCommandNotFound._getSourcesListc             C   s   | d |  j  k r) | d |  j  k r) n. | d |  j  k r@ d S| d |  j  k rW d Sy |  j j | d  } Wn d } Yn Xy |  j j | d  } Wn d } Yn X| | p | | k | | k  S)Nr   r>   ry   ry   )rQ   rT   index)r   xyZxidxZyidxr   r   r   sortByComponent   s     zCommandNotFound.sortByComponentc             C   s   d t  j k r d  S| r t d  } t j d k rC t |  } n< t |  } t j j r t	 | t
  r | j t j j  } | j   t d  k r t j   d k r d } n d } d | | f } t d	 | d
 t j t j | j   d d d  S)NZ COMMAND_NOT_FOUND_INSTALL_PROMPTz Do you want to install it? (N/y)r   r|   r   rn   zsudo z%sapt install %sz%sr   shellF)r5   environrd   r   versioninputZ	raw_inputstdinencodingr   strr   lowerr\   geteuidr   stdout
subprocesscallr)   )r   Zpackage_namepromptZanswerZcommand_prefixZinstall_commandr   r   r   install_prompt   s     	zCommandNotFound.install_promptFc                s  d d      j  d  rN t j j   rE t j j   g } qm g  } n    f d d   |  j D } | r| rt |  d k r t t d  d  d	 t j j	 | d
   i d t
 j nZ t t d  d  i d t
 j x4 | D], } t d t j j	 |   d t
 j q Wt t |  t t j d d  j d    } t |  d
 k rt t d  d j	 |  d t
 j d d j	 |  k rt t d  d t
 j d St j j d  pt j j d  sd S |  j   k rd S|  j   } t |  d
 k r/|  j   nt |  d k rt t d   d d d t
 j t j   d
 k rt t d  d t
 j t d | d
 d
 d t
 j |  j | d
 d
  n |  j rt t d  d t
 j t d | d
 d
 d t
 j |  j | d
 d
  n1 t t d  d  d | d
 d
 i d t
 j | d
 d |  j k rt t d  | d
 d d t
 j n3t |  d k r| j d t |  j   t t d    d t
 j xt | D]l } | d |  j k rt d | d
 d t
 j qt d | d
 d! t d"  | d d# d t
 j qWt j   d
 k rt t d$  d% d t
 j nB |  j rt t d$  d& d t
 j n t t d'  d t
 j t |  d
 k S)(z7 give advice where to find the given command to stderr c             S   sA   t  j j t  j j |  |   o@ t  j j t  j j |  |   S)z? helper that returns if a command is found in the given prefix )r5   r6   rS   rR   isdir)prefixr9   r   r   r   
_in_prefix   s    !z*CommandNotFound.advise.<locals>._in_prefix/c                s%   g  |  ] }   |   r |  q Sr   r   )r#   r   )r   r9   r   r   r=      s   	 z*CommandNotFound.advise.<locals>.<listcomp>r>   z1Command '%(command)s' is available in '%(place)s'r9   Zplacer   r   z:Command '%(command)s' is available in the following placesz * %sPATHrn   :z_The command could not be located because '%s' is not included in the PATH environment variable.ZsbinzfThis is most likely caused by the lack of administrative privileges associated with your user account.Fz/usr/bin/aptz/usr/bin/aptitudez-The program '%s' is currently not installed. endzYou can install it by typing:zapt install %szsudo apt install %szWTo run '%(command)s' please ask your administrator to install the package '%(package)s'rg   z1You will have to enable the component called '%s'r   z8The program '%s' can be found in the following packages:z (z-You will have to enable component called '%s')zTry: %s <selected package>zapt installzsudo apt installz-Ask your administrator to install one of them)
startswithr5   r6   rS   dirnameprefixesrA   r   rd   rR   r   r   rl   rD   ro   r)   rq   rc   ri   r\   r   r   r^   rW   sortr   r}   )r   r9   Zignore_installedr   r   Zmissingrf   rg   r   )r   r9   r   advise   sh    	?#*1&#	1(9 	 zCommandNotFound.adviseN)rH   rI   rJ   rK   rL   rM   rN   )r   r   r    rZ   r   r   ri   rc   rq   rV   r}   r   r   r   r   r   r   rG   k   s          rG   )Z
__future__r   Zdbm.gnuZgnur   ru   r   r[   r5   os.pathr\   r   r   	functoolsr   r   Z_gettext_methodgetattrZtranslationrd   objectr   r!   r1   rF   rG   r   r   r   r   <module>   s*   	