pyosd: ein einfaches beispiel
nach einer langen pause mal wieder ein beitrag.
Ich hab gerade pythons On-Screen-Display Bibliothek pyosd entdeckt. Eine simple Schnittstelle zur X On-Screen Display library, die es ermöglicht jede beliebige Nachrichten im X display einzublenden.
Eine gute Anwendung um pyosd auszuprobieren ist mcabber. Mcabber, ein consolenbasierter jabber client, der mittlerweile in Version 0.9.9 vorliegt bietet schon seit Version 0.7.1 die Möglichkeit events wie einkommende Nachrichten oder Statuswechsel der buddies an einen externen Befehl weiterzuleiten. Mcabber liefert z.B. ein einfaches Shellskript aus, das jede Nachricht mit einem nervigen PING ankündigen kann oder ein Skript, dass osd_cat verwendet um die Nachricht auf dem X Display anzukündigen.
Aus der mcabberrc:
1 # External command for events You can specify a script or
2 # process to be launched when an event occurs. Set
3 # ‘events_ignore_active_window’ to 1 if you don’t want the
4 # script to be run for a message to the current active
5 # window (default: 0).
6 #
7 # If ‘event_log_files’ is set, a file is created and
8 # contains the body of the message (incoming messages only);
9 # the file name is the last parameter. If you enable this,
10 # you can specify the directory mcabber will use to create
11 # these messages with the ‘event_log_dir’ variable (default
12 # is the system temp dir, or MCABBERTMPDIR environment
13 # variable). Please note that mcabber won’t delete these
14 # files, it’s your script’s job.
15 #
16 # The command is called the following way:
17 # $events_command MSG IN jabber@id [file]
18 # (when receiving a message)
19 # $events_command MSG OUT jabber@id
20 # (when sending a message)
21 # $events_command MSG MUC room_id [file]
22 # (when receiving a MUC message)
23 # $events_command STATUS X jabber@id
24 # (new buddy status is X)
25 # See sample script in contrib/ directory.
26 set events_command = ~/.mcabber/eventcmd.py
~
Das osd_cat Shellskript lässt sich schnell mit python und pyosd ersetzen:
1 #!/usr/bin/python
2 #—————————————
3 #
4 #
5 # Filename: eventcmd.py
6 # Description: OSD for mcabber events
7 # Date: 10/22/08
8 #—————————————
9
10
11 import pyosd
12 import sys
13 import os
14 from random import randint
15
16 # use xfontsel to select a font
17 FONT = "-*-fixed-*-*-*-*-15-*-*-*-c-90-*-*"
18 COLOR_MSG="darkgreen"
19 COLOR_STATUS="yellow"
20 POSITION = pyosd.POS_BOT
21 ALIGN = pyosd.ALIGN_RIGHT
22 TIMEOUT = 10
23 MAXOFFSET = 100
24 # maximum number of characters to display
25 # 0: nothing from the message is displayed
26 MAXCHARS = 0
27
28
29 MSG_FROM = ‘msg from %s‘
30 MSG_SAYS = ‘%s: %s‘
31 OFFLINE = ‘%s offline‘
32 ONLINE = ‘%s online‘
33
34
35 def display(str, color):
36 “’ Show OSD message “’
37
38 ods = pyosd.osd(font="fixed", timeout=TIMEOUT)
39
40 ods.set_colour(color)
41 ods.set_font(FONT)
42 ods.set_align(ALIGN)
43 ods.set_pos(POSITION)
44
45 # change position randomly, so that
46 # new messages don’t cover old ones.
47 offset = randint(0,MAXOFFSET)
48 ods.set_vertical_offset(offset)
49
50 ods.display(str)
51 ods.wait_until_no_display()
52
53
54
55 def main(e, w, b, f=None):
56 “’ identify mcabber message “’
57
58
59 if e == "MSG":
60 if w == "IN":
61 if f and MAXCHARS:
62 print "using line"
63 line = file(filename).readline().rstrip(‘\n‘)
64 file.close
65
66 if len(line) >= MAXCHARS:
67 line = line[:maxchars] + "…"
68
69 msg = MSG_SAYS % (b, line )
70 os.remove(filename)
71 else:
72 msg = MSG_FROM % b
73
74 display(msg, COLOR_MSG)
75
76 elif e == "STATUS":
77 if w == "_":
78 display(OFFLINE % b, COLOR_STATUS)
79 elif w == "O":
80 display(ONLINE % b, COLOR_STATUS)
81
82
83
84 if __name__ == ‘__main__‘:
85
86 event = sys.argv[1]
87 what = sys.argv[2]
88 buddy = sys.argv[3]
89
90
91 if len(sys.argv) > 4:
92 filename = sys.argv[4]
93 main(event, what, buddy, filename)
94 else:
95 main(event, what, buddy)
96
97 sys.exit(0)

Eine Antwort schreiben