Release 1.6.0.
[xtoys] / xmsg.in
CommitLineData
bce8c6ee
MW
1#! @PYTHON@
2### -*-python-*-
3###
4### Report a message to the user
5###
6### (c) 2008 Straylight/Edgeware
7###
8
9###----- Licensing notice ---------------------------------------------------
10###
11### This file is part of the Edgeware X tools collection.
12###
13### X tools is free software; you can redistribute it and/or modify
14### it under the terms of the GNU General Public License as published by
15### the Free Software Foundation; either version 2 of the License, or
16### (at your option) any later version.
17###
18### X tools is distributed in the hope that it will be useful,
19### but WITHOUT ANY WARRANTY; without even the implied warranty of
20### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21### GNU General Public License for more details.
22###
23### You should have received a copy of the GNU General Public License
24### along with X tools; if not, write to the Free Software Foundation,
25### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
27VERSION = '@VERSION@'
28
29###--------------------------------------------------------------------------
30### External dependencies.
31
32import optparse as O
33from sys import stdin, stdout, stderr, exit
34import os as OS
35import errno as E
36
37import xtoys as XT
38GTK = XT.GTK
39
40###--------------------------------------------------------------------------
41### Option parsing.
42
43def parse_args():
44 """
45 Parse the command line, returning a triple (PARSER, OPTS, ARGS).
46 """
47
48 op = XT.make_optparse \
49 ([('E', 'error',
50 {'action': 'store_const', 'dest': 'type', 'const': 'error',
51 'help': "Mark the window as reporting an error."}),
52 ('I', 'informational',
53 {'action': 'store_const', 'dest': 'type', 'const': 'info',
54 'help': "Mark the window as providing information."}),
55 ('Q', 'question',
56 {'action': 'store_const', 'dest': 'type', 'const': 'question',
57 'help': "Mark the window as asking a question."}),
58 ('W', 'warning',
59 {'action': 'store_const', 'dest': 'type', 'const': 'warning',
60 'help': "Mark the window as giving a warning."}),
61 ('d', 'headline',
62 {'dest': 'headline',
63 'help': "Set the window's headline message."}),
64 ('m', 'markup',
65 {'action': 'store_true', 'dest': 'markupp',
66 'help': "Parse message strings for Pango markup."}),
67 ('t', 'title',
68 {'dest': 'title',
69 'help': "Set the window's title string."})],
70 version = VERSION,
71 usage = '%prog [-EIQWm] [-t TITLE] [-d HEADLINE] '
72 'MESSAGE [BUTTONS...]')
73
74 op.set_defaults(title = 'xmsg',
75 type = 'info',
76 headline = None,
77 markupp = False)
78
79 opts, args = op.parse_args()
80 return op, opts, args
81
82###--------------------------------------------------------------------------
83### Main program.
84
85def main():
86 op, opts, args = parse_args()
87 if len(args) == 0:
88 op.print_usage(stderr)
89 exit(1)
90
91 ## Sort out the message.
92 message = args[0]
93 buttons = args[1:]
94 if message.startswith('!'):
95 message = message[1:]
96 elif message == '-':
97 message = stdin.read()
98
99 ## Display it and retrieve and answer.
100 try:
101 msg = XT.Message(title = opts.title,
102 type = opts.type,
103 message = message,
104 headline = opts.headline,
105 buttons = buttons,
106 markupp = opts.markupp)
107 except ValueError, err:
108 op.error(err[0])
109 result = msg.ask()
110
111 ## Done.
112 if result != True:
113 print result
114 exit(0)
115
116if __name__ == '__main__':
117 main()
118
119###----- That's all, folks --------------------------------------------------