Unity 8
 All Classes Functions Properties
create_interactive_notification.py
1 #!/usr/bin/env python
2 from __future__ import print_function
3 
4 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
5 #
6 # Unity Autopilot Test Suite
7 # Copyright (C) 2012-2013 Canonical
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #
22 
23 import argparse
24 from gi.repository import GLib, Notify
25 import signal
26 
27 
28 def action_callback(notification, action_id, data):
29  print(action_id)
30 
31 
32 def quit_callback(notification):
33  loop.quit()
34 
35 
36 if __name__ == '__main__':
37  parser = argparse.ArgumentParser(
38  description='Create an interactive notification'
39  )
40 
41  parser.add_argument(
42  '--summary',
43  help='summary text of the notification',
44  default='Summary'
45  )
46  parser.add_argument(
47  '--body',
48  help='body text of the notification',
49  default='Body'
50  )
51  parser.add_argument(
52  '--icon',
53  help='path to the icon to display',
54  default=None
55  )
56  parser.add_argument(
57  '--action',
58  help='id and label for the callback in the format: id,label',
59  action='append',
60  default=[]
61  )
62  parser.add_argument(
63  '--urgency',
64  help='LOW, NORMAL, CRITICAL',
65  choices=['LOW', 'NORMAL', 'CRITICAL'],
66  default='NORMAL'
67  )
68  parser.add_argument(
69  '--hints',
70  help='list of comma sep options',
71  action='append',
72  default=[]
73  )
74 
75  args = parser.parse_args()
76 
77  Notify.init('Interactive Notifications')
78 
79  notification = Notify.Notification.new(args.summary, args.body, args.icon)
80 
81  for hint in args.hints:
82  key, value = hint.split(',', 1)
83  notification.set_hint_string(key, value)
84 
85  for action in args.action:
86  action_id, action_label = action.split(',', 1)
87  notification.add_action(
88  action_id,
89  action_label,
90  action_callback,
91  None,
92  None
93  )
94 
95  def signal_handler(signam, frame):
96  loop.quit()
97 
98  signal.signal(signal.SIGINT, signal_handler)
99 
100  loop = GLib.MainLoop.new(None, False)
101  notification.connect('closed', quit_callback)
102  notification.show()
103  loop.run()