-
Notifications
You must be signed in to change notification settings - Fork 4
/
02_wallpaper_rotator.py
executable file
·112 lines (80 loc) · 2.99 KB
/
02_wallpaper_rotator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
"""
My own wallpaper changer.
I was not satisfied with the existing solutions so I made my own :)
Usage:
======
Simply launch it in the background.
You can also put it among the startup applications.
"""
import os
import sys
import random
from time import sleep
import config as cfg
from wallpapers.helper import gnome
def print_info():
li = [cfg.get_photo_dir_by_key(key) for key in cfg.ROTATOR_CHOICE]
print "# Jabba's Wallpaper Rotator for Gnome v0.2"
print "#", li
print "# initial duration: {0} sec.".format(cfg.DURATION)
class WallpaperPicker:
"""Simple class for collecting images and picking one randomly."""
def __init__(self):
"""To prevent picking the previous image."""
self.prev = None
def collect_images(self):
"""Collect images from _several_ directories."""
li = []
for key in cfg.ROTATOR_CHOICE:
photo_dir = cfg.get_photo_dir_by_key(key)
li.extend([os.path.join(photo_dir, x) for x in os.listdir(photo_dir) if x.lower().endswith('jpg')])
self.images = li
def get_nb_images(self):
"""Number of images."""
return len(self.images)
def get_first_image(self):
"""Get the first image."""
return self.images[0]
def get_random_image(self):
"""Get a random image from the list."""
random.shuffle(self.images)
img = random.choice(self.images)
while img == self.prev:
img = random.choice(self.images)
self.prev = img
return img
def free_memory(self):
"""Don't keep the list in memory while waiting (sleep). After all,
this list will be re-read each time."""
del self.images
def main():
print_info()
wp = WallpaperPicker()
while True:
# if you modify the config file, this script doesn't have to be restarted
reload(cfg)
cfg.self_verify()
# The list of images is re-read in each iteration because Wallpaper Downloader
# is supposed to work parallelly. Thus, newly downloaded images will have a
# chance to appear on the desktop.
wp.collect_images()
nb_images = wp.get_nb_images()
if nb_images == 0:
# there are no images in the directory => do nothing
pass
if nb_images == 1:
# there is only one image => easy choice
wallpaper = wp.get_first_image()
if nb_images > 1:
# there are several images => choose a different image than the previous pick
wallpaper = wp.get_random_image()
gnome.set_wallpaper_image(wallpaper)
wp.free_memory()
try:
sleep(float(cfg.DURATION))
except KeyboardInterrupt:
sys.exit()
#############################################################################
if __name__ == "__main__":
main()