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
|
#!/usr/bin/env python
import sys, os
import argparse, json
import random, subprocess, signal, time
"""
" CLI
"""
parser = argparse.ArgumentParser(
prog='wallpaper',
description='Cycle through wallpapers'
)
parser.add_argument(
'--config',
required=True,
help='{<weight>: { find: <path>, exclude: <path> }, <weight>:...}'
)
parser.add_argument(
'--interval',
default=600
)
args = parser.parse_args()
config = json.loads(args.config)
"""
" Program
"""
pid_running = -1
while True:
probability_number = random.randrange(start=1, stop=100, step=1)
# build "find" command based on args.config
# see https://stackoverflow.com/questions/16489449/select-element-from-array-with-probability-proportional-to-its-value/16490300#16490300
find_cmd = ''
accumulative = 0
for weight, wallpaper_path in config.items():
lower_bound = accumulative
accumulative += int(weight)
upper_bound = accumulative
if lower_bound <= probability_number and probability_number <= upper_bound:
find_cmd = 'find "{find}" -type f'.format(find=wallpaper_path['find'])
if 'exclude' in wallpaper_path.keys():
find_cmd += ' -not -path "{exclude}"'.format(exclude=wallpaper_path['exclude'])
break
# run swaybg
swaybg_cmd = f'swaybg -i $({find_cmd} | shuf -n1) -m fill &'
subprocess.run(swaybg_cmd, shell=True)
# wait until new instance of swaybg has booted up properly
time.sleep(5)
# kill the previous process
# the new swaybg process takes its place automatically
if pid_running != -1:
os.kill(pid_running, signal.SIGTERM)
# set pid for next iteration
pid_running = int(subprocess.check_output(['pidof', 'swaybg']))
# wait $INTERVAL seconds
time.sleep(int(args.interval))
|