o
    Ñ¢UPù  ã                   @   sX   d 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 G dd„ dƒZ	G dd„ dƒZ
dS )z{
Implementation of the XDG Recent File Storage Specification Version 0.2
http://standards.freedesktop.org/recent-file-spec
é    N)ÚParsingErrorc                   @   sT   e Zd Zdd„ Zddd„Zdd„ Zddd	„Zddd„Zddd„Zdd„ Z	dd„ Z
dS )ÚRecentFilesc                 C   s   g | _ d| _d S )NÚ )r   Úfilename©Úself© r   úA/var/www/html/env/lib/python3.10/site-packages/xdg/RecentFiles.pyÚ__init__   s   
zRecentFiles.__init__Nc                 C   sÈ   |st j t  d¡d¡}z	tjj |¡}W n ty!   t	d|ƒ‚ tj
jjy/   t	d|ƒ‚w || _|jD ]'}|jtjjjkr]|jdkr]|jD ]}|jtjjjkr\|jdkr\|  |¡ qHq6|  ¡  dS )ziParse a list of recently used files.
        
        filename defaults to ``~/.recently-used``.
        ÚHOMEz.recently-usedúFile not foundzNot a valid .menu filer   Ú
RecentItemN)ÚosÚpathÚjoinÚgetenvÚxmlÚdomÚminidomÚparseÚIOErrorr   ÚparsersÚexpatÚ
ExpatErrorr   Ú
childNodesÚnodeTypeÚNodeÚELEMENT_NODEÚtagNameÚ_RecentFiles__parseRecentItemÚsort)r   r   ÚdocÚchildÚrecentr   r   r	   r      s&   

ÿ




€zRecentFiles.parsec                 C   sÚ   t ƒ }| j |¡ |jD ]^}|jtjjjkrj|j	dkr#|jd j
|_q|j	dkr0|jd j
|_q|j	dkr?t|jd j
ƒ|_q|j	dkrHd|_q|j	dkrj|jD ]}|jtjjjkri|j	dkri|j |jd j
¡ qPqd S )	NÚURIr   z	Mime-TypeÚ	TimestampÚPrivateTÚGroupsÚGroup)Ú
RecentFiler   Úappendr   r   r   r   r   r   r   Ú	nodeValuer$   ÚMimeTypeÚintr%   ÚPrviater'   )r   Úitemr#   Ú	attributeÚgroupr   r   r	   Ú__parseRecentItem*   s&   







€ñzRecentFiles.__parseRecentItemc                 C   s  |s
| j s
td|ƒ‚|s| j }t|dƒ}t |tj¡ | d¡ | d¡ | jD ]Q}| d¡ | dtj	j
 |j¡ ¡ | d|j ¡ | d|j ¡ |jd	krV| d
¡ t|jƒdkrt| d¡ |jD ]	}| d| ¡ qe| d¡ | d¡ q(| d¡ t |tj¡ | ¡  dS )z³Write the list of recently used files to disk.
        
        If the instance is already associated with a file, filename can be
        omitted to save it there again.
        r   Úwz<?xml version="1.0"?>
z<RecentFiles>
z  <RecentItem>
z    <URI>%s</URI>
z    <Mime-Type>%s</Mime-Type>
z    <Timestamp>%s</Timestamp>
Tz    <Private/>
r   z    <Groups>
z      <Group>%s</Group>
z    </Groups>
z  </RecentItem>
z</RecentFiles>
N)r   r   ÚopenÚfcntlÚlockfÚLOCK_EXÚwriter   r   ÚsaxÚsaxutilsÚescaper$   r,   r%   r&   Úlenr'   ÚLOCK_UNÚclose)r   r   ÚfÚrr1   r   r   r	   r8   ?   s0   












zRecentFiles.writer   c           	      C   s¢   g }d}| j D ]G}|r|D ]}||jv r| |¡ |d7 }qn$|r5|D ]}||jkr3| |¡ |d7 }q#n|jdkrC| |¡ |d7 }|dkrN||krN |S q|S )zþGet a list of recently used files.
        
        The parameters can be used to filter by mime types, by group, or to
        limit the number of items returned. By default, the entire list is
        returned, except for items marked private.
        r   é   F)r   r'   r*   r,   r&   )	r   Ú	mimetypesÚgroupsÚlimitÚtmpÚir/   r1   Úmimetyper   r   r	   ÚgetFilesa   s2   


€ý

€ý

€ zRecentFiles.getFilesFc                 C   s‚   || j v r| j  |¡}| j | }nt| j ƒdkr| j  ¡  tƒ }| j  |¡ ||_||_tt	 	¡ ƒ|_
||_|r;||_|  ¡  dS )z}Add a recently used file.
        
        item should be the URI of the file, typically starting with ``file:///``.
        iô  N)r   Úindexr<   Úpopr)   r*   r$   r,   r-   Útimer%   r&   r'   r    )r   r/   rG   rC   ÚprivaterI   r#   r   r   r	   ÚaddFile~   s   

zRecentFiles.addFilec                 C   s   || j v r| j  |¡ dS dS )z<Remove a recently used file, by URI, from the list.
        N)r   Úremove)r   r/   r   r   r	   Ú
deleteFile˜   s   
ÿzRecentFiles.deleteFilec                 C   s   | j  ¡  | j  ¡  d S ©N)r   r    Úreverser   r   r   r	   r    ž   s   
zRecentFiles.sortrP   )NNr   )NF)Ú__name__Ú
__module__Ú__qualname__r
   r   r   r8   rH   rM   rO   r    r   r   r   r	   r   
   s    


"
r   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )r)   c                 C   s"   d| _ d| _d| _d| _g | _d S )Nr   F)r$   r,   r%   r&   r'   r   r   r   r	   r
   ¤   s
   
zRecentFile.__init__c                 C   s   t | j|jƒS rP   )Úcmpr%   ©r   Úotherr   r   r	   Ú__cmp__«   ó   zRecentFile.__cmp__c                 C   s   | j |j k S rP   )r%   rV   r   r   r	   Ú__lt__®   s   zRecentFile.__lt__c                 C   s   | j t|ƒkS rP   )r$   ÚstrrV   r   r   r	   Ú__eq__±   rY   zRecentFile.__eq__c                 C   s   | j S rP   )r$   r   r   r   r	   Ú__str__´   s   zRecentFile.__str__N)rR   rS   rT   r
   rX   rZ   r\   r]   r   r   r   r	   r)   £   s    r)   )Ú__doc__Úxml.dom.minidomr   Úxml.sax.saxutilsr   rK   r5   Úxdg.Exceptionsr   r   r)   r   r   r   r	   Ú<module>   s     