#!/usr/bin/python import os import types import random import sys import dcopext import kdecore from qt import QString CONFIGURATION_FILENAME = "wallpaper.conf" RENAME_SCRIPT = "numericnames" DEFAULT_SRC = os.path.expanduser("~/Images/wallpaper") DEFAULT_OPERATION_NAME = "change" OPERATIONS = {} def loadOperations(pattern="operation"): functions = {} for name, function in [(name, f) for name, f in globals().iteritems() if type(f) == types.FunctionType and pattern in name]: shortName = name.replace(pattern, "") shortName = shortName.lower() functions[shortName] = function return functions def operationHelp(args): """Prints this help""" global OPERATIONS ops = [(name, f.__doc__) for name, f in OPERATIONS.iteritems()] ops.sort() head, tail = os.path.split(__file__) print "USAGE: %s [operation] [arguments] [pattern]\n" % tail print "operation: the operation to execute (default: %s)" % DEFAULT_OPERATION_NAME print "pattern: if present, is used to select a subdirectory out of the source directory" print "arguments are operation dependant." for name, doc in ops: print "\n%s" % name if doc: for line in doc.split("\n"): print "\t%s" % line def operationChange(args): """Changes to a randomly selected wallpaper from the source folder no arguments""" src = findSource(args) changeWallpaper(randomWallpaper(getWallpapers(src))) def operationDelete(args): """Changes to a randomly selected wallpaper from the source folder Deletes the previous wallpaper no arguments""" src = findSource(args) success, oldwall = changeWallpaper(randomWallpaper(getWallpapers(src))) if (success): print "removing", str(oldwall) os.remove(str(oldwall)) def operationRename(args): """Runs the configured renaming scheme script in the source folder no arguments""" src = findSource(args) os.chdir(src) os.execlp(RENAME_SCRIPT) def operationLatest(args): """Changes to a randomly selected wallpaper out of the N latest from the source folder By default, using this without any arguments changes to the latest wallpaper arguments N: number of latest wallpapers to choose from (default 1)""" nb = 1 if args and args[0].isdigit(): nb = int(args.pop(0)) src = findSource(args) changeWallpaper(randomWallpaper(newestWallpapers(getWallpapers(src), nb))) def loadConfiguration(): global DEFAULT_OPERATION_NAME, DEFAULT_SRC head, tail = os.path.split(__file__) filename = head + os.path.sep + CONFIGURATION_FILENAME if (os.path.exists(filename)): execfile(filename, globals()) else: print "Configuration file missing!" print "Creating from application built-in defaults." print "DEFAULT_SRC =", DEFAULT_SRC print "DEFAULT_OPERATION_NAME =", DEFAULT_OPERATION_NAME print "RENAME_SCRIPT =", RENAME_SCRIPT conf = open(filename, "w") conf.write("DEFAULT_SRC = '%s'\n" % DEFAULT_SRC) conf.write("DEFAULT_OPERATION_NAME = '%s'\n" % DEFAULT_OPERATION_NAME) conf.write("RENAME_SCRIPT = '%s'\n" % RENAME_SCRIPT) conf.flush() conf.close() operationHelp([]) def parseArgs(): """Parse the application arguments RETURN: operation to be executed, the rest of the arguments""" operation = None src = DEFAULT_SRC arguments = sys.argv[1:] if arguments: operationName = arguments[0] try: operation = OPERATIONS[operationName] arguments.pop(0) except KeyError, e: # operation name invalid, use default param operation = OPERATIONS[DEFAULT_OPERATION_NAME] else: operation = OPERATIONS[DEFAULT_OPERATION_NAME] return operation, arguments def findSource(arguments): """Given a list of arguments corresponding to a search phrase, returns the subdirectory matching the arguments""" src = DEFAULT_SRC # If there's any arguments left, it's a search path if arguments: target = " ".join(arguments) target = target.upper() content = os.listdir(src) for d in content: f = src + os.sep + d if os.path.isdir(f) and target in d.upper(): src += os.sep + d break; return src def getWallpapers(src): """Given a source directory, recursively list all non-hidden files and returns it""" wallies = [] content = os.listdir(src) for f in content: if f[0] == ".": continue f = src + os.sep + f if os.path.isfile(f): wallies.append(f) elif os.path.isdir(f): wallies.extend(getWallpapers(f)) return wallies def randomWallpaper(wallies): """Select a random wallpaper from a list""" return wallies[int(random.random() * len(wallies))] def newestWallpapers(wallies, N=1): """Select the top N latest wallpaper from a list (using modification time""" wallies = [(os.path.getmtime(wall), wall) for wall in wallies] wallies.sort() wallies.reverse() return [wall for ctime, wall in wallies[:N]] def changeWallpaper(wall, nb_desk=2): """Change the current wallpaper to the one specified RETURN: Success, old wallpaper""" success, oldwall = kdesktop.KBackgroundIface.currentWallpaper(0) for i in range(nb_desk): kdesktop.KBackgroundIface.setWallpaper(i, wall, 6) return success, oldwall # LOAD OPERATIONS FROM DEFINED FUNCTIONS OPERATIONS = loadOperations() # LOAD CONFIGURATION loadConfiguration() # PARSE THE ARGUMENTS, EXTRACT THE OPERATION operation, args = parseArgs() # STANDARD KDE COMMAND LINE APP STARTUP, TO BE ABLE TO USE DCOP INTERNALLY aboutdata = kdecore.KAboutData("dcop_test", "dcop_test", "0.1", "foo", kdecore.KAboutData.License_GPL, "Copyleft") kdecore.KCmdLineArgs.init([sys.argv[0]], aboutdata) app = kdecore.KApplication(True, False) dcop = app.dcopClient() kdesktop = dcopext.DCOPApp("kdesktop", dcop) # RUN THE OPERATION operation(args)