Patch (session_change_hook)
Tuomas Härkönen <[email protected]> Tue, 20 Jul 2004 11:04:53 +0300
| Newsgroups | gmane.comp.games.mud.client.lyntin |
|---|---|
| Message-ID | <[email protected]> |
Hi,
I picked up Lyntin a few days ago and so far I've liked it very much. It
also gave me a good excuse to learn Python, which has been on my agenda
for quite some years now. *grin*
As I was working on a module, I stumbled into a problem I was not able
to find a good solution to: I needed to hook a function to the event of
current session changing. I use this to change Lyntin's title bar to
reflect the session that's currently active.
I think I could've managed something to this effect by hooking some of
the from_* and to_* hooks, but it did not seem like an elegant solution.
So, I added a new hook -- "session_change_hook". It's spammed when
Engine._current_session is changed. It gets two arguments, which are
Session instances of the new and previous session.
I'm posting this here in case someone else finds it useful. You are free
to use this code as you wish, although I wouldn't mind being credited if
you distribute it further. :)
Note that I've only used Python for a few days, so I just *might* have
made an error or two.
By the way, the Windows distribution of 4.1.1 (executable package) comes
with hooks.py. Confused me a little bit as I was working on this.
Tuomas
diff -ur lyntin\engine.py lyntin.session_change_hook\engine.py
--- lyntin\engine.py Mon May 10 16:59:02 2004
+++ lyntin.session_change_hook\engine.py Tue Jul 20 10:51:06 2004
@@ -97,6 +97,17 @@
then Lyntin shuts down.
Arg mapping: {}
+
+
+X{session_change_hook}::
+
+ This hook gets spammed whenever the current session changes.
+
+ Arg mapping: { "new": session.Session, "previous": session.Session }
+
+ new - the session that is being changed to
+
+ previous - the session that was previously the current session
"""
import Queue, thread, sys, traceback, os.path
from threading import Thread
@@ -457,7 +468,13 @@
if self._sessions.has_key(ses):
input = mem.split(" ", 1)
if len(input) < 2:
+ # Store previous session for the hook
+ previous_session = self._current_session
self._current_session = self._sessions[ses]
+ # Spam the session_change_hook as the session changes
+ exported.hook_spam("session_change_hook", \
+ {"new": self._current_session, \
+ "previous": previous_session})
exported.write_message("%s now current session." % ses)
else:
self.handleUserData(input[1], internal=1,
session=self._sessions[ses])
@@ -607,6 +624,7 @@
@param name: the name of the session to switch to
@type name: string
"""
+ previous_session = self._current_session
if name == '':
keys = self._sessions.keys()
@@ -628,6 +646,10 @@
exported.write_error("No session of that name.")
return
+ # Spam the session_change_hook as the session changes
+ exported.hook_spam("session_change_hook", \
+ {"new": self._current_session, \
+ "previous": previous_session})
exported.write_message("Switching to session '%s'." %
self._current_session.getName())
def writeSession(self, message):
diff -ur lyntin\exported.py lyntin.session_change_hook\exported.py
--- lyntin\exported.py Wed Mar 31 19:21:48 2004
+++ lyntin.session_change_hook\exported.py Tue Jul 20 10:52:22 2004
@@ -429,11 +429,15 @@
def set_current_session(ses):
"""
Changes the current session to another session.
+ This also spams session_change_hook.
@param ses: the session instance to set the current session to
@type ses: session.Session
"""
+ previous_session = myengine._current_session
myengine._current_session = ses
+ hook_spam("session_change_hook", {"new": myengine._current_session, \
+ "previous": previous_session})
def get_num_errors():
"""
-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop
FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=4721&alloc_id=10040&op=click