如何更改此Python脚本以成功设置Ubuntu快捷键?

如何更改此Python脚本以成功设置Ubuntu快捷键?,python,ubuntu,key-bindings,Python,Ubuntu,Key Bindings,我正在尝试让一个Python脚本工作,该脚本旨在设置Ubuntu快捷键。它在Ubuntu 16.10中似乎不起作用,我不确定为什么。此外,我发现很难跟踪命令、字符串、Bash等所需的引号及其各自的转义 脚本的使用方式如下所示: ./set_Ubuntu_shortkey.py \ --command="bash -c \"xvkbd -text \$(date \"

我正在尝试让一个Python脚本工作,该脚本旨在设置Ubuntu快捷键。它在Ubuntu 16.10中似乎不起作用,我不确定为什么。此外,我发现很难跟踪命令、字符串、Bash等所需的引号及其各自的转义

脚本的使用方式如下所示:

./set_Ubuntu_shortkey.py                                                               \
    --command="bash -c \"xvkbd -text \$(date \"+%Y-%m-%dT%H%MZ\" --utc) 2>/dev/null\"" \
    --name="type_time_UTC"                                                             \
    --keys="<Control><Shift>d"

不相关,但
如果有([command,key,name])则为None
没有任何意义
any(…)
返回一个布尔值,布尔值永远不会是
None
@Rawing感谢您的发现。我已将其更新为更合理的版本(
,如果[command,keys,name]
中没有)。
#!/usr/bin/env python

"""
################################################################################
#                                                                              #
# set_Ubuntu_shortkey                                                          #
#                                                                              #
################################################################################

usage:
    program [options]

options:
    -h, --help      display help message
    --version       display version and exit
    --command=TEXT  command
    --keys=TEXT     keys
    --name=TEXT     name
    --debugpassive  display commands without executing
"""

import docopt
import logging
import os
import subprocess
import sys
import time

def main(options):

    command       = options["--command"]
    keys          = options["--keys"]
    name          = options["--name"]
    debug_passive = options["--debugpassive"]

    if None in [command, keys, name]:
        print("insufficient options specified")
        exit()

    print("\nset shortkey:"          )
    print("- name:    " + name       )
    print("- command: " + command    )
    print("- keys:    " + keys + "\n")

    shortkeys_current     = subprocess.check_output([
                               "gsettings",
                               "get",
                               "org.gnome.settings-daemon.plugins.media-keys",
                               "custom-keybindings"
                            ]).decode("utf-8")
    if shortkeys_current.strip() == "@as []":
        shortkeys_current = "[]"
    shortkeys_current     = eval(shortkeys_current)
    index_shortkey_new    = len(shortkeys_current)
    address_shortkey_new  =\
        "/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/" +\
        "custom" + str(index_shortkey_new) + "/"
    shortkeys_current.append(
        address_shortkey_new
    )

    command_list_1 = [
        "gsettings",
        "set",
        "org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:" +\
            address_shortkey_new,
        "name",
        name
    ]
    command_list_2 = [
        "gsettings",
        "set",
        "org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:" +\
            address_shortkey_new,
        "command",
        command
    ]
    command_list_3 = [
        "gsettings",
        "set",
        "org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:" +\
            address_shortkey_new,
        "binding",
        keys
    ]

    if debug_passive:
        print(" ".join(command_list_1))
        print(" ".join(command_list_2))
        print(" ".join(command_list_3))
    else:
        subprocess.Popen(command_list_1)
        subprocess.Popen(command_list_2)
        subprocess.Popen(command_list_3)

if __name__ == "__main__":
    options = docopt.docopt(__doc__)
    if options["--version"]:
        print(version)
        exit()
    main(options)