]>
code.delx.au - bg-scripts/blob - bin/randombg.py
6 import asyncore
, asynchat
, socket
7 import os
, os
. path
, random
, sys
, time
8 from optparse
import OptionParser
10 from logging
import debug
, info
, warning
, error
, critical
11 logging
. basicConfig ( format
= " %(levelname)s : %(message)s " )
13 import cPickle
as pickle
21 except ImportError , e
:
22 critical ( "Missing libraries! Exiting..." )
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
:
35 class BaseFileList ( object ):
36 """Base file list implementation"""
38 raise NotImplementedError ()
40 def add_path ( self
, path
):
41 raise NotImplementedError ()
43 def store_cache ( self
, path
):
46 def load_cache ( self
, filename
, rescanPaths
= False ):
49 def get_next_image ( self
):
50 raise NotImplementedError ()
52 def get_prev_image ( self
):
53 raise NotImplementedError ()
55 def get_current_image ( self
):
56 raise NotImplementedError ()
62 class RandomFileList ( BaseFileList
):
66 self
. last_image
= None
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
))
74 def add_path ( self
, path
):
75 self
. paths
. append ( path
)
76 debug ( 'Added path " %s " to the list' % path
)
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
85 return len ( self
. list ) > 0
88 class AllRandomFileList ( BaseFileList
):
94 # Scan the input directory, and then randomize the file list
96 debug ( "Scanning paths" )
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
))
106 random
. shuffle ( self
. list )
108 def add_path ( self
, path
):
109 self
. paths
. append ( path
)
110 debug ( 'Added path " %s " to the list' % path
)
112 def store_cache ( self
, filename
):
114 fd
= open ( filename
, 'wb' )
115 pickle
. dump ( obj
= self
, file = fd
, protocol
= 2 )
116 debug ( "Cache successfully stored" )
118 warning ( "Exception while storing cache: ' %s '" % e
)
120 def load_cache ( self
, filename
, rescanPaths
= False ):
121 debug ( 'Attempting to load cache from " %s "' % filename
)
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
))
132 debug ( "Ignoring cache, path lists do not match" )
134 warning ( "Exception while loading cache: ' %s '" % e
)
136 def get_current_image ( self
):
137 return self
. list [ self
. imagePointer
]
139 def __inc_in_range ( self
, n
, amount
= 1 , rangeMax
= None , rangeMin
= 0 ):
140 if rangeMax
== None : rangeMax
= len ( self
. list )
142 return ( n
+ amount
) % rangeMax
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
))
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
))
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."""
163 self
. directories
= {}
165 def scan_paths ( self
):
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
):
174 filenames
= list ( filter_images ( filenames
))
176 self
. directories
[ dirpath
] = filenames
177 debug ( 'Adding " %s " to " %s "' % ( filenames
, dirpath
))
179 debug ( "No images found in ' %s '" % dirpath
)
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
)
189 return len ( self
. directories
. values ())
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..." )
199 debug ( "Initialising wallchanger" )
200 wallchanger
. init ( options
. background_colour
, options
. permanent
)
201 self
. cycle_time
= options
. cycle_time
206 def find_files ( self
, options
, paths
):
207 if options
. all_random
:
208 filelist
= AllRandomFileList ()
209 elif options
. folder_random
:
210 filelist
= FolderRandomFileList ()
212 filelist
= RandomFileList ()
215 filelist
. add_path ( path
)
217 if filelist
. load_cache ( options
. history_filename
):
218 debug ( "Loaded cache successfully" )
220 debug ( "Could not load cache" )
221 filelist
. scan_paths ()
226 image
= self
. filelist
. get_next_image ()
227 wallchanger
. set_image ( image
)
231 if self
. task
is not None :
233 self
. task
= asyncsched
. schedule ( self
. cycle_time
, next
)
234 debug ( "Reset timer for %s seconds" % self
. cycle_time
)
236 def cmd_reload ( self
):
237 image
= self
. filelist
. get_current_image ()
238 wallchanger
. set_image ( image
)
242 image
= self
. filelist
. get_next_image ()
243 wallchanger
. set_image ( image
)
247 image
= self
. filelist
. get_prev_image ()
248 wallchanger
. set_image ( image
)
251 def cmd_rescan ( self
):
252 self
. filelist
. scan_paths ()
256 if self
. task
is not None :
260 class Server ( asynchat
. async_chat
):
261 def __init__ ( self
, cycler
, conn
, addr
):
262 asynchat
. async_chat
.__ init
__ ( self
, conn
= conn
)
265 self
. set_terminator ( " \n " )
267 def collect_incoming_data ( self
, data
):
268 self
. ibuffer
. append ( data
)
270 def found_terminator ( self
):
271 line
= "" . join ( self
. ibuffer
). lower ()
273 prefix
, cmd
= line
. split ( None , 1 )
275 debug ( 'Bad line received " %s "' % line
)
277 if hasattr ( self
. cycler
, "cmd_" + cmd
):
278 debug ( 'Executing command " %s "' % cmd
)
279 getattr ( self
. cycler
, "cmd_" + cmd
)()
281 debug ( 'Unknown command received " %s "' % cmd
)
285 class Listener ( asyncore
. dispatcher
):
286 def __init__ ( self
, socket_filename
, cycler
):
287 asyncore
. dispatcher
.__ init
__ ( self
)
289 self
. create_socket ( socket
. AF_UNIX
, socket
. SOCK_STREAM
)
290 self
. bind ( socket_filename
)
291 self
. listen ( 2 ) # Backlog = 2
293 def handle_accept ( self
):
294 conn
, addr
= self
. accept ()
295 Server ( self
. cycler
, conn
, addr
)
298 def do_server ( options
, paths
):
301 cycler
= Cycler ( options
, paths
)
302 listener
= Listener ( options
. socket_filename
, cycler
)
304 except KeyboardInterrupt :
307 # Make sure that the socket is cleaned up
309 os
. unlink ( options
. socket_filename
)
313 def do_client ( options
, args
):
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
)
322 time
. sleep ( options
. cycle_time
)
327 parser
= OptionParser ( version
= "%prog " + VERSION
,
328 description
= "Cycles through random background images." ,
330 " \n (server) %prog [options] dir [dir2 ...]"
331 " \n (client) %prog [options] [next|prev|rescan|reload|pause] [...]"
332 " \n The first instance to be run will be the server. \n "
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: %def ault]" )
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: %def ault]" )
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." )
361 parser
= build_parser ()
362 options
, args
= parser
. parse_args ( sys
. argv
[ 1 :])
364 if options
. verbose
== 1 :
365 logging
. getLogger (). setLevel ( logging
. INFO
)
366 elif options
. verbose
>= 2 :
367 logging
. getLogger (). setLevel ( logging
. DEBUG
)
369 if os
. path
. exists ( options
. socket_filename
):
370 do_client ( options
, args
)
372 do_server ( options
, args
)
375 if __name__
== "__main__" :