def handle_settings(parameters):

    def _get_value(parameters2):
        """ Searches for a 'value' parameter in the dictionary. """
        value2 = None
        for key2 in parameters2:
            if str(key2) == 'value':
                value2 = str(parameters2[key2])
        return value2

    message_back = None
    for key in parameters:
        # Compiler
        if str(key) == 'compiler':
            if str(parameters[key]) == "['get']":
                message_back = actions.get_compiler_path()
            elif str(parameters[key]) == "['set']":
                message_back = actions.set_compiler_path()
        # Sketch
        elif str(key) == 'sketch':
            if str(parameters[key]) == "['get']":
                message_back = actions.get_sketch_path()
            elif str(parameters[key]) == "['set']":
                message_back = actions.set_sketch_path()
        # Arduino Board
        elif str(key) == 'board':
            if str(parameters[key]) == "['get']":
                message_back = actions.get_arduino_boards()
            elif str(parameters[key]) == "['set']":
                value = _get_value(parameters)
                value = re.sub(r'^\[\'', '', value)
                value = re.sub(r'\'\]', '', value)
                message_back = actions.set_arduino_board(value)
        # Serial port
        elif str(key) == 'serial':
            if str(parameters[key]) == "['get']":
                message_back = actions.get_serial_ports()
            elif str(parameters[key]) == "['set']":
                value = _get_value(parameters)
                value = re.sub(r'^\[\'', '', value)
                value = re.sub(r'\'\]', '', value)
                message_back = actions.set_serial_port(value)
        # Launch Only Options
        elif str(key) == 'ide':
            if str(parameters[key]) == "['get']":
                message_back = actions.get_load_ide_only()
            elif str(parameters[key]) == "['set']":
                value = _get_value(parameters)
                value = re.sub(r'^\[\'', '', value)
                value = re.sub(r'\'\]', '', value)
                message_back = actions.set_load_ide_only(value)
        # The Value parameter is only used in some cases
        elif str(key) == 'value':
            pass
        # Parameter not recognised
        else:
            print('The "' + str(key) + '" = ' + str(parameters[key]) +
                  ' parameter is not recognised!')
    return message_back
Пример #2
0
def handle_settings(parameters):
    def _get_value(parameters2):
        """ Searches for a 'value' parameter in the dictionary. """
        value2 = None
        for key2 in parameters2:
            if str(key2) == 'value':
                value2 = str(parameters2[key2])
        return value2

    message_back = None
    for key in parameters:
        # Compiler
        if str(key) == 'compiler':
            if str(parameters[key]) == "['get']":
                message_back = actions.get_compiler_path()
            elif str(parameters[key]) == "['set']":
                message_back = actions.set_compiler_path()
        # Sketch
        elif str(key) == 'sketch':
            if str(parameters[key]) == "['get']":
                message_back = actions.get_sketch_path()
            elif str(parameters[key]) == "['set']":
                message_back = actions.set_sketch_path()
        # Arduino Board
        elif str(key) == 'board':
            if str(parameters[key]) == "['get']":
                message_back = actions.get_arduino_boards()
            elif str(parameters[key]) == "['set']":
                value = _get_value(parameters)
                value = re.sub(r'^\[\'', '', value)
                value = re.sub(r'\'\]', '', value)
                message_back = actions.set_arduino_board(value)
        # Serial port
        elif str(key) == 'serial':
            if str(parameters[key]) == "['get']":
                message_back = actions.get_serial_ports()
            elif str(parameters[key]) == "['set']":
                value = _get_value(parameters)
                value = re.sub(r'^\[\'', '', value)
                value = re.sub(r'\'\]', '', value)
                message_back = actions.set_serial_port(value)
        # Launch Only Options
        elif str(key) == 'ide':
            if str(parameters[key]) == "['get']":
                message_back = actions.get_load_ide_only()
            elif str(parameters[key]) == "['set']":
                value = _get_value(parameters)
                value = re.sub(r'^\[\'', '', value)
                value = re.sub(r'\'\]', '', value)
                message_back = actions.set_load_ide_only(value)
        # The Value parameter is only used in some cases
        elif str(key) == 'value':
            pass
        # Parameter not recognised
        else:
            print('The "' + str(key) + '" = ' + str(parameters[key]) +
                  ' parameter is not recognised!')
    return message_back
