Skip to content

Instantly share code, notes, and snippets.

@kotnik
Created October 23, 2014 09:45
Show Gist options
  • Save kotnik/c2ccac0049e13b672fda to your computer and use it in GitHub Desktop.
Save kotnik/c2ccac0049e13b672fda to your computer and use it in GitHub Desktop.
Change background to red on low battery.
#!/usr/bin/env python3
import os
import time
import argparse
from subprocess import check_call
def cli_options():
parser = argparse.ArgumentParser()
parser.add_argument(
"--percentage", "-p", help="battery %% warning", default=8, type=int,
)
parser.add_argument(
"--level-file", "-l", help="battery level info file",
default="/sys/class/power_supply/BAT0/capacity", type=str,
)
parser.add_argument(
"--background", "-b", help="background image",
default="%s/.bg.jpg" % os.getenv("HOME"), type=str,
)
parser.add_argument(
"--warning", "-w", help="warning background image",
default="%s/.bg-warning.jpg" % os.getenv("HOME"), type=str,
)
parser.add_argument(
"--lock-ok", "-o", help="lock file",
default="%s/.bg-ok.lock" % os.getenv("HOME"), type=str,
)
parser.add_argument(
"--lock-warning", "-i", help="lock file",
default="%s/.bg-warning.lock" % os.getenv("HOME"), type=str,
)
parser.add_argument(
"--setter", "-s", help="background setter program",
default="/usr/bin/feh --bg-fill", type=str,
)
return parser.parse_args()
def remove_lock(lock):
try:
os.unlink(lock)
except FileNotFoundError:
pass
def set_wallpaper(setter, bg_file):
setter = setter.split(" ")
setter.append(bg_file)
check_call(setter)
def main():
options = cli_options()
# Reset state.
remove_lock(options.lock_ok)
remove_lock(options.lock_warning)
while True:
with open(options.level_file, "r") as f:
current_level = int(f.read())
if current_level <= options.percentage and \
not os.path.exists(options.lock_warning):
set_wallpaper(options.setter, options.warning)
remove_lock(options.lock_ok)
open(options.lock_warning, "a").close()
elif current_level > options.percentage and \
not os.path.exists(options.lock_ok):
set_wallpaper(options.setter, options.background)
remove_lock(options.lock_warning)
open(options.lock_ok, "a").close()
time.sleep(10)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment