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