X-Git-Url: https://code.delx.au/bg-scripts/blobdiff_plain/c3a633c99afac3a08701da1dd945e59973096a2b..fd176b77f01e67307c9899735ecac01939b2aebf:/wallchanger.py diff --git a/wallchanger.py b/wallchanger.py index 2bbd6f9..8bfb69b 100755 --- a/wallchanger.py +++ b/wallchanger.py @@ -8,9 +8,9 @@ import commands, sys, os, os.path, time import logging try: - import PIL, PIL.Image + import PIL, PIL.Image except ImportError: - PIL = None + PIL = None __all__ = ("init", "set_image") @@ -18,275 +18,275 @@ __all__ = ("init", "set_image") changers = [] def set_image(filename): - logging.info("Setting image: %s", filename) - for changer in changers: - if not changer.set_image(filename): - logging.warning("Failed to set background: wallchanger.set_image(%s), changer=%s", filename, changer) + logging.info("Setting image: %s", filename) + for changer in changers: + if not changer.set_image(filename): + logging.warning("Failed to set background: wallchanger.set_image(%s), changer=%s", filename, changer) def check_cmd(cmd): - return commands.getstatusoutput(cmd)[0] == 0 + return commands.getstatusoutput(cmd)[0] == 0 def init(*args, **kwargs): - """Desktop Changer factory""" + """Desktop Changer factory""" - classes = [] + classes = [] - if sys.platform == "win32": - classes.append(WIN32Changer) - return + if sys.platform == "win32": + classes.append(WIN32Changer) + return - logging.debug("Testing for OSX (NonX11)") - if check_cmd("ps ax -o command -c|grep -q WindowServer"): - classes.append(OSXChanger) + logging.debug("Testing for OSX (NonX11)") + if check_cmd("ps ax -o command -c|grep -q WindowServer"): + classes.append(OSXChanger) - if 'DISPLAY' not in os.environ or os.environ['DISPLAY'].startswith('/tmp/launch'): - # X11 is not running - return - else: - if os.uname()[0] == 'Darwin': - # Try to detect if the X11 server is running on OSX - if check_cmd("ps ax -o command|grep -q '^/.*X11 .* %s'" % os.environ['DISPLAY']): - # X11 is not running for this display - return + if 'DISPLAY' not in os.environ or os.environ['DISPLAY'].startswith('/tmp/launch'): + # X11 is not running + return + else: + if os.uname()[0] == 'Darwin': + # Try to detect if the X11 server is running on OSX + if check_cmd("ps ax -o command|grep -q '^/.*X11 .* %s'" % os.environ['DISPLAY']): + # X11 is not running for this display + return - logging.debug("Testing for XFCE4") - if check_cmd("xwininfo -name 'xfce4-session'"): - classes.append(Xfce4Changer) + logging.debug("Testing for XFCE4") + if check_cmd("xwininfo -name 'xfce4-session'"): + classes.append(Xfce4Changer) - logging.debug("Testing for Gnome") - if check_cmd("xwininfo -name 'gnome-settings-daemon'"): - if check_cmd("gsettings get org.gnome.desktop.background picture-uri"): - classes.append(Gnome3Changer) - else: - classes.append(Gnome2Changer) + logging.debug("Testing for Gnome") + if check_cmd("xwininfo -name 'gnome-settings-daemon'"): + if check_cmd("gsettings get org.gnome.desktop.background picture-uri"): + classes.append(Gnome3Changer) + else: + classes.append(Gnome2Changer) - logging.debug("Testing for xloadimage") - if check_cmd("which xloadimage"): - classes.append(XLoadImageChanger) + logging.debug("Testing for xloadimage") + if check_cmd("which xloadimage"): + classes.append(XLoadImageChanger) - if len(classes) == 0: - raise Exception("Unknown window manager") + if len(classes) == 0: + raise Exception("Unknown window manager") - for klass in classes: - changers.append(klass(*args, **kwargs)) + for klass in classes: + changers.append(klass(*args, **kwargs)) class BaseChanger(object): - name = "undefined" - def __init__(self, background_color='black', convert=False): - logging.info('Determined the window manager is "%s"', self.name) - self.background_color = background_color - self.convert = convert - - try: - def _exec_cmd(self, cmd): - import subprocess - return subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait() - - except ImportError: - # A simple implementation of subprocess for python2.4 - def _exec_cmd(self, cmd): - """Runs a program given in cmd""" - return os.spawnvp(os.P_WAIT, cmd[0], cmd) - - def set_image(self, filename): - raise NotImplementedError() - - def convert_image_format(self, filename, format='BMP', allowAlpha=False, extension='.bmp'): - """Convert the image to another format, and store it in a local place""" - if not os.path.exists(filename): - logger.warn('The input file "%s" does not exist, so it will not be converted', filename) - return filename, False - if PIL is None: - logger.warn('PIL could not be found, not converting image format') - return filename, False - - self.remove_old_image_cache() - output_name = os.path.join(self._ConvertedWallpaperLocation, '%s%s' % (time.time(), extension)) - img = PIL.Image.open(filename) - - # Remove the alpha channel if the user doens't want it - if not allowAlpha and img.mode == 'RGBA': - img = img.convert('RGB') - img.save(output_name, format) - - return output_name, True + name = "undefined" + def __init__(self, background_color='black', convert=False): + logging.info('Determined the window manager is "%s"', self.name) + self.background_color = background_color + self.convert = convert + + try: + def _exec_cmd(self, cmd): + import subprocess + return subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait() + + except ImportError: + # A simple implementation of subprocess for python2.4 + def _exec_cmd(self, cmd): + """Runs a program given in cmd""" + return os.spawnvp(os.P_WAIT, cmd[0], cmd) + + def set_image(self, filename): + raise NotImplementedError() + + def convert_image_format(self, filename, format='BMP', allowAlpha=False, extension='.bmp'): + """Convert the image to another format, and store it in a local place""" + if not os.path.exists(filename): + logger.warn('The input file "%s" does not exist, so it will not be converted', filename) + return filename, False + if PIL is None: + logger.warn('PIL could not be found, not converting image format') + return filename, False + + self.remove_old_image_cache() + output_name = os.path.join(self._ConvertedWallpaperLocation, '%s%s' % (time.time(), extension)) + img = PIL.Image.open(filename) + + # Remove the alpha channel if the user doens't want it + if not allowAlpha and img.mode == 'RGBA': + img = img.convert('RGB') + img.save(output_name, format) + + return output_name, True class XLoadImageChanger(BaseChanger): - name = "xloadimage" - _ConvertedWallpaperLocation = '/tmp/wallpapers_xloadimage/' - def remove_old_image_cache(self): - """Cleans up any old temp images""" - if not os.path.isdir(self._ConvertedWallpaperLocation): - os.mkdir(self._ConvertedWallpaperLocation) - for fullpath, filenames, dirnames in os.walk(self._ConvertedWallpaperLocation, topdown=False): - for filename in filenames: - os.unlink(os.path.join(fullpath, filename)) - for dirname in dirnames: - os.unlink(os.path.join(fullpath, dirname)) - - def convert_image_format(self, filename): - """Convert the image to a png, and store it in a local place""" - self.remove_old_image_cache() - output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time()) - cmd = ["convert", filename, output_name] - logging.debug("""Convert command: '"%s"'""", '" "'.join(cmd)) - return output_name, self._exec_cmd(cmd) - - def set_image(self, filename): - if self.convert: - filename, convert_status = self.convert_image_format(filename) - if convert_status: - logging.debug('Convert failed') - cmd = [ - "xloadimage", - "-onroot", - "-fullscreen", - "-border", "black", - filename, - ] - logging.debug('''WMaker bgset command: "'%s'"''', "' '".join(cmd)) - return not self._exec_cmd(cmd) + name = "xloadimage" + _ConvertedWallpaperLocation = '/tmp/wallpapers_xloadimage/' + def remove_old_image_cache(self): + """Cleans up any old temp images""" + if not os.path.isdir(self._ConvertedWallpaperLocation): + os.mkdir(self._ConvertedWallpaperLocation) + for fullpath, filenames, dirnames in os.walk(self._ConvertedWallpaperLocation, topdown=False): + for filename in filenames: + os.unlink(os.path.join(fullpath, filename)) + for dirname in dirnames: + os.unlink(os.path.join(fullpath, dirname)) + + def convert_image_format(self, filename): + """Convert the image to a png, and store it in a local place""" + self.remove_old_image_cache() + output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time()) + cmd = ["convert", filename, output_name] + logging.debug("""Convert command: '"%s"'""", '" "'.join(cmd)) + return output_name, self._exec_cmd(cmd) + + def set_image(self, filename): + if self.convert: + filename, convert_status = self.convert_image_format(filename) + if convert_status: + logging.debug('Convert failed') + cmd = [ + "xloadimage", + "-onroot", + "-fullscreen", + "-border", "black", + filename, + ] + logging.debug('''WMaker bgset command: "'%s'"''', "' '".join(cmd)) + return not self._exec_cmd(cmd) class OSXChanger(BaseChanger): - name = "Mac OS X" - _ConvertedWallpaperLocation = '/tmp/wallpapers/' - _DesktopPlistLocation = os.path.expanduser('~/Library/Preferences/com.apple.desktop.plist') - - def __init__(self, *args, **kwargs): - BaseChanger.__init__(self, *args, **kwargs) - - def remove_old_image_cache(self): - """Cleans up any old temp images""" - if not os.path.isdir(self._ConvertedWallpaperLocation): - os.mkdir(self._ConvertedWallpaperLocation) - for fullpath, filenames, dirnames in os.walk(self._ConvertedWallpaperLocation, topdown=False): - for filename in filenames: - os.unlink(os.path.join(fullpath, filename)) - for dirname in dirnames: - os.unlink(os.path.join(fullpath, dirname)) - - def convert_image_format(self, filename): - """Convert the image to a png, and store it in a local place""" - self.remove_old_image_cache() - output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time()) - try: - return super(OSXChanger, self).convert_image_format(filename, format='PNG', extension='.png') - except ImportError: - logging.debug('Could not load PIL, going to try just copying the image') - import shutil - output_name = os.path.join(self._ConvertedWallpaperLocation, os.path.basename(filename)) - shutil.copyfile(filename, output_name) - return output_name, True - - def fix_desktop_plist(self): - """Removes the entry in the desktop plist file that specifies the wallpaper for each monitor""" - try: - import Foundation - desktop_plist = Foundation.NSMutableDictionary.dictionaryWithContentsOfFile_(self._DesktopPlistLocation) - # Remove all but the 'default' entry - for k in desktop_plist['Background'].keys(): - if k == 'default': - continue - desktop_plist['Background'].removeObjectForKey_(k) - # Store the plist again (Make sure we write it out atomically -- Don't want to break finder) - desktop_plist.writeToFile_atomically_(self._DesktopPlistLocation, True) - except ImportError: - logging.debug('Could not import the Foundation module, you may have problems with dual screens') - - def set_image(self, filename): - self.fix_desktop_plist() - if self.convert: - filename, ret = self.convert_image_format(filename) - if not ret: - logging.debug("Convert failed") - return False - cmd = """osascript -e 'tell application "finder" to set desktop picture to posix file "%s"'""" % filename - logging.debug(cmd) - return not commands.getstatusoutput(cmd)[0] + name = "Mac OS X" + _ConvertedWallpaperLocation = '/tmp/wallpapers/' + _DesktopPlistLocation = os.path.expanduser('~/Library/Preferences/com.apple.desktop.plist') + + def __init__(self, *args, **kwargs): + BaseChanger.__init__(self, *args, **kwargs) + + def remove_old_image_cache(self): + """Cleans up any old temp images""" + if not os.path.isdir(self._ConvertedWallpaperLocation): + os.mkdir(self._ConvertedWallpaperLocation) + for fullpath, filenames, dirnames in os.walk(self._ConvertedWallpaperLocation, topdown=False): + for filename in filenames: + os.unlink(os.path.join(fullpath, filename)) + for dirname in dirnames: + os.unlink(os.path.join(fullpath, dirname)) + + def convert_image_format(self, filename): + """Convert the image to a png, and store it in a local place""" + self.remove_old_image_cache() + output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time()) + try: + return super(OSXChanger, self).convert_image_format(filename, format='PNG', extension='.png') + except ImportError: + logging.debug('Could not load PIL, going to try just copying the image') + import shutil + output_name = os.path.join(self._ConvertedWallpaperLocation, os.path.basename(filename)) + shutil.copyfile(filename, output_name) + return output_name, True + + def fix_desktop_plist(self): + """Removes the entry in the desktop plist file that specifies the wallpaper for each monitor""" + try: + import Foundation + desktop_plist = Foundation.NSMutableDictionary.dictionaryWithContentsOfFile_(self._DesktopPlistLocation) + # Remove all but the 'default' entry + for k in desktop_plist['Background'].keys(): + if k == 'default': + continue + desktop_plist['Background'].removeObjectForKey_(k) + # Store the plist again (Make sure we write it out atomically -- Don't want to break finder) + desktop_plist.writeToFile_atomically_(self._DesktopPlistLocation, True) + except ImportError: + logging.debug('Could not import the Foundation module, you may have problems with dual screens') + + def set_image(self, filename): + self.fix_desktop_plist() + if self.convert: + filename, ret = self.convert_image_format(filename) + if not ret: + logging.debug("Convert failed") + return False + cmd = """osascript -e 'tell application "finder" to set desktop picture to posix file "%s"'""" % filename + logging.debug(cmd) + return not commands.getstatusoutput(cmd)[0] class WIN32Changer(BaseChanger): - name = "Windows" - _ConvertedWallpaperLocation = os.path.join(os.environ.get('APPDATA', os.path.expanduser('~')), 'wallchanger') - - def __init__(self, *args, **kwargs): - BaseChanger.__init__(self, *args, **kwargs) - if not self.convert: - logging.warn('Running on windows, but convert is not set') - - def remove_old_image_cache(self): - """Cleans up any old temp images""" - if not os.path.isdir(self._ConvertedWallpaperLocation): - os.mkdir(self._ConvertedWallpaperLocation) - for fullpath, filenames, dirnames in os.walk(self._ConvertedWallpaperLocation, topdown=False): - for filename in filenames: - os.unlink(os.path.join(fullpath, filename)) - for dirname in dirnames: - os.unlink(os.path.join(fullpath, dirname)) - - def set_image(self, filename): - import ctypes - user32 = ctypes.windll.user32 - - # Taken from the Platform SDK - SPI_SETDESKWALLPAPER = 20 - SPIF_SENDWININICHANGE = 2 - - if self.convert: - filename, ret = self.convert_image_format(filename) - if not ret: - logging.debug("Convert failed") - return False - - # Parameters for SystemParametersInfoA are: - # (UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni) - user32.SystemParametersInfoA( - SPI_SETDESKWALLPAPER, - 0, - filename, - SPIF_SENDWININICHANGE, - ) - return True + name = "Windows" + _ConvertedWallpaperLocation = os.path.join(os.environ.get('APPDATA', os.path.expanduser('~')), 'wallchanger') + + def __init__(self, *args, **kwargs): + BaseChanger.__init__(self, *args, **kwargs) + if not self.convert: + logging.warn('Running on windows, but convert is not set') + + def remove_old_image_cache(self): + """Cleans up any old temp images""" + if not os.path.isdir(self._ConvertedWallpaperLocation): + os.mkdir(self._ConvertedWallpaperLocation) + for fullpath, filenames, dirnames in os.walk(self._ConvertedWallpaperLocation, topdown=False): + for filename in filenames: + os.unlink(os.path.join(fullpath, filename)) + for dirname in dirnames: + os.unlink(os.path.join(fullpath, dirname)) + + def set_image(self, filename): + import ctypes + user32 = ctypes.windll.user32 + + # Taken from the Platform SDK + SPI_SETDESKWALLPAPER = 20 + SPIF_SENDWININICHANGE = 2 + + if self.convert: + filename, ret = self.convert_image_format(filename) + if not ret: + logging.debug("Convert failed") + return False + + # Parameters for SystemParametersInfoA are: + # (UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni) + user32.SystemParametersInfoA( + SPI_SETDESKWALLPAPER, + 0, + filename, + SPIF_SENDWININICHANGE, + ) + return True class Gnome2Changer(BaseChanger): - name = "Gnome" - def set_image(self, filename): - cmd = ['gconftool-2', '--type', 'string', '--set', '/desktop/gnome/background/picture_filename', filename] - logging.debug(cmd) - return not self._exec_cmd(cmd) + name = "Gnome" + def set_image(self, filename): + cmd = ['gconftool-2', '--type', 'string', '--set', '/desktop/gnome/background/picture_filename', filename] + logging.debug(cmd) + return not self._exec_cmd(cmd) class Gnome3Changer(BaseChanger): - name = "Gnome3" - def set_image(self, filename): - cmd = ['gsettings', 'set', 'org.gnome.desktop.background', 'picture-uri', 'file://'+filename] - logging.debug(cmd) - return not self._exec_cmd(cmd) + name = "Gnome3" + def set_image(self, filename): + cmd = ['gsettings', 'set', 'org.gnome.desktop.background', 'picture-uri', 'file://'+filename] + logging.debug(cmd) + return not self._exec_cmd(cmd) class Xfce4Changer(BaseChanger): - name = "XFCE4" - def set_image(self, filename): - cmd = [ - "xfconf-query", - "-c", "xfce4-desktop", - "-p", "/backdrop/screen0/monitor0/image-path", - "-s", filename, - ] - logging.debug(cmd) - return not self._exec_cmd(cmd) + name = "XFCE4" + def set_image(self, filename): + cmd = [ + "xfconf-query", + "-c", "xfce4-desktop", + "-p", "/backdrop/screen0/monitor0/image-path", + "-s", filename, + ] + logging.debug(cmd) + return not self._exec_cmd(cmd) def main(filename): - logging.basicConfig(level=logging.DEBUG, format="%(levelname)s: %(message)s") - init() - set_image(filename) + logging.basicConfig(level=logging.DEBUG, format="%(levelname)s: %(message)s") + init() + set_image(filename) if __name__ == "__main__": - try: - filename = sys.argv[1] - except: - print >>sys.stderr, "Usage: %s filename" % sys.argv[0] - sys.exit(1) + try: + filename = sys.argv[1] + except: + print >>sys.stderr, "Usage: %s filename" % sys.argv[0] + sys.exit(1) - main(filename) + main(filename)