def _handle_get_all_configs(self, req: Request): """ Responds with the names and descriptions of all available configs :param req: empty Request :return: None """ try: self._validator.validate(req.get_payload(), "api_empty_request") except ValidationError: self._respond_with_error(req, "ValidationError", f"Request validation error at '{ApiURIs.config_storage_get_all.uri}'") return manager = ClientConfigManager() config_names = manager.get_config_names() all_configs = {} self._logger.info("fetching all configs") for config in config_names: if not config == "Example": try: conf = manager.get_config(config) all_configs[config] = conf["description"] except ConfigDoesNotExistException: self._logger.error("congratz, something went wrong, abort, abort, return to the moon base") pass else: pass payload = {"configs": all_configs} req.respond(payload)
def _select_config(self) -> Optional[dict]: manager = ClientConfigManager() config_names = manager.get_config_names() config_data: Optional[dict] = None while not config_data: config_index = select_option(config_names, "which config to write", "Quit") if config_index == -1: return None config_data = manager.get_config(config_names[config_index]) if not config_data: response = ask_for_continue( "Config file could either not be loaded, isn no valid json file or" "no valid config. Try again?") if not response: return None else: continue return config_data
def debug_config_doesnt_exist(config_manager: ClientConfigManager): try: config_manager.delete_config_file(CONFIG_SAVE["name"]) except ConfigDoesNotExistException: pass yield CONFIG_SAVE try: config_manager.delete_config_file(CONFIG_SAVE["name"]) except ConfigDoesNotExistException: pass
def _handle_delete_config(self, req: Request): """ Deletes the config for a given name, if there is no config, an error is returned :param req: Request containing name of the config :return: None """ try: self._validator.validate(req.get_payload(), "api_config_delete_get") except ValidationError: self._respond_with_error(req, "ValidationError", f"Request validation error at '{ApiURIs.config_storage_delete.uri}'") return name = req.get_payload()["name"] manager = ClientConfigManager() try: manager.delete_config_file(name) except ConfigDoesNotExistException as err: self._respond_with_error(req=req, err_type="ConfigDoesNotExistException", message=err.args[0]) return self._respond_with_status(req, True, "Config was deleted successfully")
def _handle_save_config(self, req: Request): """ Saves the given config or overwrites an already existing config :param req: Request containing the config :return: None """ try: self._validator.validate(req.get_payload(), "api_config_save") except ValidationError: self._respond_with_error(req, "ValidationError", f"Request validation error at '{ApiURIs.config_storage_save.uri}'") return config = req.get_payload()["config"] overwrite = req.get_payload()["overwrite"] manager = ClientConfigManager() try: manager.write_config(config, overwrite=overwrite) except ConfigAlreadyExistsException as err: self._respond_with_error(req=req, err_type="ConfigAlreadyExistsException", message=err.args[0]) return self._respond_with_status(req, True, "Config was saved successfully")
def _handle_get_config(self, req: Request): """ Responds with the config for a given name, if there is none an error is returned :param req: Request containing the name of the requested config :return: None """ try: self._validator.validate(req.get_payload(), "api_config_delete_get") except ValidationError: self._respond_with_error(req, "ValidationError", f"Request validation error at '{ApiURIs.config_storage_get.uri}'") return try: name = req.get_payload()["name"] config = ClientConfigManager().get_config(name) payload = {"config": config} req.respond(payload) except ConfigDoesNotExistException as err: self._respond_with_error(req=req, err_type="ConfigDoesNotExistException", message=err.args[0])
def _write_config(self): manager = ClientConfigManager() config_names = manager.get_config_names() config_path: Optional[str] config_data: Optional[dict] = None while not config_data: config_index = select_option(config_names, "which config to write", "Quit") if config_index == -1: return config_data = manager.get_config(config_names[config_index]) if not config_data: response = ask_for_continue( "Config file could either not be loaded, isn no valid json file or" "no valid config. Try again?") if not response: return else: continue print(f"Loaded config '{config_data['name']}'") print() print("Writing config:") write_controller = ClientController(self._client_name, self._network) w_system = False w_event = False w_gadget = False has_error = False write_option = select_option([ "Write complete config", "System only", "Gadgets only", "Events only" ], "writing task", "back") if write_option == -1: return elif write_option == 0: w_system = True w_event = True w_gadget = True elif write_option == 1: w_system = True elif write_option == 2: w_gadget = True elif write_option == 3: w_event = True if w_system: print("Writing system config...") try: with LoadingIndicator(): write_controller.write_system_config(config_data["system"]) print("Config was successfully written\n") except NoClientResponseException: print("Received no response to config write request\n") has_error = True except ConfigWriteError: print("Failed to write config on chip\n") has_error = True if w_gadget: print("Writing gadget config...") try: with LoadingIndicator(): write_controller.write_gadget_config( config_data["gadgets"]) print("Config was successfully written\n") except NoClientResponseException: print("Received no response to config write request\n") has_error = True except ConfigWriteError: print("Failed to write config on chip\n") has_error = True if w_event: print("Writing event config...") try: with LoadingIndicator(): write_controller.write_event_config(config_data["events"]) print("Config was successfully written\n") except NoClientResponseException: print("Received no response to config write request\n") has_error = True except ConfigWriteError: print("Failed to write config on chip\n") has_error = True if not has_error: print("All writing efforts were successful\n") else: print("Completed with errors\n")
def debug_config(config_manager: ClientConfigManager, debug_config_doesnt_exist: dict): config_manager.write_config(CONFIG_SAVE, overwrite=True) yield CONFIG_SAVE
def config_manager(): manager = ClientConfigManager() yield manager
def test_client_controller_write_event_config(controller: ClientController, connector: DummyNetworkConnector, manager: ClientConfigManager): working_config = manager.get_config(WORKING_CONFIG_NAME)["events"] config_write_test_helper(controller.write_event_config, working_config, connector)