| 1 |
""" |
| 2 |
Implementation of the XDG Recent File Storage Specification |
| 3 |
http://standards.freedesktop.org/recent-file-spec |
| 4 |
""" |
| 5 |
|
| 6 |
import xml.dom.minidom, xml.sax.saxutils |
| 7 |
import os, time, fcntl |
| 8 |
from xdg.Exceptions import ParsingError |
| 9 |
|
| 10 |
class RecentFiles: |
| 11 |
def __init__(self): |
| 12 |
self.RecentFiles = [] |
| 13 |
self.filename = "" |
| 14 |
|
| 15 |
def parse(self, filename=None): |
| 16 |
"""Parse a list of recently used files. |
| 17 |
|
| 18 |
filename defaults to ``~/.recently-used``. |
| 19 |
""" |
| 20 |
if not filename: |
| 21 |
filename = os.path.join(os.getenv("HOME"), ".recently-used") |
| 22 |
|
| 23 |
try: |
| 24 |
doc = xml.dom.minidom.parse(filename) |
| 25 |
except IOError: |
| 26 |
raise ParsingError('File not found', filename) |
| 27 |
except xml.parsers.expat.ExpatError: |
| 28 |
raise ParsingError('Not a valid .menu file', filename) |
| 29 |
|
| 30 |
self.filename = filename |
| 31 |
|
| 32 |
for child in doc.childNodes: |
| 33 |
if child.nodeType == xml.dom.Node.ELEMENT_NODE: |
| 34 |
if child.tagName == "RecentFiles": |
| 35 |
for recent in child.childNodes: |
| 36 |
if recent.nodeType == xml.dom.Node.ELEMENT_NODE: |
| 37 |
if recent.tagName == "RecentItem": |
| 38 |
self.__parseRecentItem(recent) |
| 39 |
|
| 40 |
self.sort() |
| 41 |
|
| 42 |
def __parseRecentItem(self, item): |
| 43 |
recent = RecentFile() |
| 44 |
self.RecentFiles.append(recent) |
| 45 |
|
| 46 |
for attribute in item.childNodes: |
| 47 |
if attribute.nodeType == xml.dom.Node.ELEMENT_NODE: |
| 48 |
if attribute.tagName == "URI": |
| 49 |
recent.URI = attribute.childNodes[0].nodeValue |
| 50 |
elif attribute.tagName == "Mime-Type": |
| 51 |
recent.MimeType = attribute.childNodes[0].nodeValue |
| 52 |
elif attribute.tagName == "Timestamp": |
| 53 |
recent.Timestamp = int(attribute.childNodes[0].nodeValue) |
| 54 |
elif attribute.tagName == "Private": |
| 55 |
recent.Prviate = True |
| 56 |
elif attribute.tagName == "Groups": |
| 57 |
|
| 58 |
for group in attribute.childNodes: |
| 59 |
if group.nodeType == xml.dom.Node.ELEMENT_NODE: |
| 60 |
if group.tagName == "Group": |
| 61 |
recent.Groups.append(group.childNodes[0].nodeValue) |
| 62 |
|
| 63 |
def write(self, filename=None): |
| 64 |
"""Write the list of recently used files to disk. |
| 65 |
|
| 66 |
If the instance is already associated with a file, filename can be |
| 67 |
omitted to save it there again. |
| 68 |
""" |
| 69 |
if not filename and not self.filename: |
| 70 |
raise ParsingError('File not found', filename) |
| 71 |
elif not filename: |
| 72 |
filename = self.filename |
| 73 |
|
| 74 |
with open(filename, "w") as f: |
| 75 |
fcntl.lockf(f, fcntl.LOCK_EX) |
| 76 |
f.write('<?xml version="1.0"?>\n') |
| 77 |
f.write("<RecentFiles>\n") |
| 78 |
|
| 79 |
for r in self.RecentFiles: |
| 80 |
f.write(" <RecentItem>\n") |
| 81 |
f.write(" <URI>%s</URI>\n" % xml.sax.saxutils.escape(r.URI)) |
| 82 |
f.write(" <Mime-Type>%s</Mime-Type>\n" % r.MimeType) |
| 83 |
f.write(" <Timestamp>%s</Timestamp>\n" % r.Timestamp) |
| 84 |
if r.Private == True: |
| 85 |
f.write(" <Private/>\n") |
| 86 |
if len(r.Groups) > 0: |
| 87 |
f.write(" <Groups>\n") |
| 88 |
for group in r.Groups: |
| 89 |
f.write(" <Group>%s</Group>\n" % group) |
| 90 |
f.write(" </Groups>\n") |
| 91 |
f.write(" </RecentItem>\n") |
| 92 |
|
| 93 |
f.write("</RecentFiles>\n") |
| 94 |
fcntl.lockf(f, fcntl.LOCK_UN) |
| 95 |
|
| 96 |
def getFiles(self, mimetypes=None, groups=None, limit=0): |
| 97 |
"""Get a list of recently used files. |
| 98 |
|
| 99 |
The parameters can be used to filter by mime types, by group, or to |
| 100 |
limit the number of items returned. By default, the entire list is |
| 101 |
returned, except for items marked private. |
| 102 |
""" |
| 103 |
tmp = [] |
| 104 |
i = 0 |
| 105 |
for item in self.RecentFiles: |
| 106 |
if groups: |
| 107 |
for group in groups: |
| 108 |
if group in item.Groups: |
| 109 |
tmp.append(item) |
| 110 |
i += 1 |
| 111 |
elif mimetypes: |
| 112 |
for mimetype in mimetypes: |
| 113 |
if mimetype == item.MimeType: |
| 114 |
tmp.append(item) |
| 115 |
i += 1 |
| 116 |
else: |
| 117 |
if item.Private == False: |
| 118 |
tmp.append(item) |
| 119 |
i += 1 |
| 120 |
if limit != 0 and i == limit: |
| 121 |
break |
| 122 |
|
| 123 |
return tmp |
| 124 |
|
| 125 |
def addFile(self, item, mimetype, groups=None, private=False): |
| 126 |
"""Add a recently used file. |
| 127 |
|
| 128 |
item should be the URI of the file, typically starting with ``file:///``. |
| 129 |
""" |
| 130 |
# check if entry already there |
| 131 |
if item in self.RecentFiles: |
| 132 |
index = self.RecentFiles.index(item) |
| 133 |
recent = self.RecentFiles[index] |
| 134 |
else: |
| 135 |
# delete if more then 500 files |
| 136 |
if len(self.RecentFiles) == 500: |
| 137 |
self.RecentFiles.pop() |
| 138 |
# add entry |
| 139 |
recent = RecentFile() |
| 140 |
self.RecentFiles.append(recent) |
| 141 |
|
| 142 |
recent.URI = item |
| 143 |
recent.MimeType = mimetype |
| 144 |
recent.Timestamp = int(time.time()) |
| 145 |
recent.Private = private |
| 146 |
if groups: |
| 147 |
recent.Groups = groups |
| 148 |
|
| 149 |
self.sort() |
| 150 |
|
| 151 |
def deleteFile(self, item): |
| 152 |
"""Remove a recently used file, by URI, from the list. |
| 153 |
""" |
| 154 |
if item in self.RecentFiles: |
| 155 |
self.RecentFiles.remove(item) |
| 156 |
|
| 157 |
def sort(self): |
| 158 |
self.RecentFiles.sort() |
| 159 |
self.RecentFiles.reverse() |
| 160 |
|
| 161 |
|
| 162 |
class RecentFile: |
| 163 |
def __init__(self): |
| 164 |
self.URI = "" |
| 165 |
self.MimeType = "" |
| 166 |
self.Timestamp = "" |
| 167 |
self.Private = False |
| 168 |
self.Groups = [] |
| 169 |
|
| 170 |
def __cmp__(self, other): |
| 171 |
return cmp(self.Timestamp, other.Timestamp) |
| 172 |
|
| 173 |
def __lt__ (self, other): |
| 174 |
return self.Timestamp < other.Timestamp |
| 175 |
|
| 176 |
def __eq__(self, other): |
| 177 |
return self.URI == str(other) |
| 178 |
|
| 179 |
def __str__(self): |
| 180 |
return self.URI |