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