This is

as days pass by, by Stuart Langridge

. Here I write about many things. In the past I wrote about other things but the past is past. I write code for people to play with, I write about my life on Twitter, and I write here.

On I wrote Find files with an emblem in Gnome, on the subject of Python, Howtos, and Linux.

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. The 'Sam presents' file has a 'money' emblem. 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]))
Petar Vasić

Thanks for the script.

Is there any way to add custom emblems in Gnome?

sil

Petar: there is. See http://www.gnome.org/learn/users-guide/latest/gosnautilus-203.html (Creating a New Emblem).

as days pass by » Blog Archive » My home backup system

[...] To get the list, we can use my findemblem.py script (and you thought I just wrote it for fun!). The final script, dobackup.sh, which actually does the work, just does the above steps, and looks like this: [...]

Sjon

There is a typo in your script; the last line misses a backslash:

print 'n'.join(findEmblemedFiles(sys.argv[1]))

should be

print '\n'.join(findEmblemedFiles(sys.argv[1]))

NawaMan

I am a developers of a project call Gnome-EasyBackup (https://launchpad.net/gnomeeasybackup) which make use of gnome emblems as a way to specified what files should be included/excluded in a backup (rdiff-backup as a backend). The program extract emblems information from `~/.nautilus/metafiles/*.xml` which work fine but after I upgraded to Ubuntu 9.10 which has Gnome 2.28 it stop running. After checking it, I found that there is no more `~/.nautilus/metafiles/*.xml`. It is an empty folder.

Where did it go? Please let me know if you are aware of?

This website belongs to Stuart Langridge. Contact details are available. Don't eat yellow snow. Valid HTML5, at least in theory, except for the bits that aren't because I'm that futuristic that I'm ahead of the spec, oh yes. HTML5 help from Bruce Lawson, among others. Fonts from the superb FontSquirrel. End.