Пример #3
0
    def test_set_sketch_path_valid(self, mock_isdir):
        """Test set_sketch_path function changes the compiler Setting.

        :param mock_isdir: Mock for os.path.isdir().
        :return: None.
        """
        old_sketch_dir = self.settings.sketch_dir
        new_sketch_dir = os.path.join(self.temp_folder, 'folder')
        self.assertNotEqual(old_sketch_dir, new_sketch_dir)
        mock_isdir.return_value = True

        returned_path = actions.set_sketch_path(new_sketch_dir)

        self.assertEqual(returned_path, new_sketch_dir)
        self.assertEqual(returned_path, self.settings.sketch_dir)
        self.assertNotEqual(returned_path, old_sketch_dir)
        self.assertEqual(self.settings.sketch_dir, new_sketch_dir)
        self.assertNotEqual(self.settings.sketch_dir, old_sketch_dir)
Пример #4
0
    def test_set_sketch_path_invalid(self, mock_isdir):
        """Test invalid directory sent in set_sketch_path function.

        Tests that the set_sketch_path function does not edit the settings if
        the sent path is not valid.

        :param mock_isdir: Mock for os.path.isdir().
        :return: None.
        """
        old_sketch_dir = self.settings.sketch_dir
        new_sketch_dir = os.path.join(self.temp_folder, 'folder')
        self.assertNotEqual(old_sketch_dir, new_sketch_dir)
        mock_isdir.return_value = False

        returned_path = actions.set_sketch_path(new_sketch_dir)

        self.assertEqual(returned_path, old_sketch_dir)
        self.assertEqual(returned_path, self.settings.sketch_dir)
        self.assertNotEqual(returned_path, new_sketch_dir)
        self.assertEqual(self.settings.sketch_dir, old_sketch_dir)
        self.assertNotEqual(self.settings.sketch_dir, new_sketch_dir)
Пример #5
0
def handler_settings_update_individual(name):
    """Handle the POST setting requests.

    Error codes:
    63 - Unexpected setting type to update.
    64 - Unable to parse sent JSON.
    65 - JSON received does not have 'new_value' key.
    66 - Invalid value.
    67 - New value could not be set.

    :param name: Setting value to retrieve.
    :return: JSON string with the formed response.
    """
    response_dict = {'response_type': 'settings',
                     'response_state': 'full_response',
                     'settings_type': name}
    try:
        new_value = request.json['new_value']
    except (TypeError, ValueError):
        response_dict.update({
            'success': False,
            'errors': [{
                'id': 64,
                'description': 'Unable to parse sent JSON.'
            }]
        })
    except KeyError:
        response_dict.update({
            'success': False,
            'errors': [{
                'id': 65,
                'description': 'JSON received does not have \'new_value\' key.'
            }]
        })
    else:
        if not new_value:
            response_dict.update({
                'success': False,
                'errors': [{
                    'id': 66,
                    'description': 'Invalid value.'
                }]
            })
        else:
            options = None
            set_value = None
            if name == 'compiler':
                set_value = actions.set_compiler_path(new_value)
            elif name == 'sketch':
                set_value = actions.set_sketch_path(new_value)
            elif name == 'board':
                set_value = actions.set_arduino_board(new_value)
                options = [{'value': board, 'display_text': board}
                           for board in actions.get_arduino_boards()]
            elif name == 'serial':
                set_value = actions.set_serial_port(new_value)
                options = [{'value': k, 'display_text': v}
                           for k, v in iteritems(actions.get_serial_ports())]
            elif name == 'ide':
                set_value = actions.set_load_ide_only(new_value)
                options = [{'value': k, 'display_text': v} for k, v in
                           iteritems(actions.get_load_ide_options())]
            else:
                response_dict.update({'success': False,
                                      'settings_type': 'invalid'})
                response_dict.setdefault('errors', []).append({
                    'id': 63,
                    'description': 'Unexpected setting type to update.'
                })
            # Check if sent value was set, might have been expanded in Settings
            if set_value in new_value:
                response_dict.update({
                    'success': True,
                    'selected': set_value
                })
                if options:
                    response_dict.update({'options': options})
            else:
                response_dict.update({'success': False})
                response_dict.setdefault('errors', []).append({
                    'id': 67,
                    'description': 'New value could not be set.'
                })
    set_header_no_cache()
    return response_dict