]> code.delx.au - bg-scripts/blobdiff - wallchanger.py
Automated merge with ssh://hg@kagami.tsukasa.net.au/bg_scripts/
[bg-scripts] / wallchanger.py
index 5982621bff25d95a22003b2898501e68677a35dd..6c2ba004c3b14ad0a69b5909c62b7242c899c6ce 100755 (executable)
@@ -4,7 +4,7 @@
 # Licensed for distribution under the GPL version 2, check COPYING for details
 # This is a cross platform/cross window manager way to change your wallpaper
 
-import commands, sys, os, os.path, subprocess, time
+import commands, sys, os, os.path, time
 import logging
 
 __all__ = ("init", "set_image")
@@ -31,7 +31,7 @@ def init(*args, **kwargs):
        else:
                if os.uname()[0] == 'Darwin':
                        # Try to detect if the X11 server is running on OSX
-                       if commands.getstatusoutput("ps ax -o command|grep -q '/.*X11 .* %s'" % os.environ['DISPLAY'])[0] != 0:
+                       if commands.getstatusoutput("ps ax -o command|grep -q '^/.*X11 .* %s'" % os.environ['DISPLAY'])[0] != 0:
                                # X11 is not running for this display
                                return
 
@@ -59,6 +59,22 @@ class BaseChanger(object):
                self.permanent = permanent
                self.convert = convert
 
+               try:
+                       import subprocess
+               except ImportError:
+                       self._runProgram = self._runProgram_command
+               else:
+                       self._runProgram = self._runProgram_subprocess
+
+       def _runProgram_subprocess(self, cmd):
+               import subprocess
+               return subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait()
+
+       # A simple implementation of subprocess for python2.4
+       def _runProgram_command(self, cmd):
+               """Runs a program given in cmd"""
+               return os.spawnvp(os.P_WAIT, cmd[0], cmd)
+
        def set_image(self, filename):
                raise NotImplementedError()
 
@@ -81,7 +97,7 @@ class WMakerChanger(BaseChanger):
                output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time())
                cmd = ["convert", '-resize', '1280', '-gravity', 'Center', '-crop', '1280x800+0+0', file, output_name]
                logging.debug("""Convert command: '"%s"'""", '" "'.join(cmd))
-               return output_name, subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait()
+               return output_name, self._runProgram(cmd)
 
        def set_image(self, file):
                if self.convert:
@@ -100,7 +116,7 @@ class WMakerChanger(BaseChanger):
                        cmd += ["-u"] # update the wmaker database
                cmd += [file]
                logging.debug('''WMaker bgset command: "'%s'"''', "' '".join(cmd))
-               return not subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait()
+               return not self._runProgram(cmd)
 
 class OSXChanger(BaseChanger):
        name = "Mac OS X"
@@ -167,7 +183,7 @@ class GnomeChanger(BaseChanger):
        def set_image(self, file):
                cmd = ['gconftool-2', '--type', 'string', '--set', '/desktop/gnome/background/picture_filename', file]
                logging.debug(cmd)
-               return not subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait()
+               return not self._runProgram(cmd)
 
 class KDEChanger(BaseChanger):
        name = "KDE"
@@ -183,7 +199,7 @@ class KDEChanger(BaseChanger):
                cmds.append(['dcop', 'kdesktop', 'KBackgroundIface', 'configure'])
                for cmd in cmds:
                        logging.debug(cmd)
-                       if subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait() != 0:
+                       if self._runProgram(cmd) != 0:
                                return False
 
                return True