-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpslide
executable file
·115 lines (100 loc) · 4.74 KB
/
wpslide
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
113
114
115
#!/usr/bin/env python3
from os import listdir
import os.path
from random import shuffle
import urllib.parse
import subprocess
import sys
from time import sleep
from argparse import ArgumentParser
from shlex import quote
DESKTOP = '''[Desktop Entry]
Type=Application
Name=Wallpaper Slideshow
Exec=%s'''
class WPSlide(ArgumentParser):
def __init__(self):
ArgumentParser.__init__(self, prog='wpslide', description='Wallpaper Slideshow')
self.add_argument('-d', '--directory', action='store', type=str, dest='picdir', default=os.path.expanduser('~/Pictures'), metavar='DIR', help='path to directory containing picture files (%(default)s)')
self.add_argument('-t', '--time', action='store', type=int, dest='delay', default=10, metavar='TIME', help='time in minutes between pictures (%(default)smn)')
self.add_argument('--gsettings', action='store', type=str, dest='gsettings', default='/usr/bin/gsettings', metavar='PATH', help='path to the gsettings executable (%(default)s)')
self.add_argument('--notify', action='store', type=str, dest='notify', default='/usr/bin/notify-send', metavar='PATH', help='path to the notify executable (%(default)s)')
self.add_argument('--schema', action='store', type=str, dest='gschema', default='org.gnome.desktop.background', metavar='SCHEMA', help='Gnome property schema for background (%(default)s)')
self.add_argument('--keys', action='store', type=str, dest='gkeys', default='picture-uri,picture-uri-dark', metavar='KEYS', help='Gnome property keys for background (%(default)s)')
self.add_argument('-o', '--output', action='store', type=str, dest='output', default=os.path.expanduser('~/.config/autostart/wpslide.desktop'), metavar='PATH', help='file where to write desktop (%(default)s)')
self.add_argument('--history', action='store', type=str, dest='history', default=os.path.expanduser('~/.config/wpslide/history'), metavar='PATH', help='file containing wallpaper history')
self.add_argument('action', action='store', type=str, choices=('start', 'desktop'), metavar='ACTION', help='start: start slideshow, desktop: write desktop file')
def run(self):
self.args = self.parse_args()
if self.args.action == 'start':
self.start()
elif self.args.action == 'desktop':
self.write_desktop()
else:
raise RuntimeError('action: %s?' % self.args.action)
def start(self):
history = self.read_history()
piclist = listdir(self.args.picdir)
gkeys = tuple(k.strip() for k in self.args.gkeys.split(','))
while True:
shuffle(piclist)
for pic in piclist:
if pic in history:
sys.stderr.write('Skip %s\n' % pic)
continue
abspic = os.path.normpath(os.path.join(self.args.picdir, pic))
picuri = 'file://' + urllib.parse.quote(abspic)
for key in gkeys:
cmdline = [self.args.gsettings, 'set', self.args.gschema, key, picuri]
subprocess.run(cmdline)
cmdline = [self.args.notify, 'Set wallpaper', '-i', abspic, pic]
subprocess.run(cmdline)
self.add_to_history(pic)
sleep(self.args.delay * 60)
history = set()
self.clear_history()
def read_history(self):
if os.path.isfile(self.args.history):
with open(self.args.history) as f:
return set(line.strip() for line in f)
else:
return set()
def add_to_history(self, pic):
with WPSlide._open(self.args.history, mode='a') as f:
f.write(pic)
f.write('\n')
def clear_history(self):
with WPSlide._open(self.args.history):
pass
@staticmethod
def _mapargs():
first = True
picdir = False
for arg in sys.argv:
if first:
yield os.path.abspath(arg)
first = False
continue
if picdir:
yield os.path.abspath(arg)
picdir = False
continue
if arg == '-d' or arg == '--directory':
yield arg
picdir = True
continue
if arg == 'desktop':
yield 'start'
continue
yield arg
@staticmethod
def _open(path, mode='w'):
dirname = os.path.dirname(path)
os.makedirs(dirname, exist_ok=True)
return open(path, mode)
def write_desktop(self):
cmdline = [quote(arg) for arg in WPSlide._mapargs()]
with WPSlide._open(self.args.output) as f:
f.write(DESKTOP % ' '.join(cmdline))
if __name__ == '__main__':
WPSlide().run()