3
Ѣ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   1/usr/lib/python3/dist-packages/xdg/RecentFiles.py__init__   s    zRecentFiles.__init__Nc             C   s   |st jjt jdd}ytjjj|}W n@ tk
rH   t	d|Y n$ tj
jjk
rj   t	d|Y nX || _x\|jD ]R}|jtjjjkrz|jdkrzx2|jD ](}|jtjjjkr|jdkr| j| qW qzW | j  dS )ziParse a list of recently used files.
        
        filename defaults to ``~/.recently-used``.
        HOMEz.recently-usedzFile not foundzNot a valid .menu filer   Z
RecentItemN)ospathjoingetenvxmldomZminidomparseIOErrorr   ZparsersZexpatZ
ExpatErrorr   
childNodesnodeTypeNodeELEMENT_NODEtagName_RecentFiles__parseRecentItemsort)r   r   docZchildrecentr   r   r   r      s"    

zRecentFiles.parsec             C   s   t  }| jj| x|jD ]}|jtjjjkr|j	dkrH|jd j
|_q|j	dkrb|jd j
|_q|j	dkrt|jd j
|_q|j	dkrd|_q|j	dkrx<|jD ]2}|jtjjjkr|j	dkr|jj|jd j
 qW qW d S )	NURIr   z	Mime-Type	TimestampPrivateTGroupsZGroup)
RecentFiler   appendr   r   r   r   r   r   r   Z	nodeValuer   MimeTypeintr   ZPrviater   )r   itemr   Z	attributegroupr   r   r   Z__parseRecentItem*   s"    





zRecentFiles.__parseRecentItemc             C   s&  | r| j  rtd|n
|s$| j }t|d}tj|tj |jd |jd x| jD ]}|jd |jdtj	j
j|j  |jd|j  |jd|j  |jd	kr|jd
 t|jdkr|jd x|jD ]}|jd|  qW |jd |jd qXW |jd tj|tj |j  dS )zWrite 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.
        zFile not found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lockfZLOCK_EXwriter   r   ZsaxZsaxutilsescaper   r"   r   r   lenr   ZLOCK_UNclose)r   r   frr%   r   r   r   r*   ?   s0    








zRecentFiles.writer   c       	      C   s   g }d}x| j D ]}|rDxv|D ] }||jkr|j| |d7 }qW nL|rtxF|D ] }||jkrN|j| |d7 }qNW n|jdkr|j| |d7 }|dkr||krP qW |S )zGet 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   Z	mimetypesgroupslimitZtmpir$   r%   mimetyper   r   r   getFilesa   s&    







zRecentFiles.getFilesFc             C   s   || j kr"| j j|}| j | }n*t| j dkr:| j j  t }| j j| ||_||_tt	j	 |_
||_|rv||_| j  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$   r4   r1   Zprivater6   r   r   r   r   addFile~   s    

zRecentFiles.addFilec             C   s   || j kr| j j| 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  | j j  d S )N)r   r   reverse)r   r   r   r   r      s    
zRecentFiles.sort)N)N)NNr   )NF)__name__
__module____qualname__r	   r   r   r*   r5   r9   r;   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 )N)Zcmpr   )r   otherr   r   r   __cmp__   s    zRecentFile.__cmp__c             C   s   | j |j k S )N)r   )r   r@   r   r   r   __lt__   s    zRecentFile.__lt__c             C   s   | j t|kS )N)r   str)r   r@   r   r   r   __eq__   s    zRecentFile.__eq__c             C   s   | j S )N)r   )r   r   r   r   __str__   s    zRecentFile.__str__N)r=   r>   r?   r	   rA   rB   rD   rE   r   r   r   r   r       s
   r    )__doc__Zxml.dom.minidomr   Zxml.sax.saxutilsr   r8   r(   Zxdg.Exceptionsr   r   r    r   r   r   r   <module>   s    