PDA

View Full Version : Moobot module reloading?


jemfinch
07-18-2002, 10:46 AM
Just out of curiosity (and since I'm too lazy to read the source code :)) how is module/callback reloading implemented in moobot?

Jeremy

Strike
07-18-2002, 04:37 PM
I think the answer to your question lies in the fact that we mostly rely upon the imp module. For more details than that, I defer you to Bradmont :)

jemfinch
07-18-2002, 04:52 PM
Hmm. The imp module. That's not in the standard distribution, is it? (Oh, wait, it is. I forgot -- moobot uses functions, not classes for its callbacks. Thus dynamic reloading of modules would work fine. The problem I have is that my callbacks ("modules" in moobot terminology) are classes, and when you reload a module, the classes still stay as they were before the reloading)

I'm whipping up an IRC Bot in Python, and I think I'll probably handle reloading by just using pickle and reload; pickle a class, reload the module it's from, and unpickle the class, and you've got the new reloaded class. I'm looking into other methods, though.

Jeremy

Bradmont
07-18-2002, 08:09 PM
Originally posted by jemfinch
Hmm. The imp module. That's not in the standard distribution, is it? (Oh, wait, it is. I forgot -- moobot uses functions, not classes for its callbacks. Thus dynamic reloading of modules would work fine. The problem I have is that my callbacks ("modules" in moobot terminology) are classes, and when you reload a module, the classes still stay as they were before the reloading)
Jeremy

Not so. Moobot used to use functions for its callbacks, but we changed that at the same time I implemented run-time loading/unloading of modules.

However, one "module" doesn't map exactly to one class, since any given source file can have more than one bot module in it (for example, loading the weblookup module instantiates 11 different classes that each do some sort of net lookups). Each file has an attribute named handler_list, which is a list of strings containing the names of the classes to instantiate. The bot then goes through each of these and uses getattr() to get the class from the module, and then instantiates it. I think I might eventually change that to use a dir() of the module, then check if each element in it is a subclass of MooBotModule (every module is derrived from that) and load any that are.

jemfinch
07-18-2002, 10:58 PM
So, Bradmont or Strike, any pitfalls you want to make sure I avoid while writing a Python IRC bot? It doesn't have to relate to reloading, I'm just curious what lessons you folks have learned that I might be able to make use of.

Jeremy

Strike
07-19-2002, 12:29 AM
Hmmm ... design it yourself? Oh, you're doing that :)

Seriously, most of our big issues is that we didn't really get to exercise our own design because: a) we didn't really know how at the start, and b) we were basing things heavily off one of the classes in irclib. I think if we had to rewrite it all again, it would be much cleaner. Too much of it now is "oh, well, this will work - after all, we'll probably rewrite it if it gets much more complicated than this *chuckle*" And, we keep adding things on :) Luckily, since it's Python, it's still pretty maintainable. It's just not very "pretty" from a correctness standpoint.

So, as far as Python goes, no real major pitfalls to speak of, no.

jemfinch
07-19-2002, 03:38 PM
Well, here's what I have so far. Since I code on a box that's entirely disconnected from the INTARWEB, I don't have anything working right now, and I haven't bothered writing the network driver yet. Soon (hopefully after this weekend) I'll have my interactive command line driver written so I can play with the bot.

It's not very documented, but I'm hoping the code is pretty easy to understand. It does use one function from Twisted (http://www.twistedmatrix.com/), which most of the network stuff and other things will likely be based on. (The function is twisted.python.reflect.prefixedMethodNames, which takes a class object and returns all its methods that start with a given prefix [in this case, the prefix is 'do']). If you have any suggestions feel free to offer them -- some stuff I need to do is already in the TODO file, but there are lots of things floating in my head that I haven't written down. If you have any questions, or if any part of the code is unclear, do ask, because I'll either rewrite the code to be more clear, or I'll comment it so it becomes clear.

If you see any bugs, you better let me know (or else!!!) :-D

Jeremy