Purple exclamation mark.svg Planning the future of Botwiki! - Help us bring Botwiki up to date, contribute to our strategy discussion, add bot scripts, and contribute manuals, guides, and tutorials! Almost anything related to bots, particularly those used to edit mediawiki, is welcome.

Red exclamation mark.svg UNABLE TO EDIT? - We've experienced attacks by spambots lately and now require you to confirm your e-mail before you can edit (go to your preferences, enter an e-mail address, and request a confirmation e-mail, then go to your e-mail and click on the confirmation link). We also require new accounts to make a few edits and wait a few minutes before before you can create a page; however, if this is a problem contact us in #botwiki and we can manually confirm your account. Sorry for the inconvenience.

Python:BotSottile/reload.py

From Botwiki
Jump to: navigation, search
#!/usr/bin/env python
"""
reload.py - Reload Modules Dynamically
Author: Sean B. Palmer, inamidst.com - FiloSottile, filosottile@wikimedia
About: http://inamidst.com/phenny/ - http://botwiki.sno.cc/wiki/Python:BotSottile
"""
 
import re, sys, irc, time, urllib, os, time
 
def f_reload(self, origin, match, text): 
   name = match.group(1)
   module = getattr(__import__('modules.' + name), name)
   reload(module)
 
   if hasattr(module, '__file__'): 
      import os.path, time
      mtime = os.path.getmtime(module.__file__)
      modified = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(mtime))
   else: modified = 'unknown'
 
   self.register(vars(module))
   self.bindrules()
 
   config = self.config
   nickname = config.nick
   datadir = config.datadir
   self.config = reload(config)
   self.config.nick = nickname
   self.config.datadir = datadir
   t = time.localtime()
   for f in self.logz.values():
      f.close()
   self.logz = {}
   for chan in self.chans_with_flag('log'):
      path = os.path.join(config.datadir, 'logs', chan)
      if not os.path.exists(path): os.mkdir(path)
      self.logz[chan] = file(os.path.join(path, '%d-%d-%d %d-%d.txt' % (t[0], t[1], t[2], t[3], t[4])), 'w')
 
   msg = '%r (version: %s)' % (module, modified)
   self.msg(origin.sender, origin.nick + ': ' + msg)
f_reload.rule = (['reload'], r"(.*)")
 
def f_download(self, origin, match, text):
   import config
   name = match.group(1).strip()
   modules = ('stuff', 'reload', 'seen', 'timing', 'thanks', 'help', 'rejoin', 'ops', 'admin', 'thesaurus', 'validate', 'ip', 'upload')
   base_modules = ('irc', 'config', 'phenny', 'starter', 'botti', 'cron')
   if name in modules or name in base_modules:
      url = 'http://botwiki.sno.cc/w/index.php?title=Python:BotSottile/' + name + '.py&action=raw'
      if name in modules: path = os.path.join(config.datadir, 'modules', name + '.py')
      elif name in base_modules: path = os.path.join(config.datadir, name + '.py')
   else: return
   url = urllib.URLopener().open(url).read().replace('<source lang=' + '"python">\n', '')
   f = file(path, 'w')
   f.write(url)
   f.close()
   self.msg(origin.sender, origin.nick + ': download del modulo ' + name + ' riuscito!')
f_download.rule = (['download'], r"(.*)")
f_download.thread = True
 
#
Personal tools
Share