Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 78 lines (66 sloc) 2.135 kB
a21017a @akkana randomline: choose a random line from the input. Useful for things
authored
1 #! /usr/bin/env python
2
3 # Pick a new random background
6f8d861 @akkana Fix Usage comment
authored
4 # usage: randombg [directory]
a21017a @akkana randomline: choose a random line from the input. Useful for things
authored
5 # Default directory is ~/Images/Backgrounds/$dpy_width
6
7 import sys, os, subprocess
8 import random
9
bc5b9f8 @akkana Walk recursively through files -- don't just take top level.
authored
10 def recursive_read(rootdir):
11 file_list = []
12 for root, dirs, files in os.walk(rootdir):
13 for filename in files:
14 # Exclude files without extensions, like Tags.
d7fb97c @akkana Don't use Tags.anything as an image name
authored
15 if filename.startswith("Tags"):
16 continue
17 if '.' not in filename:
18 continue
19 file_list.append(os.path.join(rootdir, root, filename))
bc5b9f8 @akkana Walk recursively through files -- don't just take top level.
authored
20 return file_list
21
22 def set_random_bg(bgdir, arg):
23 images = recursive_read(bgdir)
24 if not images:
25 print "No images in", bgdir
26 sys.exit(1)
27 img = random.choice(images)
28 print "Setting background to", img
29 subprocess.call(['hsetroot', arg, img])
a21017a @akkana randomline: choose a random line from the input. Useful for things
authored
30
31 # If the dir is specified explicitly, go ahead and do it.
bc5b9f8 @akkana Walk recursively through files -- don't just take top level.
authored
32 if len(sys.argv) > 1:
a21017a @akkana randomline: choose a random line from the input. Useful for things
authored
33 set_random_bg(sys.argv[1], '-fill')
34
35 # Find the current resolution:
36 fp = os.popen("xdpyinfo")
bc5b9f8 @akkana Walk recursively through files -- don't just take top level.
authored
37 while True:
a21017a @akkana randomline: choose a random line from the input. Useful for things
authored
38 line = fp.readline().strip()
bc5b9f8 @akkana Walk recursively through files -- don't just take top level.
authored
39 if line[0:11] == 'dimensions:':
a21017a @akkana randomline: choose a random line from the input. Useful for things
authored
40 parts = line[12:].split()[0].split('x')
41 width = int(parts[0])
42 height = int(parts[1])
43 break
44 fp.close()
bc5b9f8 @akkana Walk recursively through files -- don't just take top level.
authored
45 if not width or not height:
a21017a @akkana randomline: choose a random line from the input. Useful for things
authored
46 print "Couldn't find screen dimensions!"
47 sys.exit(1)
48
49 basedir = os.path.expanduser("~/Images/Backgrounds")
50 bgdir = os.path.join(basedir, '%dx%d' % (width, height))
51
52 # If the exact right directory exists, use it.
bc5b9f8 @akkana Walk recursively through files -- don't just take top level.
authored
53 if os.path.exists(bgdir):
a21017a @akkana randomline: choose a random line from the input. Useful for things
authored
54 print bgdir, "exists: using it"
55 set_random_bg(bgdir, '-fill')
6cb7ea2 @akkana Don't keep looking for a resolution match if we already found an exac…
authored
56 sys.exit(0)
a21017a @akkana randomline: choose a random line from the input. Useful for things
authored
57
58 # We don't have images at exactly the right resolution.
59 # Try to find the closest match:
60 print "Looking for the closest match"
61 mindiff = 99999
62 bgdir = None
bc5b9f8 @akkana Walk recursively through files -- don't just take top level.
authored
63 for d in os.listdir(basedir):
64 try:
a21017a @akkana randomline: choose a random line from the input. Useful for things
authored
65 parts = d.split('x')
bc5b9f8 @akkana Walk recursively through files -- don't just take top level.
authored
66 if len(parts) == 2:
a21017a @akkana randomline: choose a random line from the input. Useful for things
authored
67 w = int(parts[0])
68 h = int(parts[1])
69 diff = abs(w - width) + abs(h - height)
bc5b9f8 @akkana Walk recursively through files -- don't just take top level.
authored
70 if diff < mindiff:
a21017a @akkana randomline: choose a random line from the input. Useful for things
authored
71 mindiff = diff
72 bgdir = os.path.join(basedir, d)
bc5b9f8 @akkana Walk recursively through files -- don't just take top level.
authored
73 except:
a21017a @akkana randomline: choose a random line from the input. Useful for things
authored
74 continue
75
76 set_random_bg(bgdir, '-fill')
77
Something went wrong with that request. Please try again.