SHARE
TWEET

Wallpaper Changer

mbpaster Feb 5th, 2012 282 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. #
  3. # bg_slideshow.py
  4. # (c) 2012 MB http://somethingididnotknow.wordpress.com
  5. #
  6. # This script is released under WTFPL, but it's heavily inspired from Romano
  7. # Giannetti's original bg_slideshow.py
  8. # See http://rlog.rgtti.com/software/linux/wallpaper-slideshow-for-gnome3/
  9. #
  10. # A simple wallpaper changer for GNOME desktop.
  11. # List your wallpapers folders in file ~/.bg_slideshow.conf and run the script:
  12. #
  13. # python bg_slideshow.py &
  14. #
  15. # or add a startup entry using gnome-session-properties
  16. import sys
  17. import os
  18. import random
  19. from time import gmtime, sleep, strftime
  20.  
  21. class WallpaperChanger:
  22.     # conf file is in ~/.bg_slideshow.conf change it here if you want
  23.     conf_file = os.path.join(os.getenv("HOME"), '.bg_slideshow.conf')
  24.     mode = "zoom" # one of none, centered, wallpaper, scaled, stretched, zoom, spanned
  25.     delay = 3 * 60 # in seconds
  26.     formats = [".jpg", ".png", ".gif", ".jpeg"] # accepted formats
  27.     files = []
  28.     all_files = set()
  29.    
  30.     def __init__(self):
  31.         self.index = 0
  32.         self.parse_conf()
  33.  
  34.     def parse_conf(self):
  35.         """parses the configuration file, interpreting every non-commented line
  36.        as a directory into which wallpaper images reside.
  37.        In case no images are found, the script exits"""
  38.         self.bad_folders = []
  39.         if not os.path.isfile(self.conf_file):
  40.             with open(self.conf_file, 'w') as conf:
  41.                 self.append_error(conf, 'List your wallpapers folder here')
  42.                 print 'List your wallpapers folders in configuration file ', self.conf_file
  43.                 sys.exit(1)
  44.         self.conf_last_modified = os.path.getmtime(self.conf_file)
  45.         self.files = self.get_all_files()
  46.         if not self.files:
  47.             self.append_error(conf, 'No images found!')
  48.             sys.exit(1)
  49.         self.all_files = set(self.files)
  50.         random.shuffle(self.files)
  51.        
  52.     def get_all_files(self):
  53.         """adds all files in all directories found in the configuration file to
  54.        a list containing all wallpapers to be shown with no duplicates (based
  55.        on complete file name)"""
  56.         files = []
  57.         with open(self.conf_file, 'r+') as conf:
  58.              # trim whitespaces and newlines
  59.             for line in [x.strip() for x in conf.readlines()]:
  60.                 if not line.startswith('#'):
  61.                     if os.path.isdir(line):
  62.                         try:
  63.                             files.extend([os.path.join(line, file) for file in os.listdir(line) if os.path.splitext(file)[1] in self.formats])
  64.                         except OSError, e:
  65.                             # deal with bad folders later...
  66.                             self.bad_folders.append('%s: %s' % (line, e.strerror))
  67.             if self.bad_folders:
  68.                 for folder in self.bad_folders:
  69.                     self.append_error(conf, folder)
  70.         return self.remove_duplicates(files)
  71.        
  72.     def remove_duplicates(self, files):
  73.         seen = set()
  74.         seen_add = seen.add
  75.         return [x for x in files if x not in seen and not seen.add(x)]
  76.            
  77.     def append_error(self, conf, line):
  78.         # go to the end of conf file and append messages there
  79.         conf.seek(0, 2)
  80.         conf.write('# %s - %s%s' % (strftime("%Y-%m-%d %H:%M:%S", gmtime()), line, os.linesep))
  81.        
  82.     def poll_for_new_files(self):
  83.         """checks whether new files have been added to the configured folders"""
  84.         all_files = set(self.get_all_files())
  85.         new_files = all_files - self.all_files
  86.         if new_files:
  87.             shown = set(self.files[:self.index])
  88.             self.files = list(all_files - shown)
  89.             self.all_files = all_files
  90.             random.shuffle(self.files)
  91.             self.index = 0
  92.            
  93.     def run(self):
  94.         sleep(self.delay) # this seems to be needed to avoid gnome-shell crashes
  95.         while True:
  96.             last_edit = os.path.getmtime(self.conf_file)
  97.             if last_edit != self.conf_last_modified:
  98.                 self.parse_conf()
  99.             next_bg = self.files[self.index]
  100.             if os.path.isfile(next_bg):
  101.                 os.system('/usr/bin/gsettings set org.gnome.desktop.background picture-uri file://%s' % self.files[self.index])
  102.                 os.system('/usr/bin/gsettings set org.gnome.desktop.background picture-options %s' % self.mode)
  103.                 sleep(self.delay)
  104.             self.index += 1
  105.             self.poll_for_new_files()
  106.             if self.index >= len(self.files):
  107.                 self.index = 0
  108.                 self.parse_conf() # this also checks that at least a bg exists!
  109.                 random.shuffle(self.files)
  110.            
  111. if __name__ == "__main__":
  112.     changer = WallpaperChanger()
  113.     changer.run()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top