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