def test_ask_for_continue(monkeypatch): monkeypatch.setattr('builtins.input', lambda: 'y') result = ask_for_continue("Please select type 'y'") assert result is True inputs = iter(["", "yolo", "n"]) monkeypatch.setattr('builtins.input', lambda: next(inputs)) result = ask_for_continue("Please select type 'n'") assert result is False
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 _connect_to_client(self): """Scans for clients and lets the user select one if needed and possible.""" while not self._client_name: with LoadingIndicator(): client_id = None client_list = self._scan_for_clients() if len(client_list) == 0: pass elif len(client_list) > 1: client_id = select_option(client_list, "client to connect to") else: client_id = client_list[0] self._client_name = client_id if not self._client_name: response = ask_for_continue( "Could not find any gadget. Try again?") if not response: raise ToolkitException
def upload_software_to_client(serial_port: str) -> bool: print("Uploading Software to Client:") flasher = ChipFlasher() result = flasher.upload_software("develop", serial_port) if result: print("Uploading software was successful") return True else: print("Uploading software failed.") if ask_for_continue("Try again with some extra caution?"): print("Uploading Software to Client:") result = flasher.upload_software("develop", serial_port, clone_new_repository=True) if result: print("Uploading software was successful") return True else: print("Uploading software failed again.") return False else: return False
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")
sys.exit(0) elif to_do == 0: # Override override = True else: config_name = input( "Please enter a ID to identify the config later\n" ) print(f"You entered the following config:\n" f"ID: {config_name}\n" f"IP: {ip}\n" f"Port: {port}\n" f"Username: {username}\n" f"Password: {password}") if not ask_for_continue("Do you want to use it?"): continue try: manager.set_mqtt_config( config_name, { "ip": ip, "port": port, "username": username, "password": password }) except InvalidConfigException: print( "Some of the entered values were illegal, please try again.\n" ) continue