Skip to content

Instantly share code, notes, and snippets.

@lenerd
Created August 20, 2015 13:52
Show Gist options
  • Save lenerd/940214f719d399479f45 to your computer and use it in GitHub Desktop.
Save lenerd/940214f719d399479f45 to your computer and use it in GitHub Desktop.
Changes between xrandr scripts.
#!/usr/bin/env python3
"""
Simple program to execute scripts from a given set one after another.
Written to easily change between xrandr configurations.
Point SCRIPT_DIR to the location of the scripts to be executed.
The name of the last executed script will be saved in SCRIPT_DIR/current.
With each call this program executes the next scripts in alphabetical order,
until one exits successfully (returns 0).
If an argument is given, it will be interpretated as the name of a script to be
executed.
"""
# The MIT License (MIT)
# Copyright (c) 2015 Lennart Braun <lenerd@posteo.de>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import sys
import subprocess
__author__ = "Lennart Braun"
__email__ = "lenerd@posteo.de"
__copyright__ = "Copyright (C) 2015 Lennart Braun"
__license__ = "MIT"
__version__ = "1.0"
SCRIPT_DIR = '~/.screenlayout'
def main():
directory = os.path.expanduser(SCRIPT_DIR)
if not os.path.exists(directory):
print("error: directory '{}' not found".format(directory),
file=sys.stderr)
sys.exit(1)
files = sorted(f for f in os.listdir(directory) if f.endswith('.sh'))
if not files:
print("error: no shell scripts found", file=sys.stderr)
sys.exit(1)
# file containing last called script
cur = os.path.join(directory, 'current')
# script given as argument
if len(sys.argv) > 1:
target = sys.argv[1]
if target not in files:
print("error: '{}' not found".format(target), file=sys.stderr)
sys.exit(1)
ret = subprocess.call(os.path.join(directory, target),
stderr=subprocess.DEVNULL)
if ret:
sys.exit(1)
with open(cur, 'w') as f:
f.write(script)
if os.path.exists(cur):
with open(cur, 'r') as f:
current = f.read()
else:
current = ''
if current in files:
start = files.index(current)
else:
start = -1
for i in range(1, len(files) + 1):
index = (start + i) % len(files)
script = files[index]
ret = subprocess.call(os.path.join(directory, script),
stderr=subprocess.DEVNULL)
if not ret:
break
print(script)
# TODO: notification
with open(cur, 'w') as f:
f.write(script)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment