]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/commitdiff
Add ability to remove profile
authorSimon Wydooghe <wydooghe.simon@gmail.com>
Sun, 27 Mar 2016 17:18:01 +0000 (19:18 +0200)
committerSimon Wydooghe <wydooghe.simon@gmail.com>
Sun, 27 Mar 2016 17:18:01 +0000 (19:18 +0200)
This commit adds profile removal ability.
You can use '-r' or '--remove'.

Following checks are done:
* not a virtual profile
* profile can be found in profiles.keys()

Also added the remove option to the bash completion (untested though,
using zsh and quite unfamiliar with it).

autorandr.py
contrib/bash_completion/autorandr

index 596d3ffb504d20f04d3cbad2dba2956efe660ddf..dbcf8753d2d08bf16f93aa425abc38936b53e064 100755 (executable)
@@ -33,6 +33,7 @@ import posix
 import re
 import subprocess
 import sys
+import shutil
 
 from collections import OrderedDict
 from distutils.version import LooseVersion as Version
@@ -53,6 +54,7 @@ Usage: autorandr [options]
 -h, --help              get this small help
 -c, --change            reload current setup
 -s, --save <profile>    save your current setup to profile <profile>
+-r, --remove <profile>  remove profile <profile>
 -l, --load <profile>    load profile <profile>
 -d, --default <profile> make profile <profile> the default profile
 --skip-options <option> comma separated list of xrandr arguments (e.g. "gamma")
@@ -731,7 +733,7 @@ def exec_scripts(profile_path, script_name, meta_information=None):
 
 def main(argv):
     try:
-       options = dict(getopt.getopt(argv[1:], "s:l:d:cfh", [ "dry-run", "change", "default=", "save=", "load=", "force", "fingerprint", "config", "debug", "skip-options=", "help" ])[0])
+        options = dict(getopt.getopt(argv[1:], "s:r:l:d:cfh", [ "dry-run", "change", "default=", "save=", "remove=", "load=", "force", "fingerprint", "config", "debug", "skip-options=", "help" ])[0])
     except getopt.GetoptError as e:
         print("Failed to parse options: {0}.\n"
               "Use --help to get usage information.".format(str(e)),
@@ -791,6 +793,21 @@ def main(argv):
         print("Saved current configuration as profile '%s'" % options["--save"])
         sys.exit(0)
 
+    if "-r" in options:
+        options["--remove"] = options["-r"]
+    if "--remove" in options:
+        if options["--remove"] in ( x[0] for x in virtual_profiles ):
+            raise AutorandrException("Cannot remove profile '%s':\nThis configuration name is a reserved virtual configuration." % options["--remove"])
+        if options["--remove"] not in profiles.keys():
+            raise AutorandrException("Cannot remove profile '%s':\nThis profile does not exist." % options["--remove"])
+        try:
+            profile_folder = os.path.join(profile_path, options["--remove"])
+            shutil.rmtree(profile_folder)
+        except Exception as e:
+            raise AutorandrException("Failed to remove profile '%s'" % (options["--remove"],), e)
+        print("Removed profile '%s'" % options["--remove"])
+        sys.exit(0)
+
     if "-h" in options or "--help" in options:
         exit_help()
 
index 7a8b81b5dc4d0714619c5e3bbdd760d78ba20dd3..a106b7e2ff3bb3ed82edf3a7f31571dacd2cb189 100644 (file)
@@ -8,8 +8,8 @@ _autorandr ()
        cur="${COMP_WORDS[COMP_CWORD]}"
        prev="${COMP_WORDS[COMP_CWORD-1]}"
 
-       opts="-h -c -s -l -d"
-       lopts="--help --change --save --load --default --force --fingerprint --config --dry-run"
+       opts="-h -c -s -r -l -d"
+       lopts="--help --change --save --remove --load --default --force --fingerprint --config --dry-run"
        if [ -d ~/.autorandr ]; then
                prfls="`find ~/.autorandr/* -maxdepth 1 -type d -printf '%f\n'`"
        elif [ -d ~/.config/autorandr ]; then
@@ -35,7 +35,7 @@ _autorandr ()
                esac
 
        case "${prev}" in
-               -l|--load|-d|--default)
+               -r|--remove|-l|--load|-d|--default)
                        COMPREPLY=( $( compgen -W "${prfls}" -- $cur ) )
                        return 0
                        ;;