ó
`ï}Nc           @   s¸   y* d  d l  m Z d  d l m Z e Z Wn e k
 rE Z e Z n Xd  d l	 Z
 d  d l Z d  d l Z d  d l Z d Z d d
 d „  ƒ  YZ d e f d „  ƒ  YZ d d d	 „ Z d S(   iÿÿÿÿ(   t   DesktopEntry(   t   LocaleNs   /usr/share/app-install/desktop/t   Indexerc           B   s   e  Z d d  „ Z d „  Z RS(   c         C   s®   | |  _  | |  _ | d  k r' d } n  | |  _ | j d ƒ d |  _ t j | ƒ |  _ t	 j
 ƒ  |  _ y, t	 j |  j ƒ |  _ |  j j |  j ƒ Wn t	 j k
 r© n Xd  S(   Nt   ent   _i    (   t
   val_popcont   progresst   Nonet   langt   splitt   xlangR   t   expand_languagest   xdglangst   xapiant   TermGeneratort   indexert   Stemt   stemmert   set_stemmert   InvalidArgumentError(   t   selfR   R   R   (    (    s2   /usr/share/apt-xapian-index/plugins/app-install.pyt   __init__   s    				c         C   sQ  | j  d ƒ | j  d | ƒ |  j j | ƒ t j } zR |  j t _ |  j j | j ƒ  ƒ |  j j | j ƒ  ƒ |  j j | j	 ƒ  ƒ Wd  | t _ Xx% | j
 ƒ  D] } | j  d | ƒ q£ Wy t | j d ƒ ƒ } WnE t k
 r} |  j r|  j j d | t | ƒ f ƒ n  d } n X|  j d k rM| j |  j t j | ƒ ƒ n  d  S(   Nt   XDt   XDFt   XDTs   X-AppInstall-Popcons#   %s: parsing X-AppInstall-Popcon: %siÿÿÿÿ(   t   add_termR   t   set_documentR   t   langsR   t   index_text_without_positionst   getNamet   getGenericNamet
   getCommentt   getCategoriest   intt   gett
   ValueErrorR   t   verboset   strR   t	   add_valueR   t   sortable_serialise(   R   t   documentt   fnamet   entryt   oldlangst   catt   popcont   e(    (    s2   /usr/share/apt-xapian-index/plugins/app-install.pyt   index   s(    	
	#
N(   t   __name__t
   __module__R   R   R/   (    (    (    s2   /usr/share/apt-xapian-index/plugins/app-install.pyR      s   t
   AppInstallc           B   sJ   e  Z d  „  Z d „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z	 RS(   c         C   s   | |  _  | |  _ d  S(   N(   R   R   (   R   R   R   (    (    s2   /usr/share/apt-xapian-index/plugins/app-install.pyR   B   s    	c         C   sg  d } x‹ t  j t ƒ D]z } | d d k s | j d ƒ rB q n  y: t  j j t  j j t | ƒ ƒ } | | k r{ | } n  Wq t k
 r q Xq Wt d | d t d d d d	 ƒ g d
 t d t d d ƒ g d t d d d d d d d d d d ƒ t d d d d d d d d d d ƒ t d d d d d d d d d d ƒ t d d d d d d d d d d ƒ g ƒ S(    s  
        Return general information about the plugin.

        The information returned is a dict with various keywords:
         
         timestamp (required)
           the last modified timestamp of this data source.  This will be used
           to see if we need to update the database or not.  A timestamp of 0
           means that this data source is either missing or always up to date.
         values (optional)
           an array of dicts { name: name, desc: description }, one for every
           numeric value indexed by this data source.

        Note that this method can be called before init.  The idea is that, if
        the timestamp shows that this plugin is currently not needed, then the
        long initialisation can just be skipped.
        i    t   .s   .desktopt	   timestampt   valuest   names
   app-popcont   descs    app-install .desktop popcon rankt   sourcest   paths+   .desktop files provided by app-install-datat   prefixest   idxR   t   qpt   types;   Marker to indicate that the package contains .desktop filest   ldescsi   Only 'XD' can present in the index. This is used to efficiently filter packages that have a .desktop fileR   s   File name of the .desktop filesZ   This is the name of a .desktop file contained in the package. There could be more than oneR   t   bools   Categories from .desktop filessP   This is similar to a tag, but filled with the categories found in .desktop filest   Zs   Stemmed forms of keywordssd   This contains the stemmed forms of keywords as generated by TermGenerator and matched by QueryParserN(
   t   ost   listdirt   APPINSTALLDIRt   endswithR9   t   getmtimet   joint   OSErrort   dictR   (   R   t   maxtst   ft   ts(    (    s2   /usr/share/apt-xapian-index/plugins/app-install.pyt   infoF   s4      ! c         C   s  | d } | j  d d ƒ |  _ g  d	 g t |  j ƒ D] } t | |  j | ƒ ^ q6 |  _ i  |  _ | j d t	 ƒ x‰ t
 j t	 ƒ D]x } | d d k s„ | j d ƒ r° q„ n  t t
 j j t	 | ƒ ƒ } | j  d ƒ } |  j j | g  ƒ j | | f ƒ q„ W| j ƒ  d	 S(
   s5  
        If needed, perform long initialisation tasks here.

        info is a dictionary with useful information.  Currently it contains
        the following values:

          "values": a dict mapping index mnemonics to index numbers

        The progress indicator can be used to report progress.
        R5   s
   app-popconiÿÿÿÿs   Reading .desktop files from %si    R3   s   .desktops   X-AppInstall-PackageN(   R"   R   R   t   listR   R   t   indexerst   entriest   beginRC   RA   RB   RD   R    R9   RF   t
   setdefaultt   appendt   end(   R   RL   R   R5   R   RJ   R*   t   pkg(    (    s2   /usr/share/apt-xapian-index/plugins/app-install.pyt   init{   s    
;	  &c         K   sM   | d k	 rI x: |  j D], } | j j t j j ƒ | j j | ƒ q Wn  d S(   s²   
        Receive extra parameters from the indexer.

        This may be called more than once, but after init().

        We are using this to get the database instance
        N(   R   RN   R   t	   set_flagsR   R   t   FLAG_SPELLINGt   set_database(   R   t   dbt   kwt   i(    (    s2   /usr/share/apt-xapian-index/plugins/app-install.pyt   send_extra_info–   s    c         C   s   t  d d d d d d t ƒ S(   s+  
        Return documentation information for this data source.

        The documentation information is a dictionary with these keys:
          name: the name for this data source
          shortDesc: a short description
          fullDoc: the full description as a chapter in ReST format
        R6   s   app-install informationt	   shortDescsQ   terms, categories and popcon values extracted from the app-install .desktop filest   fullDocs<  
            The AppInstall data source reads .desktop files from %s
            and adds the following terms:

             * keywords from the .desktop descriptions, via Xapian's
               TermGenerator, in all requested locales;
             * .desktop categories, with prefix XDT;
             * name of .desktop file, with prefix XDF;
             * a single term "XD", marking that the package contains .desktop
               files.

            It also adds an "app-popcon" value with popcon ranks from the
            app-install .desktop files.
            (   RH   RC   (   R   (    (    s2   /usr/share/apt-xapian-index/plugins/app-install.pyt   doc£   s    	c         C   sc   | j  ƒ  } xP |  j j | g  ƒ D]9 } | \ } } x$ |  j D] } | j | | | ƒ q> Wq" Wd S(   sÂ   
        Update the document with the information from this data source.

        document  is the document to update
        pkg       is the python-apt Package object for this package
        N(   t   get_dataRO   R"   RN   R/   (   R   R(   RT   R6   R.   R)   R*   R[   (    (    s2   /usr/share/apt-xapian-index/plugins/app-install.pyR/   ¿   s
    c         C   sc   | j  ƒ  } xP |  j j | g  ƒ D]9 } | \ } } x$ |  j D] } | j | | | ƒ q> Wq" Wd S(   s7  
        Update the document with the information from this data source.

        This is alternative to index, and it is used when indexing with package
        data taken from a custom Packages file.

        document  is the document to update
        pkg       is the Deb822 object for this package
        N(   R`   RO   R"   RN   R/   (   R   R(   RT   R6   R.   R)   R*   R[   (    (    s2   /usr/share/apt-xapian-index/plugins/app-install.pyt   indexDeb822Ì   s
    
N(
   R0   R1   R   RL   RU   R   R\   R_   R/   Ra   (    (    (    s2   /usr/share/apt-xapian-index/plugins/app-install.pyR2   A   s   		5			c         K   sI   t  j j t ƒ s d St s6 | r2 | j d ƒ n  d St d |  d | ƒ S(   s.   
    Create and return the plugin object.
    sE   please install python-xdg if you want to index app-install-data filesR   R   N(   RA   R9   t   isdirRC   R   t   HAS_XDGR$   R2   (   R   R   RZ   (    (    s2   /usr/share/apt-xapian-index/plugins/app-install.pyRU   Ü   s     (    (   t   xdg.DesktopEntryR    t   xdgR   t   TrueRc   t   ImportErrorR.   t   Falset   axi.indexert   axiR   RA   t   os.pathRC   R   t   objectR2   R   RU   (    (    (    s2   /usr/share/apt-xapian-index/plugins/app-install.pyt   <module>   s   

3›