-if config.has_option("xsetbg", "min_delay"):
- min_delay = config.getint("xsetbg", "min_delay")
-else:
- min_delay = 3600*24 # 24 hours
-
-del config
-
-
-os.umask(0066) # octal; -rw-------; make the global persistent dictionary
- # readable only by the user
-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
-from fcntl import flock, LOCK_EX, LOCK_UN, LOCK_NB
-
-# Import pickle and all anydbm/shelve internal machinery, so that
-# when ReadyExec forks they will be ready.
-# Also create the database if it is not exists yet.
-
-try:
- global_db = shelve.open(global_db_name, flag='c')
-except anydbm.error, msg:
- if str(msg) == "db type could not be determined":
- os.remove(global_db_name)
- global_db = shelve.open(global_db_name, flag='c')
-
-# 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)
-
-for key in to_delete:
- try:
- del global_db[key]
- except KeyError:
- pass
-
-global_db.close() # Close DB in the parent process
-
-
-images = []
-
-for image_dir in image_dirs:
- # List images in all subdirectories
- for dirpath, dirs, files in os.walk(image_dir):
- images.extend([os.path.join(dirpath, file) for file in files])
-
-if not images:
- error("No images found. Abort.")
+if xsetbg_conf.has_option("xsetbg", "min_delay"):
+ min_delay = xsetbg_conf.get("xsetbg", "min_delay")
+
+ # Borrowed from http://stackoverflow.com/a/2765366
+ td_re = re.compile('(?:(?P<years>\\d+)y)?'
+ '(?:(?P<months>\\d+)m)?'
+ '(?:(?P<days>\\d+)d)?'
+ '(?:T(?:(?P<hours>\\d+)h)?'
+ '(?:(?P<minutes>\\d+)m)?(?:(?P<seconds>\\d+)s)?)?')
+ td_dict = td_re.match(min_delay).groupdict(0)
+ delta = timedelta(days=int(td_dict['days']) +
+ (int(td_dict['months']) * 30) +
+ (int(td_dict['years']) * 365),
+ hours=int(td_dict['hours']),
+ minutes=int(td_dict['minutes']),
+ seconds=int(td_dict['seconds']))
+
+ if delta:
+ min_delay = delta.days * 24*3600 + delta.seconds
+ else:
+ min_delay = int(min_delay)