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