Пример #1
0
def _execute(keychain, method, data, value=None):
    if isinstance(keychain, str):
        keychain = keychain.split(".")
    key_index_array = utils.get_index_from_key(keychain[0])
    key = key_index_array[0]
    index = utils.string_reps_int(key_index_array[1])

    if isinstance(data, list):
        if key is '' and utils.check_for_valid_index(index):
            data[index] = _execute(keychain[1:],
                                   method,
                                   data=data[index],
                                   value=value)
            if method is utils.Methods.GET_VALUE:
                data = data[index]
        else:
            ret_val = []
            if method is utils.Methods.SET_ITEM:
                data = _check_for_new_item(data, key)
            for ind, val in enumerate(data):
                data[ind] = _execute(keychain,
                                     method,
                                     data=data[ind],
                                     value=value)
                if method is utils.Methods.GET_VALUE:
                    _create_ret_array(data[ind], ret_val)
            if method is utils.Methods.GET_VALUE:
                data = ret_val
    else:
        try:
            if utils.check_for_valid_index(index) and isinstance(
                    data[key], list):
                if len(keychain) == 1:
                    data = _action(method, data, key, index, value)
                else:
                    data[key][index] = _execute(keychain[1:],
                                                method,
                                                data=data[key][index],
                                                value=value)
                    if method is utils.Methods.GET_VALUE:
                        data = data[key][index]
            else:
                if len(keychain) == 1:
                    data = _action(method, data, key, value=value)
                else:
                    data[key] = _execute(keychain[1:],
                                         method,
                                         data=data[key],
                                         value=value)
                    if method is utils.Methods.GET_VALUE:
                        data = data[key]
        except KeyError:
            if method is utils.Methods.GET_VALUE:
                return {}
            else:
                pass
        except IndexError:
            if method is utils.Methods.GET_VALUE:
                return {}
            else:
                pass
    return data
 def test_get_index_from_keystring(self):
     key_string = "Key[1]"
     self.assertEqual(utils.get_index_from_key(key_string), ['Key', '1'])
 def test_get_index_from_keystring_somestring(self):
     key_string = "Key[blablub]"
     self.assertEqual(utils.get_index_from_key(key_string),
                      ['Key', 'blablub'])
 def test_index_from_keystring_no_opening(self):
     key_string = "Key1234]"
     self.assertEqual(utils.get_index_from_key(key_string),
                      ['Key1234]', None])
 def test_index_from_keystring_close_before_opening(self):
     key_string = "Key]1234["
     self.assertEqual(utils.get_index_from_key(key_string), ['Key', None])
 def test_index_from_key_string_no_closing(self):
     key_string = "Key[1234"
     self.assertEqual(utils.get_index_from_key(key_string), ['Key', None])
 def test_index_from_keystring_no_index(self):
     key_string = "Key"
     self.assertEqual(utils.get_index_from_key(key_string), ['Key', None])
 def test_index_from_keystring_doubled_index(self):
     key_string = "Before[1234]After[5678]"
     self.assertEqual(utils.get_index_from_key(key_string),
                      ['Before', '1234'])
 def test_index_from_keystring_keystring_continues_after_index(self):
     key_string = "Before[1234]After"
     self.assertEqual(utils.get_index_from_key(key_string),
                      ['Before', '1234'])
 def test_index_from_keystring_longIndex(self):
     key = 'Key'
     index = '198' * 256
     key_string = f'{key}[{index}]'
     self.assertEqual(utils.get_index_from_key(key_string), [key, index])