~elementary-os/elementaryos/os-patch-notify-osd-precise

« back to all changes in this revision

Viewing changes to tests/notifyosd.py

  • Committer: Sergey "Shnatsel" Davidoff
  • Date: 2012-06-18 21:08:31 UTC
  • Revision ID: shnatsel@gmail.com-20120618210831-g6k5y7vecjdylgic
Initial import, version 0.9.34-0ubuntu2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
import sys
 
3
import time
 
4
from optparse import OptionParser
 
5
 
 
6
import gtk
 
7
import pynotify
 
8
 
 
9
ICON_ONLY_HINT   = "x-canonical-private-icon-only"
 
10
APPEND_HINT      = "x-canonical-append"
 
11
SYNCHRONOUS_HINT = "x-canonical-private-synchronous"
 
12
VALUE_HINT       = "value"
 
13
 
 
14
def create_gauge_notification(title, icon, value):
 
15
        n = pynotify.Notification(title, "", icon)
 
16
        n.set_hint_string(SYNCHRONOUS_HINT, "")
 
17
        n.set_hint_int32(VALUE_HINT, value)
 
18
        return n
 
19
 
 
20
def create_icon_only_notification(title, icon):
 
21
        n = pynotify.Notification(title, "", icon)
 
22
        n.set_hint_string(ICON_ONLY_HINT, "")
 
23
        return n
 
24
 
 
25
USAGE = \
 
26
"""%prog [options] <title> [<body-text>]
 
27
 
 
28
If body-text is "-" %prog will display the content of stdin.
 
29
"""
 
30
 
 
31
def main ():
 
32
        if not pynotify.init("notifyosd"):
 
33
                return 1
 
34
 
 
35
        parser = OptionParser()
 
36
        parser.usage = USAGE
 
37
 
 
38
        parser.add_option("-i", "--icon",  dest="icon",
 
39
                help = "Name of the icon to show")
 
40
 
 
41
        parser.add_option("--icon-data",   dest="icon_data",
 
42
                help = "Load icon data from a custom file")
 
43
 
 
44
        parser.add_option("-v", "--value", dest="value",
 
45
                help = "Start in value mode and display the percentage VALUE in a gauge")
 
46
 
 
47
        parser.add_option("--icon-only",   dest="icon_only",
 
48
                help = "Only show icon, ignoring body",
 
49
                action="store_true", default=False)
 
50
 
 
51
        (options, args) = parser.parse_args()
 
52
 
 
53
        if len(args) == 0:
 
54
                parser.print_usage()
 
55
                return 1
 
56
        title = args[0]
 
57
 
 
58
        if len(args) > 1:
 
59
                if args[1] == "-":
 
60
                        body = sys.stdin.read()
 
61
                else:
 
62
                        body = " ".join(args[1:])
 
63
        else:
 
64
                body = ""
 
65
 
 
66
        if options.value:
 
67
                if body:
 
68
                        print "Note: ignoring body in value mode"
 
69
                n = create_gauge_notification(title, options.icon, int(options.value))
 
70
        elif options.icon_only:
 
71
                if body:
 
72
                        print "Note: ignoring body in icon_only mode"
 
73
                if not options.icon:
 
74
                        print "Error: icon name is missing"
 
75
                        return 1
 
76
                n = create_icon_only_notification(title, options.icon)
 
77
        else:
 
78
                n = pynotify.Notification(title, body, options.icon)
 
79
 
 
80
        if options.icon_data:
 
81
                pixbuf = gtk.gdk.pixbuf_new_from_file(options.icon_data)
 
82
                n.set_icon_from_pixbuf(pixbuf)
 
83
 
 
84
        n.show ()
 
85
 
 
86
if __name__ == "__main__":
 
87
        sys.exit (main ())