]> git.phdru.name Git - xsetbg.git/blobdiff - xsetbg.py
Print shown files sorted by date.
[xsetbg.git] / xsetbg.py
index 0a4a3f97b6169e3de3bbe12035b2d97b5b5abab6..e3f3365532d6b65f4f484f46c657aa557148df08 100755 (executable)
--- a/xsetbg.py
+++ b/xsetbg.py
@@ -1,17 +1,20 @@
 #! /usr/local/bin/python -O
-"""Select a random image from $HOME/lib/xsetbg/images subdirectories
-and set it as the desktop wallpaper (display it in the root window).
+"""Set a random background image (XWin)
+
+Select a random image from a (list of) directory(s)
+and set it as the desktop wallpaper (display it in the root window)
+using xli or xsetbg programs.
 
-Author: Oleg BroytMann <phd@phd.pp.ru>
-Copyright (C) 2000-2006 PhiloSoft Design
 """
 
-__version__ = "2.17.0"
-__author__ = "Oleg BroytMann <phd@phd.pp.ru>"
-__copyright__ = "Copyright (C) 2000-2006 PhiloSoft Design"
+__version__ = "$Revision$"[11:-2]
 __revision__ = "$Id$"[5:-2]
 __date__ = "$Date$"[7:-2]
 
+__author__ = "Oleg BroytMann <phd@phd.pp.ru>"
+__copyright__ = "Copyright (C) 2000-2006 PhiloSoft Design"
+__license__ = "GNU GPL"
+
 
 import sys, os
 
@@ -91,6 +94,12 @@ if config.has_option("xsetbg", "borders"):
 else:
    borders = ["darkcyan", "steelblue", "midnightblue"]
 
+# minimum time in seconds between occurences of the same image
+if config.has_option("xsetbg", "min_delay"):
+   min_delay = config.getint("xsetbg", "min_delay")
+else:
+   min_delay = 3600*24 # 24 hours
+
 del config
 
 
@@ -99,6 +108,12 @@ os.umask(0066) # octal; -rw-------; make the global persistent dictionary
 global_db_name = "xsetbg.db"
 
 
+# DB keys
+timestamp_key = "timestamp"
+filename_key = "filename"
+old_filename_key = "old_filename"
+
+
 import random
 import anydbm, shelve
 from time import time
@@ -115,13 +130,21 @@ except anydbm.error, msg:
       os.remove(global_db_name)
       global_db = shelve.open(global_db_name, flag='c')
 
-global_db.close() # Close DB in parent process
+# Remove old filenames
+old_time = time() - min_delay
 
+to_delete = [timestamp_key]
+for key in global_db.keys():
+   if key.startswith('/') and global_db[key] < old_time:
+      to_delete.append(key)
 
-# DB keys
-timestamp_key = "timestamp"
-filename_key = "filename"
-old_filename_key = "old_filename"
+for key in to_delete:
+   try:
+      del global_db[key]
+   except KeyError:
+      pass
+
+global_db.close() # Close DB in the parent process
 
 
 images = []
@@ -183,8 +206,19 @@ def run():
       # Save current time
       global_db[timestamp_key] = current_time
 
+      # Select a random image and check if we've seen it recently;
+      # loop until we can find a new image (never seen before) or old enough.
+      for i in xrange(len(images)): # ensure the loop is not infinite
+         image_name = random.choice(images)
+         if global_db.has_key(image_name):
+            image_time = global_db[image_name]
+            if current_time - image_time > min_delay:
+               break
+         else:
+            break
+      global_db[image_name] = current_time
+
       border = random.choice(borders)
-      image_name = random.choice(images)
       root, ext = os.path.splitext(image_name)
 
       # Save filename