]> git.phdru.name Git - xsetbg.git/blobdiff - xsetbg.py
Expand tilde in the database path
[xsetbg.git] / xsetbg.py
index ce3ffad81a0c3db08c712e9802191703da30c108..ac55e25f4271ae60c300ead1650c5287e88009a8 100755 (executable)
--- a/xsetbg.py
+++ b/xsetbg.py
@@ -3,21 +3,30 @@
 
 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.
+using xli program.
 
 """
 
-__version__ = "$Revision$"[11:-2]
-__revision__ = "$Id$"[5:-2]
-__date__ = "$Date$"[7:-2]
-
-__author__ = "Oleg Broytman <phd@phd.pp.ru>"
-__copyright__ = "Copyright (C) 2000-2010 PhiloSoft Design"
+__author__ = "Oleg Broytman <phd@phdru.name>"
+__copyright__ = "Copyright (C) 2000-2014 PhiloSoft Design"
 __license__ = "GNU GPL"
 
+__all__ = ['host', 'port', 'change']
+
 
-import sys, os
+import anydbm
+from fcntl import flock, LOCK_EX, LOCK_UN, LOCK_NB
+from datetime import timedelta
+import os
+import random
+import re
+import shelve
 import subprocess
+import sys
+from time import time
+
+from xsetbg_conf import xsetbg_dir, xsetbg_conf
+from xsetbg_db import xsetbg_db
 
 
 def error(error_str, error_code=1):
@@ -25,29 +34,21 @@ def error(error_str, error_code=1):
    sys.exit(error_code)
 
 
-from ConfigParser import SafeConfigParser
-
-xsetbg_dir = os.path.dirname(os.path.abspath(__file__))
-os.chdir(xsetbg_dir)
-
-config = SafeConfigParser()
-config.read("xsetbg.conf")
-
-if config.has_option("images", "directory") or \
-   config.has_option("images", "directory0") or \
-   config.has_option("images", "directory1"):
+if xsetbg_conf.has_option("images", "directory") or \
+   xsetbg_conf.has_option("images", "directory0") or \
+   xsetbg_conf.has_option("images", "directory1"):
    image_dirs = []
-   if config.has_option("images", "directory"):
-      image_dirs.append(config.get("images", "directory"))
-   if config.has_option("images", "directory0"):
-      image_dirs.append(config.get("images", "directory0"))
-   if config.has_option("images", "directory1"):
-      image_dirs.append(config.get("images", "directory1"))
+   if xsetbg_conf.has_option("images", "directory"):
+      image_dirs.append(xsetbg_conf.get("images", "directory"))
+   if xsetbg_conf.has_option("images", "directory0"):
+      image_dirs.append(xsetbg_conf.get("images", "directory0"))
+   if xsetbg_conf.has_option("images", "directory1"):
+      image_dirs.append(xsetbg_conf.get("images", "directory1"))
    i = 2
    while True:
       option = "directory%d" % i
-      if config.has_option("images", option):
-         image_dirs.append(config.get("images", option))
+      if xsetbg_conf.has_option("images", option):
+         image_dirs.append(xsetbg_conf.get("images", option))
          i += 1
       else:
          break
@@ -60,41 +61,48 @@ image_dirs = [os.path.join(xsetbg_dir,
 ]
 
 # minimum time in seconds between background image changes
-if config.has_option("xsetbg", "min_pause"):
-   min_pause = config.getint("xsetbg", "min_pause")
+if xsetbg_conf.has_option("xsetbg", "min_pause"):
+   min_pause = xsetbg_conf.getint("xsetbg", "min_pause")
 else:
    min_pause = 60
 
-borders = config.get("xsetbg", "borders").split(',')
-if config.has_option("xsetbg", "borders"):
-   borders = [border.strip() for border in config.get("xsetbg", "borders").split(',')]
+borders = xsetbg_conf.get("xsetbg", "borders").split(',')
+if xsetbg_conf.has_option("xsetbg", "borders"):
+   borders = [border.strip() for border in xsetbg_conf.get("xsetbg", "borders").split(',')]
 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")
+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)
+
 else:
    min_delay = 3600*24 # 24 hours
 
 # httpd settings
-if config.has_option("httpd", "host"):
-   host = config.get("httpd", "host")
+if xsetbg_conf.has_option("httpd", "host"):
+   host = xsetbg_conf.get("httpd", "host")
 else:
    host = 'localhost'
 
-if config.has_option("httpd", "port"):
-   port = config.getint("httpd", "port")
+if xsetbg_conf.has_option("httpd", "port"):
+   port = xsetbg_conf.getint("httpd", "port")
 else:
    error("Config must specify a port to listen. Abort.")
 
-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"
@@ -102,37 +110,33 @@ 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
+# Create the database if it is not exists yet.
 
-# 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.
+os.umask(0066) # octal; -rw-------; make the global persistent dictionary
+               # readable only by the user
 
 try:
-   global_db = shelve.open(global_db_name, flag='c')
+   xsetbg_db = shelve.open(xsetbg_db_path, 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')
+      os.remove(xsetbg_db_path)
+      xsetbg_db = shelve.open(xsetbg_db_path, 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:
+for key in xsetbg_db.keys():
+   if key.startswith('/') and xsetbg_db[key] < old_time:
       to_delete.append(key)
 
 for key in to_delete:
    try:
-      del global_db[key]
+      del xsetbg_db[key]
    except KeyError:
       pass
 
-global_db.close() # Close DB in the parent process
+xsetbg_db.close() # Close DB in the parent process
 
 
 images = []
@@ -146,12 +150,7 @@ if not images:
    error("No images found. Abort.")
 
 
-def published(func):
-    func._wsgi_published = True
-    return func
-
-@published
-def ping(force=False):
+def change(force=False):
    # Use the program's file as the lock file:
    # lock it to prevent two processes run in parallel.
    lock_file = open("xsetbg.py", 'r')
@@ -162,12 +161,12 @@ def ping(force=False):
       return
 
 
-   global_db = None
+   xsetbg_db = None
    try:
       # Reopen the global persistent dictionary
-      global_db = shelve.open(global_db_name, 'w')
+      xsetbg_db = shelve.open(xsetbg_db_path, 'w')
 
-      timestamp = global_db.get(timestamp_key)
+      timestamp = xsetbg_db.get(timestamp_key)
       current_time = time()
 
       if not force and timestamp is not None and \
@@ -175,24 +174,24 @@ def ping(force=False):
             return
 
       # Save current time
-      global_db[timestamp_key] = current_time
+      xsetbg_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 xsetbg_db.has_key(image_name):
+            image_time = xsetbg_db[image_name]
             if current_time - image_time > min_delay:
                break
          else:
             break
-      global_db[image_name] = current_time
+      xsetbg_db[image_name] = current_time
 
       # Save filename
-      if global_db.has_key(filename_key):
-         global_db[old_filename_key] = global_db[filename_key]
-      global_db[filename_key] = image_name
+      if xsetbg_db.has_key(filename_key):
+         xsetbg_db[old_filename_key] = xsetbg_db[filename_key]
+      xsetbg_db[filename_key] = image_name
 
       program_options = ["xli", "-onroot", "-quiet"] + \
          ["-center", "-border", random.choice(borders), "-zoom", "auto",
@@ -207,54 +206,4 @@ def ping(force=False):
       flock(lock_file, LOCK_UN)
       lock_file.close()
       # Flush and close the global persistent dictionary
-      if global_db: global_db.close()
-
-@published
-def force():
-    ping(force=True)
-
-@published
-def stop():
-    QuitWSGIServer._quit_flag = True
-
-
-from wsgiref.handlers import SimpleHandler
-from wsgiref import simple_server
-simple_server.ServerHandler = SimpleHandler # Stop logging to stdout
-from wsgiref.simple_server import WSGIServer, make_server
-
-g = globals().copy()
-commands = dict([(name, g[name]) for name in g
-    if getattr(g[name], '_wsgi_published', False)])
-del g
-
-class QuitWSGIServer(WSGIServer):
-    _quit_flag = False
-
-    def serve_forever(self):
-        while not self._quit_flag:
-            self.handle_request()
-
-def app(env, start_response):
-    command = env['PATH_INFO'][1:] # Remove the leading slash
-    if command not in commands:
-        status = '404 Not found'
-        response_headers = [('Content-type', 'text/plain')]
-        start_response(status, response_headers)
-        return ['The command was not found.\n']
-
-    try:
-        commands[command]()
-    except:
-        status = '500 Error'
-        response_headers = [('Content-type', 'text/plain')]
-        start_response(status, response_headers)
-        return ['Error occured!\n']
-
-    status = '200 OK'
-    response_headers = [('Content-type', 'text/plain')]
-    start_response(status, response_headers)
-    return ['Ok\n']
-
-httpd = make_server(host, port, app, server_class=QuitWSGIServer)
-httpd.serve_forever()
+      if xsetbg_db: xsetbg_db.close()