| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame^] | 1 | import yaml |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 2 | from http import HTTPStatus |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame^] | 3 | from copy import deepcopy |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 4 | |
| 5 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 6 | |
| 7 | |
| 8 | class DbException(Exception): |
| 9 | |
| 10 | def __init__(self, message, http_code=HTTPStatus.NOT_FOUND): |
| tierno | ebbf353 | 2018-05-03 17:49:37 +0200 | [diff] [blame] | 11 | # TODO change to http.HTTPStatus instead of int that allows .value and .name |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 12 | self.http_code = http_code |
| 13 | Exception.__init__(self, "database exception " + message) |
| 14 | |
| 15 | |
| 16 | class DbBase(object): |
| 17 | |
| 18 | def __init__(self): |
| 19 | pass |
| 20 | |
| 21 | def db_connect(self, config): |
| 22 | pass |
| 23 | |
| 24 | def db_disconnect(self): |
| 25 | pass |
| 26 | |
| 27 | def get_list(self, table, filter={}): |
| tierno | ebbf353 | 2018-05-03 17:49:37 +0200 | [diff] [blame] | 28 | raise DbException("Method 'get_list' not implemented") |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 29 | |
| 30 | def get_one(self, table, filter={}, fail_on_empty=True, fail_on_more=True): |
| tierno | ebbf353 | 2018-05-03 17:49:37 +0200 | [diff] [blame] | 31 | raise DbException("Method 'get_one' not implemented") |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 32 | |
| 33 | def create(self, table, indata): |
| tierno | ebbf353 | 2018-05-03 17:49:37 +0200 | [diff] [blame] | 34 | raise DbException("Method 'create' not implemented") |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 35 | |
| 36 | def del_list(self, table, filter={}): |
| tierno | ebbf353 | 2018-05-03 17:49:37 +0200 | [diff] [blame] | 37 | raise DbException("Method 'del_list' not implemented") |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 38 | |
| 39 | def del_one(self, table, filter={}, fail_on_empty=True): |
| tierno | ebbf353 | 2018-05-03 17:49:37 +0200 | [diff] [blame] | 40 | raise DbException("Method 'del_one' not implemented") |
| tierno | b3e750b | 2018-09-05 11:25:23 +0200 | [diff] [blame^] | 41 | |
| 42 | |
| 43 | def deep_update(dict_to_change, dict_reference, key_list=None): |
| 44 | """ |
| 45 | Modifies one dictionary with the information of the other following https://tools.ietf.org/html/rfc7396 |
| 46 | Basically is a recursive python 'dict_to_change.update(dict_reference)', but a value of None is used to delete. |
| 47 | It implements an extra feature that allows modifying an array. RFC7396 only allows replacing the entire array. |
| 48 | For that, dict_reference should contains a dict with keys starting by "$" with the following meaning: |
| 49 | $[index] <index> is an integer for targeting a concrete index from dict_to_change array. If the value is None |
| 50 | the element of the array is deleted, otherwise it is edited. |
| 51 | $+[index] The value is inserted at this <index>. A value of None has not sense and an exception is raised. |
| 52 | $+ The value is appended at the end. A value of None has not sense and an exception is raised. |
| 53 | $val It looks for all the items in the array dict_to_change equal to <val>. <val> is evaluated as yaml, |
| 54 | that is, numbers are taken as type int, true/false as boolean, etc. Use quotes to force string. |
| 55 | Nothing happens if no match is found. If the value is None the matched elements are deleted. |
| 56 | $key: val In case a dictionary is passed in yaml format, if looks for all items in the array dict_to_change |
| 57 | that are dictionaries and contains this <key> equal to <val>. Several keys can be used by yaml |
| 58 | format '{key: val, key: val, ...}'; and all of them mast match. Nothing happens if no match is |
| 59 | found. If value is None the matched items are deleted, otherwise they are edited. |
| 60 | $+val If no match if found (see '$val'), the value is appended to the array. If any match is found nothing |
| 61 | is changed. A value of None has not sense. |
| 62 | $+key: val If no match if found (see '$key: val'), the value is appended to the array. If any match is found |
| 63 | nothing is changed. A value of None has not sense. |
| 64 | If there are several editions, insertions and deletions; editions and deletions are done first in reverse index |
| 65 | order; then insertions also in reverse index order; and finally appends in any order. So indexes used at |
| 66 | insertions must take into account the deleted items. |
| 67 | :param dict_to_change: Target dictionary to be changed. |
| 68 | :param dict_reference: Dictionary that contains changes to be applied. |
| 69 | :param key_list: This is used internally for recursive calls. Do not fill this parameter. |
| 70 | :return: none or raises and exception only at array modification when there is a bad format or conflict. |
| 71 | """ |
| 72 | def _deep_update_array(array_to_change, _dict_reference, _key_list): |
| 73 | to_append = {} |
| 74 | to_insert_at_index = {} |
| 75 | values_to_edit_delete = {} |
| 76 | indexes_to_edit_delete = [] |
| 77 | array_edition = None |
| 78 | _key_list.append("") |
| 79 | for k in _dict_reference: |
| 80 | _key_list[-1] = str(k) |
| 81 | if not isinstance(k, str) or not k.startswith("$"): |
| 82 | if array_edition is True: |
| 83 | raise DbException("Found array edition (keys starting with '$') and pure dictionary edition in the" |
| 84 | " same dict at '{}'".format(":".join(_key_list[:-1]))) |
| 85 | array_edition = False |
| 86 | continue |
| 87 | else: |
| 88 | if array_edition is False: |
| 89 | raise DbException("Found array edition (keys starting with '$') and pure dictionary edition in the" |
| 90 | " same dict at '{}'".format(":".join(_key_list[:-1]))) |
| 91 | array_edition = True |
| 92 | insert = False |
| 93 | indexes = [] # indexes to edit or insert |
| 94 | kitem = k[1:] |
| 95 | if kitem.startswith('+'): |
| 96 | insert = True |
| 97 | kitem = kitem[1:] |
| 98 | if _dict_reference[k] is None: |
| 99 | raise DbException("A value of None has not sense for insertions at '{}'".format( |
| 100 | ":".join(_key_list))) |
| 101 | |
| 102 | if kitem.startswith('[') and kitem.endswith(']'): |
| 103 | try: |
| 104 | index = int(kitem[1:-1]) |
| 105 | if index < 0: |
| 106 | index += len(array_to_change) |
| 107 | if index < 0: |
| 108 | index = 0 # skip outside index edition |
| 109 | indexes.append(index) |
| 110 | except Exception: |
| 111 | raise DbException("Wrong format at '{}'. Expecting integer index inside quotes".format( |
| 112 | ":".join(_key_list))) |
| 113 | elif kitem: |
| 114 | # match_found_skip = False |
| 115 | try: |
| 116 | filter_in = yaml.safe_load(kitem) |
| 117 | except Exception: |
| 118 | raise DbException("Wrong format at '{}'. Expecting '$<yaml-format>'".format(":".join(_key_list))) |
| 119 | if isinstance(filter_in, dict): |
| 120 | for index, item in enumerate(array_to_change): |
| 121 | for filter_k, filter_v in filter_in.items(): |
| 122 | if not isinstance(item, dict) or filter_k not in item or item[filter_k] != filter_v: |
| 123 | break |
| 124 | else: # match found |
| 125 | if insert: |
| 126 | # match_found_skip = True |
| 127 | insert = False |
| 128 | break |
| 129 | else: |
| 130 | indexes.append(index) |
| 131 | else: |
| 132 | index = 0 |
| 133 | try: |
| 134 | while True: # if not match a ValueError exception will be raise |
| 135 | index = array_to_change.index(filter_in, index) |
| 136 | if insert: |
| 137 | # match_found_skip = True |
| 138 | insert = False |
| 139 | break |
| 140 | indexes.append(index) |
| 141 | index += 1 |
| 142 | except ValueError: |
| 143 | pass |
| 144 | |
| 145 | # if match_found_skip: |
| 146 | # continue |
| 147 | elif not insert: |
| 148 | raise DbException("Wrong format at '{}'. Expecting '$+', '$[<index]' or '$[<filter>]'".format( |
| 149 | ":".join(_key_list))) |
| 150 | for index in indexes: |
| 151 | if insert: |
| 152 | if index in to_insert_at_index and to_insert_at_index[index] != _dict_reference[k]: |
| 153 | # Several different insertions on the same item of the array |
| 154 | raise DbException("Conflict at '{}'. Several insertions on same array index {}".format( |
| 155 | ":".join(_key_list), index)) |
| 156 | to_insert_at_index[index] = _dict_reference[k] |
| 157 | else: |
| 158 | if index in indexes_to_edit_delete and values_to_edit_delete[index] != _dict_reference[k]: |
| 159 | # Several different editions on the same item of the array |
| 160 | raise DbException("Conflict at '{}'. Several editions on array index {}".format( |
| 161 | ":".join(_key_list), index)) |
| 162 | indexes_to_edit_delete.append(index) |
| 163 | values_to_edit_delete[index] = _dict_reference[k] |
| 164 | if not indexes: |
| 165 | if insert: |
| 166 | to_append[k] = _dict_reference[k] |
| 167 | # elif _dict_reference[k] is not None: |
| 168 | # raise DbException("Not found any match to edit in the array, or wrong format at '{}'".format( |
| 169 | # ":".join(_key_list))) |
| 170 | |
| 171 | # edition/deletion is done before insertion |
| 172 | indexes_to_edit_delete.sort(reverse=True) |
| 173 | for index in indexes_to_edit_delete: |
| 174 | _key_list[-1] = str(index) |
| 175 | try: |
| 176 | if values_to_edit_delete[index] is None: # None->Anything |
| 177 | try: |
| 178 | del (array_to_change[index]) |
| 179 | except IndexError: |
| 180 | pass # it is not consider an error if this index does not exist |
| 181 | elif not isinstance(values_to_edit_delete[index], dict): # NotDict->Anything |
| 182 | array_to_change[index] = deepcopy(values_to_edit_delete[index]) |
| 183 | elif isinstance(array_to_change[index], dict): # Dict->Dict |
| 184 | deep_update(array_to_change[index], values_to_edit_delete[index], _key_list) |
| 185 | else: # Dict->NotDict |
| 186 | if isinstance(array_to_change[index], list): # Dict->List. Check extra array edition |
| 187 | if _deep_update_array(array_to_change[index], values_to_edit_delete[index], _key_list): |
| 188 | continue |
| 189 | array_to_change[index] = deepcopy(values_to_edit_delete[index]) |
| 190 | # calling deep_update to delete the None values |
| 191 | deep_update(array_to_change[index], values_to_edit_delete[index], _key_list) |
| 192 | except IndexError: |
| 193 | raise DbException("Array edition index out of range at '{}'".format(":".join(_key_list))) |
| 194 | |
| 195 | # insertion with indexes |
| 196 | to_insert_indexes = list(to_insert_at_index.keys()) |
| 197 | to_insert_indexes.sort(reverse=True) |
| 198 | for index in to_insert_indexes: |
| 199 | array_to_change.insert(index, to_insert_at_index[index]) |
| 200 | |
| 201 | # append |
| 202 | for k, insert_value in to_append.items(): |
| 203 | _key_list[-1] = str(k) |
| 204 | insert_value_copy = deepcopy(insert_value) |
| 205 | if isinstance(insert_value_copy, dict): |
| 206 | # calling deep_update to delete the None values |
| 207 | deep_update(insert_value_copy, insert_value, _key_list) |
| 208 | array_to_change.append(insert_value_copy) |
| 209 | |
| 210 | _key_list.pop() |
| 211 | if array_edition: |
| 212 | return True |
| 213 | return False |
| 214 | |
| 215 | if key_list is None: |
| 216 | key_list = [] |
| 217 | key_list.append("") |
| 218 | for k in dict_reference: |
| 219 | key_list[-1] = str(k) |
| 220 | if dict_reference[k] is None: # None->Anything |
| 221 | if k in dict_to_change: |
| 222 | del dict_to_change[k] |
| 223 | elif not isinstance(dict_reference[k], dict): # NotDict->Anything |
| 224 | dict_to_change[k] = deepcopy(dict_reference[k]) |
| 225 | elif k not in dict_to_change: # Dict->Empty |
| 226 | dict_to_change[k] = deepcopy(dict_reference[k]) |
| 227 | # calling deep_update to delete the None values |
| 228 | deep_update(dict_to_change[k], dict_reference[k], key_list) |
| 229 | elif isinstance(dict_to_change[k], dict): # Dict->Dict |
| 230 | deep_update(dict_to_change[k], dict_reference[k], key_list) |
| 231 | else: # Dict->NotDict |
| 232 | if isinstance(dict_to_change[k], list): # Dict->List. Check extra array edition |
| 233 | if _deep_update_array(dict_to_change[k], dict_reference[k], key_list): |
| 234 | continue |
| 235 | dict_to_change[k] = deepcopy(dict_reference[k]) |
| 236 | # calling deep_update to delete the None values |
| 237 | deep_update(dict_to_change[k], dict_reference[k], key_list) |
| 238 | key_list.pop() |