File: autokey-run

package info (click to toggle)
autokey 0.95.10-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,092 kB
  • sloc: python: 9,372; xml: 2,741; javascript: 249; sh: 24; makefile: 4
file content (79 lines) | stat: -rw-r--r-- 2,928 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3

# -*- coding: utf-8 -*-
# -*- mode: python -*-

# Copyright (C) 2011 Chris Dekter
# Copyright (C) 2017 Thomas Hess
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys
import dbus
import argparse


def generate_argument_parser():
    description = "Run an AutoKey function by sending a command to a running AutoKey instance over dbus."

    argument_parser = argparse.ArgumentParser(description=description)
    mode_group = argument_parser.add_mutually_exclusive_group(required=True)
    mode_group.add_argument("-s", "--script",
                            action="store_const",
                            const="script",
                            dest="mode",
                            help="Run a script")
    mode_group.add_argument("-p", "--phrase",
                            action="store_const",
                            const="phrase",
                            dest="mode",
                            help="Paste a phrase")
    mode_group.add_argument("-f", "--folder",
                            action="store_const",
                            const="folder",
                            dest="mode",
                            help="Show a folder popup menu")
    argument_parser.add_argument("name",
                                 help="The name of the script, phrase or folder.")
    return argument_parser


if __name__ == "__main__":

    parser = generate_argument_parser()
    arguments = parser.parse_args()

    bus = dbus.SessionBus()
    try:
        dbus_service = bus.get_object("org.autokey.Service", "/AppService")
    except dbus.DBusException as e:
        print("AutoKey must be running to use autokey-run.\n", file=sys.stderr)
        print(str(e), file=sys.stderr)
        sys.exit(1)

    if arguments.mode == "script":
        dbus_function = dbus_service.run_script
    elif arguments.mode == "phrase":
        dbus_function = dbus_service.run_phrase
    elif arguments.mode == "folder":
        dbus_function = dbus_service.run_folder
    else:
        print("BUG: Unknown run mode encountered: {}".format(arguments.mode), file=sys.stderr)
        sys.exit(1)

    try:
        dbus_function(arguments.name, dbus_interface="org.autokey.Service")
    except dbus.DBusException as e:
        print(e.get_dbus_message(), file=sys.stderr)
        sys.exit(1)