# HG changeset patch # User Mikael Berthe # Date 1214076940 -7200 # Node ID 5ee5101decd0da180f37d2f0c6e047f557cc2541 # Parent cf31fa2e0bbc5a745f5c87391c6a329a300e2769 Coding style changes diff -r cf31fa2e0bbc -r 5ee5101decd0 mcevent.py --- a/mcevent.py Sat Jun 21 21:15:00 2008 +0200 +++ b/mcevent.py Sat Jun 21 21:35:40 2008 +0200 @@ -5,8 +5,15 @@ # Copyright (C) 2007, 2008 Mikael Berthe "McKael" # # This script is provided under the terms of the GNU General Public License, -# see the file COPYING in the root mcabber source directory. +# see the file COPYING in this directory. # +""" +This script reads mcabber event lines from the standard input +and creates events accordingly. Examples: +STATUS O user2@domain.org +MSG IN user@domain.org +UNREAD 1 +""" import sys, getopt import os @@ -15,7 +22,7 @@ CONFFILE = "mcevent.cfg" # Default option values -opt = { +OPT = { 'use_notify': 0, 'use_voice': 0, 'use_sound': 1, @@ -26,19 +33,19 @@ NOTIFY_TIMEOUT = 4000 -contact_map = { } +CONTACT_MAP = { } # Nickname used for the notification -contact_custom_msg = { } +CONTACT_CUSTOM_MSG = { } # Message used for the notification -online_alerts = { } +ONLINE_ALERTS = { } # 0: disabled 1: normal notification 2: permanent notify box -voicemap = { } +VOICEMAP = { } # Specify the name pronounced by espeak -blacklist = { } +BLACKLIST = { } # No notification for these JIDs CMD_ESPEAK = "/usr/bin/espeak" @@ -46,6 +53,9 @@ ENCODING = '' def init_notify(): + """ + Initialize the pynotify subsystem. + """ import locale global ENCODING, NOTIFY_LOADED pynotify.init('mcnotify') @@ -53,55 +63,61 @@ NOTIFY_LOADED = True def read_conf_from_file(): + """ + Read the configuration file. + """ import ConfigParser config = ConfigParser.ConfigParser() config.read(CONFFILE) - contact_map.clear() - contact_custom_msg.clear() - online_alerts.clear() - voicemap.clear() - blacklist.clear() + CONTACT_MAP.clear() + CONTACT_CUSTOM_MSG.clear() + ONLINE_ALERTS.clear() + VOICEMAP.clear() + BLACKLIST.clear() if config.has_option("Notifications", "notify"): - opt['use_notify'] = int(config.get("Notifications", "notify")) + OPT['use_notify'] = int(config.get("Notifications", "notify")) if config.has_option("Notifications", "voice"): - opt['use_voice'] = int(config.get("Notifications", "voice")) + OPT['use_voice'] = int(config.get("Notifications", "voice")) if config.has_option("Notifications", "sound"): - opt['use_sound'] = int(config.get("Notifications", "sound")) + OPT['use_sound'] = int(config.get("Notifications", "sound")) if config.has_option("Notifications", "short_nick"): - opt['short_nick'] = int(config.get("Notifications", "short_nick")) + OPT['short_nick'] = int(config.get("Notifications", "short_nick")) if config.has_option("Notifications", "unread_file"): - opt['unread_file'] = config.get("Notifications", "unread_file") + OPT['unread_file'] = config.get("Notifications", "unread_file") if config.has_option("Notifications", "snd_cmd_msg_in"): - opt['snd_cmd_msg_in'] = config.get("Notifications", "snd_cmd_msg_in") + OPT['snd_cmd_msg_in'] = config.get("Notifications", "snd_cmd_msg_in") if config.has_section("Contacts"): for cid in config.options("Contacts"): - contact_map[cid] = config.get("Contacts", cid) + CONTACT_MAP[cid] = config.get("Contacts", cid) if config.has_section("Contact_Customized_Messages"): for cid in config.options("Contact_Customized_Messages"): - contact_custom_msg[cid] = config.get("Contact_Customized_Messages", + CONTACT_CUSTOM_MSG[cid] = config.get("Contact_Customized_Messages", cid) if config.has_section("Alerts"): for cid in config.options("Alerts"): - online_alerts[cid] = int(config.get("Alerts", cid)) + ONLINE_ALERTS[cid] = int(config.get("Alerts", cid)) if config.has_section("Voicemap"): for cid in config.options("Voicemap"): - voicemap[cid] = config.get("Voicemap", cid) + VOICEMAP[cid] = config.get("Voicemap", cid) if config.has_section("Blacklist"): for cid in config.options("Blacklist"): - blacklist[cid] = int(config.get("Blacklist", cid)) + BLACKLIST[cid] = int(config.get("Blacklist", cid)) - if opt['use_notify'] and not NOTIFY_LOADED: + if OPT['use_notify'] and not NOTIFY_LOADED: init_notify() def say(buddy, text): + """ + Create a subprocess and run a speech synthesizer. + """ import subprocess p = subprocess.Popen(CMD_ESPEAK, stdin=subprocess.PIPE, close_fds=True) child_stdin = p.stdin @@ -109,6 +125,9 @@ child_stdin.close() def notify(buddy, msg, timeout): + """ + Create a pynotify popup. + """ msgbox = pynotify.Notification(unicode(buddy, ENCODING), unicode(msg, ENCODING)) msgbox.set_timeout(timeout) @@ -116,21 +135,30 @@ msgbox.show() def get_nick(jid): - if jid in contact_map: - buddy = contact_map[jid] + """ + Return the nick of the given contact (JID). + """ + if jid in CONTACT_MAP: + buddy = CONTACT_MAP[jid] else: buddy = jid - if opt['short_nick'] and '@' in buddy: + if OPT['short_nick'] and '@' in buddy: buddy = buddy[0:buddy.index('@')] return buddy def is_blacklisted(jid): - if jid in blacklist and blacklist[jid]: + """ + Return True if the given contcat (JID) is blacklisted. + """ + if jid in BLACKLIST and BLACKLIST[jid]: return True return False def process_line(args): + """ + Parse the provided line and run events. + """ argn = len(args) if argn < 2: @@ -155,10 +183,10 @@ return buddy = get_nick(jid) - if jid in contact_custom_msg: - msg = contact_custom_msg[jid] - elif 'default' in contact_custom_msg: - msg = contact_custom_msg['default'] + if jid in CONTACT_CUSTOM_MSG: + msg = CONTACT_CUSTOM_MSG[jid] + elif 'default' in CONTACT_CUSTOM_MSG: + msg = CONTACT_CUSTOM_MSG['default'] else: msg = 'sent you a message.' @@ -166,20 +194,20 @@ textmsg = None if filename and os.path.exists(filename): - f = file(filename) - textmsg = f.read() + fileh = file(filename) + textmsg = fileh.read() - if opt['use_notify']: + if OPT['use_notify']: if not textmsg: textmsg = msg notify(buddy, textmsg, NOTIFY_TIMEOUT) - if opt['use_sound']: - os.system(opt['snd_cmd_msg_in'] + '> /dev/null 2>&1') + if OPT['use_sound']: + os.system(OPT['snd_cmd_msg_in'] + '> /dev/null 2>&1') - if opt['use_voice'] and not is_blacklisted(jid): - if jid in voicemap: - buddy = voicemap[jid] + if OPT['use_voice'] and not is_blacklisted(jid): + if jid in VOICEMAP: + buddy = VOICEMAP[jid] say(buddy, msg) if filename and os.path.exists(filename): @@ -187,15 +215,15 @@ elif event == 'STATUS': jid = arg2 - if arg1 == 'O' and jid in online_alerts: - alert_type = online_alerts[jid] + if arg1 == 'O' and jid in ONLINE_ALERTS: + alert_type = ONLINE_ALERTS[jid] if alert_type > 0: - if opt['use_sound']: - os.system(opt['snd_cmd_msg_in'] + '> /dev/null 2>&1') + if OPT['use_sound']: + os.system(OPT['snd_cmd_msg_in'] + '> /dev/null 2>&1') buddy = get_nick(jid) - if opt['use_notify']: + if OPT['use_notify']: if alert_type == 1: timeout = NOTIFY_TIMEOUT else: @@ -205,17 +233,17 @@ if filename and os.path.exists(filename): os.remove(filename) - if opt['use_voice']: - if jid in voicemap: - buddy = voicemap[jid] + if OPT['use_voice']: + if jid in VOICEMAP: + buddy = VOICEMAP[jid] say(buddy, "is online now.") elif event == 'UNREAD': # arg1 is the number of unread buffers - if opt['unread_file'] != '': - fileHandle = open(opt['unread_file'], 'w') - fileHandle.write(arg1) - fileHandle.close() + if OPT['unread_file'] != '': + fileh = open(OPT['unread_file'], 'w') + fileh.write(arg1) + fileh.close() ##### MAIN #####