Last active
October 24, 2018 08:55
-
-
Save aragaer/8dd3156f70e4253957e43dab5fbbcca2 to your computer and use it in GitHub Desktop.
wall-changer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import os | |
import random | |
import shlex | |
import subprocess | |
_DIR = os.path.expanduser('~/.wall-changer') | |
if not os.path.exists(_DIR): | |
os.mkdir(_DIR) | |
_CURRENT = os.path.join(_DIR, 'current') | |
_REGISTERED = os.path.join(_DIR, 'db') | |
for f in (_CURRENT, _REGISTERED): | |
if not os.path.exists(f): | |
open(f, 'w').close() | |
def set_groups(groups): | |
print("setting groups to", '-'.join(groups)) | |
with open(_CURRENT, 'r') as data: | |
line = data.readline().strip() | |
with open(_CURRENT, 'w') as data: | |
try: | |
_, group, image = line.split(' ', 2) | |
print(groups, group, image, file=data) | |
except ValueError: | |
group = None | |
print(groups, file=data) | |
if group not in groups: | |
change_to_groups(groups) | |
def get_current(): | |
current = None | |
group = None | |
with open(_CURRENT) as data: | |
line = data.readline().strip() | |
try: | |
groups, group, current = line.split(' ', 2) | |
except ValueError: | |
groups = line | |
return groups, group, current | |
def change(): | |
groups, _, current = get_current() | |
change_to_groups(groups, current) | |
def change_to_groups(groups, avoid=None): | |
matching = [] | |
with open(_REGISTERED) as db: | |
for line in db: | |
g, i = line.strip().split(' ', 1) | |
if g in groups and i != avoid and os.path.exists(i[1:-1]): | |
matching.append((g, i)) | |
if not matching: | |
print("No matching wallpapers found for groups", | |
'-'.join(groups)) | |
return | |
result = random.choice(matching) | |
print("Changing wallpaper to", result[1]) | |
with open(_CURRENT, 'w') as current: | |
print(groups, *result, file=current) | |
subprocess.run(shlex.split("feh --bg-fill {}".format(result[1]))) | |
def interactive(): | |
result = subprocess.run("dmenu", | |
stdin=subprocess.DEVNULL, | |
stdout=subprocess.PIPE) | |
groups = result.stdout.decode().strip() | |
if groups: | |
set_groups(groups) | |
else: | |
change() | |
def register(image, group): | |
print("Registering image", image, "as group", group) | |
changed = False | |
with open(_REGISTERED) as db, open(_REGISTERED+'.tmp', 'w') as out: | |
for line in db: | |
g, i = line.strip().split(' ', 1) | |
if i == image: | |
g = group | |
changed = True | |
print(g, i, file=out) | |
if not changed: | |
print(group, image, file=out) | |
os.rename(_REGISTERED+'.tmp', _REGISTERED) | |
def remove(): | |
_, _, current = get_current() | |
with open(_REGISTERED) as db, open(_REGISTERED+'.tmp', 'w') as out: | |
for line in db: | |
g, i = line.strip().split(' ', 1) | |
if i != current: | |
print(g, i, file=out) | |
os.rename(_REGISTERED+'.tmp', _REGISTERED) | |
change() | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
group = parser.add_mutually_exclusive_group() | |
group.add_argument("-c", "--change", action='store_true') | |
group.add_argument("-r", "--register", nargs=2) | |
group.add_argument("-g", "--groups") | |
group.add_argument("--bad", action='store_true') | |
args = parser.parse_args() | |
if args.change: | |
change() | |
elif args.register: | |
image, group = args.register | |
if len(group) != 1 or not group.isdigit(): | |
print("Group must be 0-9") | |
exit(-1) | |
if not os.path.exists(image): | |
print("File '{}' not found".format(image)) | |
exit(-1) | |
register('"{}"'.format(image), group) | |
elif args.groups: | |
set_groups(args.groups) | |
elif args.bad: | |
remove() | |
else: | |
interactive() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment