def __init__(self, loop=asyncio.get_event_loop()): # basic variables self.loop = loop self.start_time = time.time() self.running = True # future which will be called when the bot stops self.stopped_future = asyncio.Future(loop=self.loop) # stores each bot server connection self.connections = [] # set up config self.config = Config(self) logger.debug("Config system initialised.") # setup db db_config = self.config.get('database') db_host = db_config.get('host', 'localhost') db_port = db_config.get('port', 6379) db_database = db_config.get('database', 0) logger.info("Connecting to redis at {}:{}/{}".format( db_host, db_port, db_database)) self.db = redis.StrictRedis(host=db_host, port=db_port, db=db_database) logger.debug("Database system initialised.") # Bot initialisation complete logger.debug("Bot setup completed.") # create bot connections self.create_connections() self.plugin_manager = PluginManager(self)
def __init__(self, loop=asyncio.get_event_loop()): # basic variables self.loop = loop self.start_time = time.time() self.running = True # future which will be called when the bot stops self.stopped_future = asyncio.Future(loop=self.loop) # stores each bot server connection self.connections = [] # for plugins self.logger = logger # for plugins to abuse self.memory = collections.defaultdict() # declare and create data folder self.data_dir = os.path.abspath('data') if not os.path.exists(self.data_dir): logger.debug("Data folder not found, creating.") os.mkdir(self.data_dir) # set up config self.config = Config(self) logger.debug("Config system initialised.") # set values for reloading self.plugin_reloading_enabled = self.config.get("reloading", {}).get( "plugin_reloading", False) self.config_reloading_enabled = self.config.get("reloading", {}).get( "config_reloading", True) # this doesn't REALLY need to be here but it's nice self.user_agent = self.config.get( 'user_agent', 'CloudBot/3.0 - CloudBot Refresh ' '<https://github.com/CloudBotIRC/CloudBot/>') # setup db db_path = self.config.get('database', 'sqlite:///cloudbot.db') self.db_engine = create_engine(db_path) self.db_factory = sessionmaker(bind=self.db_engine) self.db_session = scoped_session(self.db_factory) self.db_metadata = MetaData() # set botvars.metadata so plugins can access when loading botvars.metadata = self.db_metadata logger.debug("Database system initialised.") # Bot initialisation complete logger.debug("Bot setup completed.") # create bot connections self.create_connections() if self.plugin_reloading_enabled: self.reloader = PluginReloader(self) self.plugin_manager = PluginManager(self)
def test_save(tmp_path): config_file = tmp_path / "config.json" config_file.write_text("{}", encoding="utf-8") bot = MockBot() config = Config(bot, filename=str(config_file)) config["foo"] = "bar" config.save_config() assert config_file.read_text(encoding="utf-8") == '{\n "foo": "bar"\n}'
def test_missing_config(tmp_path, capsys, mock_sleep): config_file = tmp_path / "config.json" bot = MockBot() with pytest.raises(SystemExit): Config(bot, filename=str(config_file)) data = capsys.readouterr() assert data.out == ("No config file found! Bot shutting down in five " "seconds.\n" "Copy 'config.default.json' to " "'config.json' for defaults.\n" "For help, " "see htps://github.com/TotallyNotRobots/CloudBot. " "Thank you for " "using CloudBot!\n")
def __init__(self, loop=asyncio.get_event_loop()): if bot.get(): raise ValueError("There seems to already be a bot running!") bot.set(self) # basic variables self.base_dir = Path().resolve() self.loop = loop self.start_time = time.time() self.running = True self.clients = {} # future which will be called when the bot stopsIf you self.stopped_future = async_util.create_future(self.loop) # stores each bot server connection self.connections = KeyFoldDict() # for plugins self.logger = logger # for plugins to abuse self.memory = collections.defaultdict() # declare and create data folder self.data_dir = os.path.abspath('data') if not os.path.exists(self.data_dir): logger.debug("Data folder not found, creating.") os.mkdir(self.data_dir) # set up config self.config = Config(self) logger.debug("Config system initialised.") # set values for reloading reloading_conf = self.config.get("reloading", {}) self.plugin_reloading_enabled = reloading_conf.get( "plugin_reloading", False) self.config_reloading_enabled = reloading_conf.get( "config_reloading", True) # this doesn't REALLY need to be here but it's nice self.user_agent = self.config.get( 'user_agent', 'CloudBot/3.0 - CloudBot Refresh ' '<https://github.com/CloudBotIRC/CloudBot/>') # setup db db_path = self.config.get('database', 'sqlite:///cloudbot.db') self.db_engine = create_engine(db_path) self.db_factory = sessionmaker(bind=self.db_engine) self.db_session = scoped_session(self.db_factory) self.db_metadata = database.metadata self.db_base = declarative_base(metadata=self.db_metadata, bind=self.db_engine) self.db_executor_pool = ExecutorPool(50, max_workers=1, thread_name_prefix='cloudbot-db') # set botvars so plugins can access when loading database.base = self.db_base logger.debug("Database system initialised.") # Bot initialisation complete logger.debug("Bot setup completed.") self.load_clients() # create bot connections self.create_connections() self.observer = Observer() if self.plugin_reloading_enabled: self.plugin_reloader = PluginReloader(self) if self.config_reloading_enabled: self.config_reloader = ConfigReloader(self) self.plugin_manager = PluginManager(self)