def __init__(self): self.plugs = {} # dict with the plugins self.reloadlock = thread.allocate_lock() # persisted data for deny of plugins (blacklist) self.plugdeny = Persist(datadir + os.sep + 'plugdeny', init=False) if not self.plugdeny.data: self.plugdeny.data = [] # persisted data for allowing of plugins (whitelist) self.plugallow = Persist(datadir + os.sep + 'plugallow', init=False) if not self.plugallow.data: self.plugallow.data = [] self.avail = [] # available plugins self.ondisk = [] # plugisn available for reload self.initcalled = [] # plugins which init functions are called self.overloads = {} # plugins to overload
class Plugins(object): """ hold all the plugins. """ def __init__(self): self.plugs = {} # dict with the plugins self.reloadlock = thread.allocate_lock() # persisted data for deny of plugins (blacklist) self.plugdeny = Persist(datadir + os.sep + 'plugdeny', init=False) if not self.plugdeny.data: self.plugdeny.data = [] # persisted data for allowing of plugins (whitelist) self.plugallow = Persist(datadir + os.sep + 'plugallow', init=False) if not self.plugallow.data: self.plugallow.data = [] self.avail = [] # available plugins self.ondisk = [] # plugisn available for reload self.initcalled = [] # plugins which init functions are called self.overloads = {} # plugins to overload def __getitem__(self, item): """ return plugin. """ if self.plugs.has_key(item): return self.plugs[item] else: return None def get(self, item, attr): """ get attribute of plugin. """ if self.plugs.has_key(item): return getattr(self.plugs[item], attr) def whatperms(self): """ return what permissions are possible. """ result = [] # search RE callbacks before the commands for i in rebefore.whatperms(): if not i in result: result.append(i) # search the commands for i in cmnds.whatperms(): if not i in result: result.append(i) # search RE callbacks after commands for i in reafter.whatperms(): if not i in result: result.append(i) result.sort() return result def exist(self, name): """ see if plugin <name> exists. """ if self.plugs.has_key(name): return 1 def disable(self, name): """ prevent plugins <name> to be loaded. """ try: config['loadlist'].remove(name) config.save() self.plugdeny.data.append(name) self.plugdeny.save() except: pass def enable(self, name): """ enable plugin <name>. """ try: if name not in config['loadlist']: config['loadlist'].append(name) config.save() self.plugdeny.data.remove(name) self.plugdeny.save() except: pass def plugsizes(self): """ call the size() function in all plugins. """ reslist = [] for i, j in self.plugs.iteritems(): try: reslist.append("%s: %s" % (i, j.size())) except AttributeError: pass return reslist def list(self): """ list of registered plugins. """ self.avail.sort() return self.avail def plugimport(self, mod, name): """ import a plugin. """ if name in config['loadlist']: self.reload(mod, name) else: self.reload(mod, name, enable=False) if self.plugs.has_key(name): return self.plugs[name] def regplugin(self, mod, name): """ register plugin. """ name = name.lower() mod = mod.lower() modname = mod + '.' + name # see if plugin is in deny if name in self.avail: rlog(9, 'plugins', '%s already registered' % name) return if name in self.plugdeny.data: rlog(10, 'plugins', '%s.%s in deny .. not loading' % \ (mod, name)) return 0 if config.has_key('loadlist') and name not in config[ 'loadlist'] and 'gozerplugs' in modname and name not in self.plugallow.data: rlog(9, 'plugins', 'not loading %s.%s' % (mod, name)) return 0 # if plugin is already registered unload it if self.plugs.has_key(name): rlog(10, 'plugins', 'overloading %s plugin with %s version' \ % (name, mod)) self.unloadnosave(name) # create the plugin data dir if not os.path.isdir(datadir + os.sep + 'plugs'): os.mkdir(datadir + os.sep + 'plugs') if not os.path.isdir(datadir + os.sep + 'plugs' + os.sep + name): os.mkdir(datadir + os.sep + 'plugs' + os.sep + name) # import the plugin plug = self.plugimport(mod, name) rlog(0, 'plugins', "%s.%s registered" % (mod, name)) return plug def showregistered(self): """ show registered plugins. """ self.avail.sort() rlog(10, 'plugins', 'registered %s' % ' .. '.join(self.avail)) self.overload() def regdir(self, dirname, exclude=[]): """ register a directory. """ threads = [] plugs = [] for plug in plugnames(dirname): if plug in exclude or plug.startswith('.'): continue try: self.regplugin(dirname, plug) plugs.append(plug) except: handle_exception() self.ondisk.extend(plugs) return plugs def regcore(self): """ register core plugins. """ self.plugdeny.init([]) self.plugallow.init([]) avail = [] plugs = gozer_import('gozerbot.plugs') for i in plugs.__plugs__: if i not in avail: try: self.regplugin('gozerbot.plugs', i) except Exception, ex: handle_exception() avail.append(i) self.ondisk.extend(avail)
class Plugins(object): """ hold all the plugins. """ def __init__(self): self.plugs = {} # dict with the plugins self.reloadlock = thread.allocate_lock() # persisted data for deny of plugins (blacklist) self.plugdeny = Persist(datadir + os.sep + 'plugdeny', init=False) if not self.plugdeny.data: self.plugdeny.data = [] # persisted data for allowing of plugins (whitelist) self.plugallow = Persist(datadir + os.sep + 'plugallow', init=False) if not self.plugallow.data: self.plugallow.data = [] self.avail = [] # available plugins self.ondisk = [] # plugisn available for reload self.initcalled = [] # plugins which init functions are called self.overloads = {} # plugins to overload def __getitem__(self, item): """ return plugin. """ if self.plugs.has_key(item): return self.plugs[item] else: return None def get(self, item, attr): """ get attribute of plugin. """ if self.plugs.has_key(item): return getattr(self.plugs[item], attr) def whatperms(self): """ return what permissions are possible. """ result = [] # search RE callbacks before the commands for i in rebefore.whatperms(): if not i in result: result.append(i) # search the commands for i in cmnds.whatperms(): if not i in result: result.append(i) # search RE callbacks after commands for i in reafter.whatperms(): if not i in result: result.append(i) result.sort() return result def exist(self, name): """ see if plugin <name> exists. """ if self.plugs.has_key(name): return 1 def disable(self, name): """ prevent plugins <name> to be loaded. """ try: config['loadlist'].remove(name) config.save() self.plugdeny.data.append(name) self.plugdeny.save() except: pass def enable(self, name): """ enable plugin <name>. """ try: if name not in config['loadlist']: config['loadlist'].append(name) config.save() self.plugdeny.data.remove(name) self.plugdeny.save() except: pass def plugsizes(self): """ call the size() function in all plugins. """ reslist = [] for i, j in self.plugs.iteritems(): try: reslist.append("%s: %s" % (i, j.size())) except AttributeError: pass return reslist def list(self): """ list of registered plugins. """ self.avail.sort() return self.avail def plugimport(self, mod, name): """ import a plugin. """ if name in config['loadlist']: self.reload(mod, name) else: self.reload(mod, name, enable=False) if self.plugs.has_key(name): return self.plugs[name] def regplugin(self, mod, name): """ register plugin. """ name = name.lower() mod = mod.lower() modname = mod + '.' + name # see if plugin is in deny if name in self.avail: rlog(9, 'plugins', '%s already registered' % name) return if name in self.plugdeny.data: rlog(10, 'plugins', '%s.%s in deny .. not loading' % \ (mod, name)) return 0 if config.has_key('loadlist') and name not in config['loadlist'] and 'gozerplugs' in modname and name not in self.plugallow.data: rlog(9, 'plugins', 'not loading %s.%s' % (mod, name)) return 0 # if plugin is already registered unload it if self.plugs.has_key(name): rlog(10, 'plugins', 'overloading %s plugin with %s version' \ % (name, mod)) self.unloadnosave(name) # create the plugin data dir if not os.path.isdir(datadir + os.sep + 'plugs'): os.mkdir(datadir + os.sep + 'plugs') if not os.path.isdir(datadir + os.sep + 'plugs' + os.sep + name): os.mkdir(datadir + os.sep + 'plugs' + os.sep + name) # import the plugin plug = self.plugimport(mod, name) rlog(0, 'plugins', "%s.%s registered" % (mod, name)) return plug def showregistered(self): """ show registered plugins. """ self.avail.sort() rlog(10, 'plugins', 'registered %s' % ' .. '.join(self.avail)) self.overload() def regdir(self, dirname, exclude=[]): """ register a directory. """ threads = [] plugs = [] for plug in plugnames(dirname): if plug in exclude or plug.startswith('.'): continue try: self.regplugin(dirname, plug) plugs.append(plug) except: handle_exception() self.ondisk.extend(plugs) return plugs def regcore(self): """ register core plugins. """ self.plugdeny.init([]) self.plugallow.init([]) avail = [] plugs = gozer_import('gozerbot.plugs') for i in plugs.__plugs__: if i not in avail: try: self.regplugin('gozerbot.plugs', i) except Exception, ex: handle_exception() avail.append(i) self.ondisk.extend(avail)