With the desktop on Ubuntu Linux (indeed, with any Gnome desktop) it’s
possible to add emblems to any file. An emblem is a little reminder, a
small icon that appears on top of the file. Think of it as a tag,
perhaps, in this Web 2.0 world.
Anyway, I needed a way of establishing which files had a
particular emblem from a script. So here’s such a script. Call as follows:
aquarius@giles:~$ python findemblem.py money
/home/aquarius/Sam presents
/home/aquarius/Work/quotation.xml
The actual script is:
import re, glob, sys, urllib, os
from xml.dom import minidom
def grep(pattern, files):
search = re.compile(pattern).search
matches = []
for file in files:
for index, line in enumerate(open(file)):
if search(line):
matches.append(file)
break
return matches
def findEmblemedFiles(emblem):
metafilesWithKeyword = grep('<keyword ',
glob.glob(os.path.expanduser('~/.nautilus/metafiles/*')))
retfiles = []
for dirxmlfile in metafilesWithKeyword:
dom = minidom.parse(dirxmlfile)
filenodes = [x.parentNode.getAttribute('name')
for x in dom.getElementsByTagName('keyword')
if x.getAttribute("name") == emblem]
files = dict([(x,'') for x in filenodes]).keys()
# turn /home/foo/.nautilus/metafiles/file:%2F%2F%2Fhome%2Ffoo%2FWork.xml
# into /home/foo/Work/
dirname = urllib.unquote(os.path.split(dirxmlfile)[1])[7:-4] + '/'
retfiles += [os.path.join(dirname,urllib.unquote(f)) for f in files]
return retfiles
if __name__ == "__main__":
print 'n'.join(findEmblemedFiles(sys.argv[1]))