summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2023-05-17 12:40:48 +0200
committerDaniel Weipert <code@drogueronin.de>2023-10-30 09:52:23 +0100
commit4868e16d65e92aa371e6ef2ae94122ac2dafd23e (patch)
tree66a7acd4538780b08b78681e94e292fc8b0eaec5
parent5a3373ab70f54b8f7a0f1c16b6a80dd4a8e87f1f (diff)
[sway] fix python based wallpapers by being more pythonic
-rwxr-xr-x.config/sway/bin/wallpaper.py38
1 files changed, 23 insertions, 15 deletions
diff --git a/.config/sway/bin/wallpaper.py b/.config/sway/bin/wallpaper.py
index 9784625..1dad045 100755
--- a/.config/sway/bin/wallpaper.py
+++ b/.config/sway/bin/wallpaper.py
@@ -3,7 +3,7 @@
import sys, os
import argparse, json
-import random, subprocess, signal, time
+import random, re, subprocess, time
"""
@@ -18,7 +18,7 @@ parser = argparse.ArgumentParser(
parser.add_argument(
'--config',
required=True,
- help='{<weight>: { find: <path>, exclude: <path> }, <weight>:...}'
+ help='{"<weight>": { "find": "<path>", "exclude": "<regex>" }, "<weight>":...}'
)
parser.add_argument(
@@ -35,13 +35,13 @@ config = json.loads(args.config)
" Program
"""
-pid_running = -1
+running_bg_process = None
while True:
probability_number = random.randrange(start=1, stop=100, step=1)
- # build "find" command based on args.config
+ # select config to use based on args.config
# see https://stackoverflow.com/questions/16489449/select-element-from-array-with-probability-proportional-to-its-value/16490300#16490300
- find_cmd = ''
+ selected_config = {}
accumulative = 0
for weight, wallpaper_path in config.items():
lower_bound = accumulative
@@ -49,27 +49,35 @@ while True:
upper_bound = accumulative
if lower_bound <= probability_number and probability_number <= upper_bound:
- find_cmd = 'find "{find}" -type f'.format(find=wallpaper_path['find'])
+ selected_config = wallpaper_path
+ break
- if 'exclude' in wallpaper_path.keys():
- find_cmd += ' -not -path "{exclude}"'.format(exclude=wallpaper_path['exclude'])
+ # build list of file paths to choose from
+ found_file_paths = []
+ for dirpath, dirnames, filenames in os.walk(os.path.expandvars(wallpaper_path['find'])):
+ for filename in filenames:
+ found_file_paths.append(f'{dirpath}/{filename}')
- break
+ if 'exclude' in selected_config.keys():
+ found_file_paths = list(filter(
+ lambda file_path: (not re.match(os.path.expandvars(wallpaper_path['exclude']), file_path)),
+ found_file_paths
+ ))
# run swaybg
- swaybg_cmd = f'swaybg -i $({find_cmd} | shuf -n1) -m fill &'
- subprocess.run(swaybg_cmd, shell=True)
+ swaybg_cmd = ['swaybg', '-i', random.choice(found_file_paths), '-m', 'fill']
+ replacing_process = subprocess.Popen(swaybg_cmd)
# 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)
+ if running_bg_process:
+ running_bg_process.terminate()
- # set pid for next iteration
- pid_running = int(subprocess.check_output(['pidof', 'swaybg']))
+ # set running process for next iteration
+ running_bg_process = replacing_process
# wait $INTERVAL seconds
time.sleep(int(args.interval))