[Python Wpg] Gnome background switcher!!!

Scott Balneaves sbalneav at legalaid.mb.ca
Mon Jun 5 17:06:57 CEST 2006


Hello PUGlings!

So, if you're like me (and heaven help you if you are) you've probably
managed to acquire a huge number of background wallpapers in your life
online.  However, one of gnome's minor deficiencies is that the
wallpaper switcher doesn't have a feature where it cycles through a list
of wallpapers.  What to do, what to do... 

One could write a python program!!!

This is an amalgam of about 3 different programs I found on the
interweb.  The problem with two of them was that they didn't actually
exit after your gnome session quit (they'd happily continue on in the
background.  For most people, it wouldn't be a problem, since when they
log out, they probably shut down.  On LTSP servers, however, this isn't
the case), and the other progam wasn't a background swither at all, but
it DID have the gnome/gtk framework for calling gtk.main, and a timeout
function.

So, slapping all 3 together, and a bit of glue, and voila!

To use:

Go to preferences->sessions, and in the "Startup Programs" tab, you'll
want something like:

/path/to/background.py -f /path/to/backgrounds

If you want to adjust the interval (currently, it switches the wallpaper
every 10 minutes), you can add a "-i interval_seconds" to the command
line as well.

The program automatically scans a directory tree, so if you have your
wallpapers organized into folders, thats no problem.

And now, for your viewing pleasure, background.py:


#!/usr/bin/python

import gtk
import gobject
import gconf
import os
import threading
import signal
import sys
import random
from optparse import OptionParser

class GtkBackgroundSwitcher (threading.Thread):
    """
    A gnome-aware background switcher.  Properly exits when your gnome
    session goes away.  Sets your picture filename in the gconf database
    appropriately.
    """

    def __init__(self, filelist, interval):
        threading.Thread.__init__ (self)

        self.index = 0
        self.filelist = filelist
        self.filelistlen = len(self.filelist)
        self.interval = interval * 1000         # Gnome intervals are in ms

        self.ready = threading.Condition()
        self.client = gconf.client_get_default()

    def run(self):
        """
        Logan's?
        """
        gobject.timeout_add(self.interval, self.change_background)
        gtk.main()

    def change_background(self):
        """
        Change the gconf background key to the next image file in the list.
        """
        self.ready.acquire ()

        if self.index < (self.filelistlen - 1):
            self.index += 1
        else:
            self.index = 0
        self.client.set_string('/desktop/gnome/background/picture_filename',
                               self.filelist[self.index])
        self.ready.release()
        return 1 

#
# Signal handler
#

def signal_handler (*args):
    print "SIGNAL:", args
    sys.exit()

#
# walk_tree builds a list of files recursively in a directory structure.
#

def walk_tree(pathvar, d):
    for name in os.listdir(d):
        path = os.path.join(d, name)
        if os.path.isdir(path):
            walk_tree(pathvar, path)
        else:
            pathvar.append(d + os.sep + name)

if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option("-f", "--folder", dest="folder",
                      help="use wallpapers from FOLDER", metavar="FOLDER")
    parser.add_option("-i", "--interval", type="int",
                      dest="interval", default=600,
                      help="time interval in seconds", metavar="INTERVAL")
    (options, args) = parser.parse_args()

    if options.folder is None:
        raise ValueError, "No valid folder specified"
    if not os.path.isdir(options.folder):
        raise ValueError, "The folder specified is not valid"

    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGSEGV, signal_handler)
    
    filelist = []
    walk_tree(filelist, options.folder)

    if not filelist:
        raise ValueError, "Folder does not contain any image files"

    random.shuffle(filelist)
    
    background = GtkBackgroundSwitcher(filelist, options.interval)
    background.start()


-- 
Scott L. Balneaves | "Looking beyond the embers of bridges glowing behind us
Systems Department |  To a glimpse of how green it was on the other side..."
Legal Aid Manitoba |    -- Pink Floyd "High Hopes"


More information about the Winnipeg mailing list