24 October 2010

Importing text files into Tomboy

Thanks to Scott Carpenter's post, I managed to write a Python script importing folder files into Tomboy.

The script creates note for each text file(".txt") using Tomboy DBus API. txt2tomboy.py:


import dbus, gobject, dbus.glib
import os

# get the d-bus session bus
bus = dbus.SessionBus()
# access the tomboy d-bus object
obj = bus.get_object("org.gnome.Tomboy", "/org/gnome/Tomboy/RemoteControl")
# access the tomboy remote control interface
tomboy = dbus.Interface(obj, "org.gnome.Tomboy.RemoteControl")

extAllowed = ['.txt']
srcDir = 'path/to/notes/'
path = os.path.expanduser(srcDir)

dirlist = os.listdir(path)
dirlist.sort()

for root, dirs, files in os.walk('/home/ruslan/now/'):
    for filename in files: 
        #print join(root, filename)
        if os.path.splitext(filename)[1] in extAllowed:
            print os.path.join(root, filename)
            f = open(os.path.join(root, filename))
            # d-bus complains if string params aren't valid UTF-8
            title = filename;

   # reset to start of file and read whole file
   f.seek(0)
   s = f.read();

   # creating named notes seems to prevent notes
   #   from showing up as "New Note NNN"
   note = tomboy.CreateNamedNote(title)

   # TODO: remove DisplayNote()/HideNote() in future versions
   # where the bug will hopefully be fixed.
   # Because of bug in Tomboy 1.8.0(at least I've noticed it in this version)
   # we have to workaround displaying the note before SetNoteContents :/
   tomboy.DisplayNote(note)
   tomboy.SetNoteContents(note, title + "\n\n" + s)
   tomboy.HideNote(note)

That's it. I'd just share what I've learnt

P.S. This is a start point in learning Python for me :)

UPDATE 25.10.2010 09:46 UTC+5

More flexible version:

#!/usr/bin/python
import dbus, gobject, dbus.glib
import os, sys, getopt

def usage():
    print __file__ + " [options]"
    print "Options:"
    print "-h --help        Display this help"
    print "-p --path        Path to source direcrory with text files"

def main(argv):
    path = './'                # source path 
    extAllowed = ['.txt']

    try:
        opts, args = getopt.getopt(argv[1:], "p:h", ("path="))

    except getopt.GetoptError:
        usage()
        sys.exit(2)

    for o, a in opts:
        if o in ('-h', '--help'):
            usage();
            sys.exit();

        elif o in ("-p", "--path"):
            path = os.path.expanduser(a)

    # get the d-bus session bus
    bus = dbus.SessionBus()
    # access the tomboy d-bus object
    obj = bus.get_object("org.gnome.Tomboy", "/org/gnome/Tomboy/RemoteControl")
    # access the tomboy remote control interface
    tomboy = dbus.Interface(obj, "org.gnome.Tomboy.RemoteControl")

    dirlist = os.listdir(path)
    dirlist.sort()

    for root, dirs, files in os.walk(path):
        for filename in files: 
            #print join(root, filename)
            if os.path.splitext(filename)[1] in extAllowed:
                print os.path.join(root, filename)
                f = open(os.path.join(root, filename))
                # d-bus complains if string params aren't valid UTF-8
                title = filename;

                # reset to start of file and read whole file
                f.seek(0)
                s = f.read();

                # creating named notes seems to prevent notes
                #   from showing up as "New Note NNN"
                note = tomboy.CreateNamedNote(title)
                # TODO: remove DisplayNote()/HideNote() in future versions
                # where the bug will hopefully be fixed.
                # Because of bug in Tomboy 1.8.0(at least I've noticed it in this version)
                # we have to workaround displaying the note before SetNoteContents :/
                tomboy.DisplayNote(note)
                tomboy.SetNoteContents(note, title + "\n\n" + s)
                tomboy.HideNote(note)

if __name__ == "__main__":
    main(sys.argv)

UPDATE Tue Jul 10 10:53:17 MSK 2012

The new Tomboy DBus API has a bug: SetNoteContents() doesn't work until the note isn't displayed. I've updated scripts with the following workaround:

tomboy.DisplayNote(note)
tomboy.SetNoteContents(note, title + "\n\n" + s)
tomboy.HideNote(note)
:/