def findPlugins(self): # Find modules using premade .info descriptor files. filelist = os.listdir(self.moduledir) plugindata = {} infos = list(os.path.join(self.moduledir, m) for m in filelist if m.endswith(".info")) log.debug("found infos: %s" % ", ".join(infos)) spec = "conf/plugin.spec" for infofile in infos: info = ConfigurationFile(infofile, configspec=spec) errors = info.isValid() if errors: line = ", ".join(errors) log.error(line) continue geninfo = info["general"] # Get absolute path of module file filename = os.path.abspath( os.path.join(self.moduledir, geninfo["modulefile"])) if os.path.exists(filename): log.debug("adding %s to queue."%filename) plugindata[geninfo['name'].lower()] = PluginTracker(self, info, filename) else: log.warning("could not find module file: %s"%filename) return plugindata
def getConfig(self): if self.info['general']['usesconfig']: if self.info['general']['usesconfspec']: conf = ConfigurationFile('%s/config.conf' % self._datadir, configspec="plugins/%s.spec" % self.info['general']['name'].lower()) errors = conf.isValid() if errors: self.manager.log.error("Error in config for %s:" % self.info['general']['name']) self.manager.log.error('\n'.join(errors)) raise InvalidConfig else: conf = ConfigurationFile('%s/config.conf' % self._datadir) else: raise DoesntUseConfig return conf
def loadConfig(self, filename="conf/socbot.conf", spec="conf/config.spec"): log.info("loading bot config from {0}".format(filename)) self.config = ConfigurationFile(filename, configspec=spec, unrepr=True) errors = self.config.isValid() if errors: log.error("Error in config:") log.error('\n'.join(errors)) return False self.sstate['config'] = self.config for d in self.config['directories'].values(): if not os.path.exists(d): os.makedirs(d) lvl = getattr(logging, self.config['logging']['level']) for l in getLoggers().values(): l.setLevel(lvl) return self.config
class main(object): def __init__(self, sstate): self.sstate = sstate self.sstate["exitcode"] = 0 self.factory = None log.info("base directory is {0}".format(self.sstate["basedir"])) def load(self): if not self.loadConfig(): return False self.loadPlugs() return True def loadConfig(self, filename="conf/socbot.conf", spec="conf/config.spec"): log.info("loading bot config from {0}".format(filename)) self.config = ConfigurationFile(filename, configspec=spec, unrepr=True) errors = self.config.isValid() if errors: log.error("Error in config:") log.error('\n'.join(errors)) return False self.sstate['config'] = self.config for d in self.config['directories'].values(): if not os.path.exists(d): os.makedirs(d) lvl = getattr(logging, self.config['logging']['level']) for l in getLoggers().values(): l.setLevel(lvl) return self.config def loadPlugs(self): log.info("starting pluginmanager and loading plugins") enablestate = ConfigurationFile(self.config['directories']['plugins'] + "/enabled.conf") pm = PluginCore(self.sstate, enablestate, self.config['directories']['plugins']) self.sstate["pluginmanager"] = pm pm.loadPlugins() pm.initPlugins() return pm def run(self): config = self.config['connection'] f = BotFactory(config, self.sstate, self) self.factory = f botService = internet.TCPClient(config["host"], config["port"], f) log.info("Connecting to {0} ({1})".format(config['name'], config["host"])) botService.startService() reactor.addSystemEventTrigger('before', 'shutdown', botService.stopService) reactor.addSystemEventTrigger('before', 'shutdown', self.shutdown) reactor.run() sys.exit(self.sstate['exitcode']) def connectionLost(self, lostconnection): if self.factory.connection or not self.factory.shuttingdown: return reactor.stop() def shutdown(self, msg="Shutdown requested."): try: self.factory.connection.quit(msg) except Exception: pass