~dcaro/terminator/fix-684340

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
#!/usr/bin/env python
#    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

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

APP_NAME='remotinator'
APP_VERSION='1.0'

COMMANDS={
        'hsplit': ['terminal_hsplit', 'Split the current terminal horizontally'],
        'vsplit': ['terminal_vsplit', 'Split the current terminal vertically'],
        'terminals': ['get_terminals', 'Get a list of all terminals'],
        }

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

    if sys.argv[0].split('/')[-1] == APP_NAME:
        if len(sys.argv) <2 or sys.argv[1] in ['-h', '--help']:
            print "Usage: %s COMMAND" % sys.argv[0]
            print "  Commands:"
            for command in COMMANDS:
                print "    %s : %s" % (command, COMMANDS[command][1])
            sys.exit(1)
        command = sys.argv[1]
    else:
        command = sys.argv[0].split('/')[-1]

    if not command in COMMANDS or not hasattr(ipc, COMMANDS[command][0]):
        err("Unknown command: %s" % command)
        sys.exit(1)

    if not os.environ.has_key('TERMINATOR_UUID'):
        err("$TERMINATOR_UUID is not set. Are you definitely running inside Terminator?")
        sys.exit(1)

    func = getattr(ipc, COMMANDS[command][0])
    func(os.environ['TERMINATOR_UUID'])