]> git.phdru.name Git - xsetbg.git/blob - xsetbg.py
Fix a bug
[xsetbg.git] / xsetbg.py
1 """Set a random background image (XWin)
2
3 Select a random image from a (list of) directory(s)
4 and set it as the desktop wallpaper (display it in the root window)
5 using xli program.
6
7 """
8
9 __author__ = "Oleg Broytman <phd@phdru.name>"
10 __copyright__ = "Copyright (C) 2000-2015 PhiloSoft Design"
11 __license__ = "GNU GPL"
12
13 __all__ = ['change']
14
15 from fcntl import flock, LOCK_EX, LOCK_UN, LOCK_NB
16 from datetime import timedelta
17 import os
18 import random
19 import re
20 import subprocess
21 import sys
22 from time import time
23
24 from xsetbg_conf import xsetbg_dir, xsetbg_conf
25 from xsetbg_db import xsetbg_db
26
27
28 def error(error_str):
29     sys.exit("%s: Error: %s\n" % (sys.argv[0], error_str))
30
31
32 if not xsetbg_db:
33     error("No database found. Run rescan_fs.py. Abort.")
34
35 if xsetbg_db.select().count() == 0:
36     error("No images found. Run rescan_fs.py. Abort.")
37
38
39 # minimum time in seconds between background image changes
40 if xsetbg_conf.has_option("xsetbg", "min_pause"):
41     min_pause = xsetbg_conf.getint("xsetbg", "min_pause")
42 else:
43     min_pause = 60
44
45 borders = xsetbg_conf.get("xsetbg", "borders").split(',')
46 if xsetbg_conf.has_option("xsetbg", "borders"):
47     borders = [border.strip() for border in
48                xsetbg_conf.get("xsetbg", "borders").split(',')]
49 else:
50     borders = ["darkcyan", "steelblue", "midnightblue"]
51
52 # minimum time in seconds between occurences of the same image
53 if xsetbg_conf.has_option("xsetbg", "min_delay"):
54     min_delay = xsetbg_conf.get("xsetbg", "min_delay")
55
56     # Borrowed from http://stackoverflow.com/a/2765366
57     td_re = re.compile('(?:(?P<years>\d+)y)?'
58                        '(?:(?P<months>\d+)m)?'
59                        '(?:(?P<days>\d+)d)?'
60                        '(?:T(?:(?P<hours>\d+)h)?'
61                        '(?:(?P<minutes>\d+)m)?(?:(?P<seconds>\d+)s)?)?')
62     td_dict = td_re.match(min_delay).groupdict(0)
63     delta = timedelta(days=int(td_dict['days']) +
64                       (int(td_dict['months']) * 30) +
65                       (int(td_dict['years']) * 365),
66                       hours=int(td_dict['hours']),
67                       minutes=int(td_dict['minutes']),
68                       seconds=int(td_dict['seconds']))
69
70     if delta:
71         min_delay = delta.days * 24*3600 + delta.seconds
72     else:
73         min_delay = int(min_delay)
74
75 else:
76     min_delay = 3600*24  # 24 hours
77
78
79 if xsetbg_db.select('last_shown IS NULL OR last_shown < %d' %
80                     (time() - min_delay)).count() == 0:
81     error("No unshown images found. Run rescan_fs.py "
82           "or decrease min_delay. Abort.")
83
84
85 def change(force=False):
86     # Use the program's file as the lock file:
87     # lock it to prevent two processes run in parallel.
88     lock_file = open(os.path.join(xsetbg_dir, 'xsetbg.py'), 'r')
89
90     try:
91         flock(lock_file, LOCK_EX | LOCK_NB)
92     except IOError:  # already locked
93         lock_file.close()
94         return
95
96     try:
97         timestamp = xsetbg_db.select('last_shown IS NOT NULL',
98                                      orderBy='-last_shown')[0].last_shown
99         current_time = time()
100
101         if not force and timestamp is not None and \
102                 current_time - timestamp < min_pause:
103             # Too early to change background
104             return
105
106         # Select a random image that has never been shown
107         not_shown_select = xsetbg_db.select('last_shown IS NULL')
108         not_shown_count = not_shown_select.count()
109         if not_shown_count:
110             row = not_shown_select[random.randint(0, not_shown_count - 1)]
111         else:
112             old_shown_select = xsetbg_db.select(
113                 'last_shown IS NOT NULL AND last_shown < %d' %
114                 current_time - min_delay)
115             old_shown_count = old_shown_select.count()
116             if old_shown_count:
117                 row = old_shown_select[random.randint(0, old_shown_count - 1)]
118             else:
119                 error("No images to show found. Run rescan_fs.py "
120                       "or decrease min_delay. Abort.")
121
122         program_options = ["xli", "-border", random.choice(borders),
123                            "-center", "-onroot", "-quiet", "-zoom", "auto",
124                            row.full_name]
125
126         rc = subprocess.call(program_options)
127         if rc:
128             error("cannot execute xli!")
129         else:
130             row.last_shown = current_time
131
132     finally:
133         # Unlock and close the lock file
134         flock(lock_file, LOCK_UN)
135         lock_file.close()