Browse Source

allow --set-desktop from cron on linux (currently supports gnome desktops only)

master
Niklas Rosenstein 2 years ago
parent
commit
11b5b6ca41
No known key found for this signature in database GPG Key ID: 6D269B33D25F6C6
  1. 1
      .gitignore
  2. 20
      src/wpx/setwp/linux.py

1
.gitignore

@ -1,6 +1,7 @@
# Python
.venv
*.egg-info
*.py[cod]
# VSCode
.vscode

20
src/wpx/setwp/linux.py

@ -25,12 +25,32 @@ from . import SetWallpaperOptions
from gi.repository import Gio
import os
import logging
import subprocess
SCHEMA = 'org.gnome.desktop.background'
PICTURE_URI = 'picture-uri'
def _ensure_dbus_session_address_var():
"""
In order to set the wallpaper from a limited set of environment variables, as
is the case when running wpx from cron, find the DBUS_SESSION_BUS_ADDRESS
value and set it.
Thanks to https://askubuntu.com/a/743024 for the solution.
"""
if not os.getenv('DBUS_SESSION_BUS_ADDRESS'):
script = (
'set -e\n'
'PID=$(pgrep --euid $(id --real --user) gnome-session | head -n1)\n'
'grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-')
output = subprocess.check_output(['bash', '-c', script]).decode().strip().strip('\x00')
os.environ['DBUS_SESSION_BUS_ADDRESS'] = output.strip()
def set_wallpaper(options: SetWallpaperOptions):
_ensure_dbus_session_address_var()
if options.desktop:
path = 'file://' + os.path.abspath(options.path)
settings = Gio.Settings.new(SCHEMA)

Loading…
Cancel
Save