コード例 #1
0
def test_select_option(monkeypatch):
    monkeypatch.setattr('builtins.input', lambda: '0')
    result = select_option(TEST_OPTION_LIST)
    assert result == 0

    inputs = iter([len(TEST_OPTION_LIST), "-1", "yolo", "1"])
    monkeypatch.setattr('builtins.input', lambda: next(inputs))
    result = select_option(TEST_OPTION_LIST, "an option")
    assert result == 1

    monkeypatch.setattr('builtins.input', lambda: str(len(TEST_OPTION_LIST)))
    result = select_option(TEST_OPTION_LIST, back_option="back")
    assert result == -1
コード例 #2
0
    def _write_config(self):

        config = self._select_config()
        if not config:
            return

        client_names = self._bridge_connector.get_client_names()
        task_option = select_option(client_names + ["<Serial Connection>"],
                                    "where to write the config", "Back")
        if task_option == -1:
            return
        elif task_option == len(client_names):
            serial_port = self._select_serial_port()
            if not serial_port:
                print("\nNo serial port available, quitting.")
                return
            try:
                self._bridge_connector.write_config_to_serial_client(
                    config, serial_port, self._software_write_callback)
                print("\nConfig writing successful.")
            except ConfigWritingFailedException:
                print("\nFailed to write the config to the client.")
        else:
            client_name = client_names[task_option]
            try:
                self._bridge_connector.write_config_to_network_client(
                    client_name, config, self._software_write_callback)
                print("\nConfig writing successful.")
            except ConfigWritingFailedException:
                print("\nFailed to write the config to the client.")
コード例 #3
0
    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
コード例 #4
0
 def _run_tasks(self):
     title_list = [x for (x, y) in self._tasks]
     method_list = [y for (x, y) in self._tasks]
     while True:
         print()
         task_option = select_option(title_list, "what to do", "Quit")
         if task_option == -1:
             return
         method_list[task_option]()
コード例 #5
0
 def _select_serial_port(self) -> Optional[str]:
     bridge_serial_ports = self._bridge_connector.get_serial_ports()
     if bridge_serial_ports:
         sel_ser_port = select_option(bridge_serial_ports,
                                      "Serial Port for Upload", "Back")
         if sel_ser_port == -1:
             return None
         return bridge_serial_ports[sel_ser_port]
     return None
コード例 #6
0
    def _select_task(self) -> bool:
        task_option = select_option(
            ["Overwrite EEPROM", "Write Config", "Reboot"], "what to do",
            "Quit")

        if task_option == -1:
            return False

        elif task_option == 0:  # Overwrite EEPROM
            self._erase_config()
            return True

        elif task_option == 1:  # Write Config
            self._write_config()
            return True

        elif task_option == 2:  # Reboot
            self._reboot_client()
            print("Reconnecting might be required.")
            return True
コード例 #7
0
    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
コード例 #8
0
    def _write_software(self):
        selected_branch = select_option(
            _default_git_branches + ["Enter own name"], "Branch", "Back")

        if selected_branch == -1:
            return
        elif selected_branch == len(_default_git_branches):
            selected_branch_name = input("Enter branch name:\n")
        else:
            selected_branch_name = _default_git_branches[selected_branch]
        print(f"Writing software branch '{selected_branch_name}':")

        serial_port = self._select_serial_port()
        if not serial_port:
            print("\nNo serial port available, quitting.")
            return

        try:
            self._bridge_connector.write_software_to_client(
                selected_branch_name, serial_port,
                self._software_write_callback)
            print("\nSoftware writing successful.")
        except SoftwareWritingFailedException:
            print("\nFailed to write software to client.")
コード例 #9
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")
コード例 #10
0
                        help='Activates the Debug Logger',
                        action='store_true')
    ARGS = parser.parse_args()

    print("Launching...")

    if ARGS.debug:
        print("Activated Debug Logging")
        logging.basicConfig(
            level=logging.DEBUG,
            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    if ARGS.connection_mode:
        connection_mode = ARGS.connection_mode
    else:
        connection_mode = select_option(
            ["Bridge", "Direct", "Upload Software"], "Connection Mode", "Quit")
        if connection_mode == -1:
            sys.exit(0)

    if connection_mode == 0:  # Bridge

        socket_client: socket = None

        if ARGS.bridge_addr:
            bridge_addr = ARGS.bridge_addr
        else:
            bridge_addr = input("Please enter server address: ")

        if ARGS.bridge_socket_port:
            socket_port = ARGS.bridge_socket_port
        else: