~waldner/terminator/plugin-ng

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
80
81
82
83
84
85
86
87
#!/usr/bin/env python2
#    remotinator - send commands to Terminator via DBus
#    Copyright (C) 2006-2010  cmsj@tenshu.net
#
#    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, version 2 only.
#
#    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, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  
#    USA

"""remotinator by Chris Jones <cmsj@tenshu.net>"""

import os
import sys
import argparse

from terminatorlib.util import dbg, err
from terminatorlib.version import APP_VERSION
try:
    from terminatorlib import ipc
except ImportError:
    err('Unable to initialise Terminator remote library. This probably means dbus is not available')
    sys.exit(1)
from terminatorlib.translation import _

APP_NAME='remotinator'

COMMANDS={
    #  Command         uuid req.    Description
    'new_window':       [False, _('Open a new window')],
    'new_tab':          [True,  _('Open a new tab')],
    'hsplit':           [True,  _('Split the current terminal horizontally')],
    'vsplit':           [True,  _('Split the current terminal vertically')],
    'get_terminals':    [False, _('Get a list of all terminals')],
    'get_window':       [True,  _('Get the UUID of a parent window')],
    'get_window_title': [True,  _('Get the title of a parent window')],
    'get_tab':          [True,  _('Get the UUID of a parent tab')],
    'get_tab_title':    [True,  _('Get the title of a parent tab')],
    }

if __name__ == '__main__':
    dbg ("%s starting up, version %s" % (APP_NAME, APP_VERSION))

    command_desc=''
    for command in sorted(COMMANDS.keys()):
        command_desc += "  %-*s  %s %s\n" % (max([len(x) for x in COMMANDS.keys()]),
                                             command,
                                             COMMANDS[command][0] and '*' or ' ',
                                             COMMANDS[command][1])

    # Parse args
    parser = argparse.ArgumentParser(
                formatter_class=argparse.RawDescriptionHelpFormatter,
                usage='%(prog)s command [options]', 
                description=_('Run one of the following Terminator DBus commands:\n\n%s') % (command_desc), 
                epilog=_('* These entries require either TERMINATOR_UUID environment var,\n  or the --uuid option must be used.'))
    parser.add_argument('-u', '--uuid', dest='uuid', type=str, metavar='UUID', default=argparse.SUPPRESS, 
                help=_('Terminal UUID for when not in env var TERMINATOR_UUID'))
    parser.add_argument('command', type=str, nargs=1, choices=sorted(COMMANDS.keys()),
                help=argparse.SUPPRESS)
    parser.add_argument('-v', '--version', action='version', version='%%(prog)s %s' %(APP_VERSION))
    options = vars(parser.parse_args())     # Straight to dict

    # Pull out the command
    command = options['command'][0]
    del options['command']

    func = getattr(ipc, command)

    uuid_required = COMMANDS[command][0]
    if uuid_required:
        uuid = options.get('uuid', os.environ.get('TERMINATOR_UUID'))
        if uuid:
            func(uuid, options)
        else:
            err("$TERMINATOR_UUID is not set, or passed as an option.")
            sys.exit(1)
    else:
        func(options)