]> code.delx.au - bg-scripts/blob - randombg.py
randombg: Fixed reload for the file lists Greg doesn't use
[bg-scripts] / randombg.py
1 #!/usr/bin/env python
2
3 VERSION = "2.0"
4
5
6 import asyncore, asynchat, socket
7 import os, os.path, random, sys, time
8 from optparse import OptionParser
9 import logging
10 from logging import debug, info, warning, error, critical
11 logging.basicConfig(format="%(levelname)s: %(message)s")
12 try:
13 import cPickle as pickle
14 except ImportError:
15 import pickle
16
17 try:
18 # Required libraries
19 import asyncsched
20 import wallchanger
21 except ImportError, e:
22 critical("Missing libraries! Exiting...")
23 sys.exit(1)
24
25
26
27
28 def filter_images(filenames):
29 extensions = ('.jpg', '.jpe', '.jpeg', '.png', '.gif', '.bmp')
30 for filename in filenames:
31 _, ext = os.path.splitext(filename)
32 if ext.lower() in extensions:
33 yield filename
34
35 class BaseFileList(object):
36 """Base file list implementation"""
37 def scan_paths(self):
38 raise NotImplementedError()
39
40 def add_path(self, path):
41 raise NotImplementedError()
42
43 def store_cache(self, path):
44 pass
45
46 def load_cache(self, filename, rescanPaths = False):
47 pass
48
49 def get_next_image(self):
50 raise NotImplementedError()
51
52 def get_prev_image(self):
53 raise NotImplementedError()
54
55 def get_current_image(self):
56 raise NotImplementedError()
57
58 def is_empty(self):
59 return True
60
61
62 class RandomFileList(BaseFileList):
63 def __init__(self):
64 self.list = []
65 self.paths = []
66 self.last_image = None
67
68 def scan_paths(self):
69 for path in self.paths:
70 for dirpath, dirsnames, filenames in os.walk(path):
71 for filename in filter_images(filenames):
72 self.list.append(os.path.join(dirpath, filename))
73
74 def add_path(self, path):
75 self.paths.append(path)
76 debug('Added path "%s" to the list' % path)
77
78 def get_next_image(self):
79 n = random.randint(0, len(self.list)-1)
80 self.last_image = self.list[n]
81 debug("Picked file '%s' from list" % self.last_image)
82 return self.last_image
83
84 def get_current_image(self):
85 if self.last_image:
86 return self.last_image
87 else:
88 return self.get_next_image()
89
90 def is_empty(self):
91 return len(self.list) == 0
92
93
94 class AllRandomFileList(BaseFileList):
95 def __init__(self):
96 self.list = None
97 self.paths = []
98 self.imagePointer = 0
99
100 # Scan the input directory, and then randomize the file list
101 def scan_paths(self):
102 debug("Scanning paths")
103
104 self.list = []
105 for path in self.paths:
106 debug('Scanning "%s"' % path)
107 for dirpath, dirsnames, filenames in os.walk(path):
108 for filename in filter_images(filenames):
109 debug('Adding file "%s"' % filename)
110 self.list.append(os.path.join(dirpath, filename))
111
112 random.shuffle(self.list)
113
114 def add_path(self, path):
115 self.paths.append(path)
116 debug('Added path "%s" to the list' % path)
117
118 def store_cache(self, filename):
119 try:
120 fd = open(filename, 'wb')
121 pickle.dump(obj = self, file = fd, protocol = 2)
122 debug("Cache successfully stored")
123 except Exception, e:
124 warning("Storing cache: %s" % e)
125
126 def load_cache(self, filename, rescanPaths = False):
127 debug('Attempting to load cache from "%s"' % filename)
128 self.paths.sort()
129 try:
130 fd = open(filename, 'rb')
131 tmp = pickle.load(fd)
132 if self.paths == tmp.paths:
133 debug("Path lists match, copying properties")
134 # Overwrite this object with the other
135 for attr in ('list', 'imagePointer'):
136 setattr(self, attr, getattr(tmp, attr))
137 else:
138 debug("Ignoring cache, path lists do not match")
139 except Exception, e:
140 warning("Loading cache: %s" % e)
141
142 def get_current_image(self):
143 return self.list[self.imagePointer]
144
145 def __inc_in_range(self, n, amount = 1, rangeMax = None, rangeMin = 0):
146 if rangeMax == None: rangeMax = len(self.list)
147 assert rangeMax > 0
148 return (n + amount) % rangeMax
149
150 def get_next_image(self):
151 self.imagePointer = self.__inc_in_range(self.imagePointer)
152 imageName = self.list[self.imagePointer]
153 debug("Picked file '%s' (pointer=%d) from list" % (imageName, self.imagePointer))
154 return imageName
155
156 def get_prev_image(self):
157 self.imagePointer = self.__inc_in_range(self.imagePointer, amount=-1)
158 imageName = self.list[self.imagePointer]
159 debug("Picked file '%s' (pointer=%d) from list" % (imageName, self.imagePointer))
160 return imageName
161
162 def is_empty(self):
163 return len(self.list) == 0
164
165 class FolderRandomFileList(BaseFileList):
166 """A file list that will pick a file randomly within a directory. Each
167 directory has the same chance of being chosen."""
168 def __init__(self):
169 self.directories = {}
170 self.last_image = None
171
172 def scan_paths(self):
173 pass
174
175 def add_path(self, path):
176 debug('Added path "%s" to the list' % path)
177 for dirpath, dirs, filenames in os.walk(path):
178 debug('Scanning "%s" for images' % dirpath)
179 if self.directories.has_key(dirpath):
180 continue
181 filenames = list(filter_images(filenames))
182 if len(filenames):
183 self.directories[dirpath] = filenames
184 debug('Adding "%s" to "%s"' % (filenames, dirpath))
185 else:
186 debug("No images found in '%s'" % dirpath)
187
188 def get_next_image(self):
189 directory = random.choice(self.directories.keys())
190 debug('directory: "%s"' % directory)
191 filename = random.choice(self.directories[directory])
192 debug('filename: "%s"' % filename)
193 return os.path.join(directory, filename)
194
195 def get_current_image(self):
196 if self.last_image:
197 return self.last_image
198 else:
199 return self.get_next_image()
200
201 def is_empty(self):
202 return len(self.directories.values()) == 0
203
204
205 class Cycler(object):
206 def init(self, options, paths):
207 self.cycle_time = options.cycle_time
208 self.history_filename = options.history_filename
209
210 debug("Initialising wallchanger")
211 wallchanger.init(options.background_colour, options.permanent)
212
213 debug("Initialising file list")
214 if options.all_random:
215 self.filelist = AllRandomFileList()
216 elif options.folder_random:
217 self.filelist = FolderRandomFileList()
218 else:
219 self.filelist = RandomFileList()
220
221 for path in paths:
222 self.filelist.add_path(path)
223
224 if self.filelist.load_cache(self.history_filename):
225 debug("Loaded cache successfully")
226 else:
227 debug("Could not load cache")
228 self.filelist.scan_paths()
229
230 if self.filelist.is_empty():
231 error("No images were found. Exiting...")
232 sys.exit(1)
233
234 self.task = None
235 self.cmd_reload()
236
237 def finish(self):
238 self.filelist.store_cache(self.history_filename)
239
240 def find_files(self, options, paths):
241 return filelist
242
243 def cmd_reset(self):
244 def next():
245 image = self.filelist.get_next_image()
246 wallchanger.set_image(image)
247 self.task = None
248 self.cmd_reset()
249
250 if self.task is not None:
251 self.task.cancel()
252 self.task = asyncsched.schedule(self.cycle_time, next)
253 debug("Reset timer for %s seconds" % self.cycle_time)
254
255 def cmd_reload(self):
256 image = self.filelist.get_current_image()
257 wallchanger.set_image(image)
258 self.cmd_reset()
259
260 def cmd_next(self):
261 image = self.filelist.get_next_image()
262 wallchanger.set_image(image)
263 self.cmd_reset()
264
265 def cmd_prev(self):
266 image = self.filelist.get_prev_image()
267 wallchanger.set_image(image)
268 self.cmd_reset()
269
270 def cmd_rescan(self):
271 self.filelist.scan_paths()
272 self.cmd_next()
273
274 def cmd_pause(self):
275 if self.task is not None:
276 self.task.cancel()
277 self.task = None
278
279 def cmd_exit(self):
280 asyncsched.exit()
281
282 class Server(asynchat.async_chat):
283 def __init__(self, cycler, conn, addr):
284 asynchat.async_chat.__init__(self, conn=conn)
285 self.cycler = cycler
286 self.ibuffer = []
287 self.set_terminator("\n")
288
289 def collect_incoming_data(self, data):
290 self.ibuffer.append(data)
291
292 def found_terminator(self):
293 line = "".join(self.ibuffer).lower()
294 self.ibuffer = []
295 prefix, cmd = line.split(None, 1)
296 if prefix != "cmd":
297 debug('Bad line received "%s"' % line)
298 return
299 if hasattr(self.cycler, "cmd_" + cmd):
300 debug('Executing command "%s"' % cmd)
301 getattr(self.cycler, "cmd_" + cmd)()
302 else:
303 debug('Unknown command received "%s"' % cmd)
304
305
306
307 class Listener(asyncore.dispatcher):
308 def __init__(self, socket_filename, cycler):
309 asyncore.dispatcher.__init__(self)
310 self.cycler = cycler
311 self.create_socket(socket.AF_UNIX, socket.SOCK_STREAM)
312 self.bind(socket_filename)
313 self.listen(2) # Backlog = 2
314
315 def handle_accept(self):
316 conn, addr = self.accept()
317 Server(self.cycler, conn, addr)
318
319 def writable(self):
320 return False
321
322
323 def do_server(options, paths):
324 try:
325 cycler = Cycler()
326 listener = Listener(options.socket_filename, cycler)
327 # Initialisation of Cycler delayed so we grab the socket quickly
328 cycler.init(options, paths)
329 try:
330 asyncsched.loop()
331 except KeyboardInterrupt:
332 print
333 cycler.finish()
334 finally:
335 # Make sure that the socket is cleaned up
336 try:
337 os.unlink(options.socket_filename)
338 except:
339 pass
340
341 def do_client(options, args):
342 if len(args) == 0:
343 args = ["next"]
344 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
345 sock.connect(options.socket_filename)
346 sock = sock.makefile()
347 for i, cmd in enumerate(args):
348 sock.write("cmd %s\n" % cmd)
349 if i+1 != len(args):
350 time.sleep(options.cycle_time)
351 sock.close()
352
353 def do_oneshot(options, paths):
354 cycler = Cycler()
355 cycler.init(options, paths)
356
357 def build_parser():
358 parser = OptionParser(version="%prog " + VERSION,
359 description = "Cycles through random background images.",
360 usage =
361 "\n(server) %prog [options] dir [dir2 ...]"
362 "\n(client) %prog [options] [next|prev|rescan|reload|pause] [...]"
363 "\nThe first instance to be run will be the server.\n"
364 )
365 parser.add_option("-p", "--permanent",
366 action="store_true", dest="permanent", default=False,
367 help="Make the background permanent. Note: This will cause all machines logged in with this account to simultaneously change background [Default: %default]")
368 parser.add_option("-v", '-d', "--verbose", "--debug",
369 action="count", dest="verbose", default=0,
370 help="Make the louder (good for debugging, or those who are curious)")
371 parser.add_option("-b", "--background-colour",
372 action="store", type="string", dest="background_colour", default="black",
373 help="Change the default background colour that is displayed if the image is not in the correct aspect ratio [Default: %default]")
374 parser.add_option("--all-random",
375 action="store_true", dest="all_random", default=False,
376 help="Make sure that all images have been displayed before repeating an image")
377 parser.add_option("-1", "--oneshot",
378 action="store_true", dest="oneshot", default=False,
379 help="Set one random image and terminate immediately.")
380 parser.add_option("--folder-random",
381 action="store_true", dest="folder_random", default=False,
382 help="Give each folder an equal chance of having an image selected from it")
383 parser.add_option("--convert",
384 action="store_true", dest="convert", default=False,
385 help="Do conversions using ImageMagick or PIL, don't rely on the window manager")
386 parser.add_option("--cycle-time",
387 action="store", type="int", default=1800, dest="cycle_time",
388 help="Cause the image to cycle every X seconds")
389 parser.add_option("--socket",
390 action="store", type="string", dest="socket_filename", default=os.path.expanduser('~/.randombg_socket'),
391 help="Location of the command/control socket.")
392 parser.add_option("--history-file",
393 action="store", type="string", dest="history_filename", default=os.path.expanduser('~/.randombg_historyfile'),
394 help="Stores the location of the last image to be loaded.")
395 return parser
396
397 def main():
398 parser = build_parser()
399 options, args = parser.parse_args(sys.argv[1:])
400
401 if options.verbose == 1:
402 logging.getLogger().setLevel(logging.INFO)
403 elif options.verbose >= 2:
404 logging.getLogger().setLevel(logging.DEBUG)
405
406 if options.oneshot:
407 do_oneshot(options, args)
408
409 if os.path.exists(options.socket_filename):
410 do_client(options, args)
411 else:
412 do_server(options, args)
413
414
415 if __name__ == "__main__":
416 main()
417