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