2 from http
import HTTPStatus
3 from copy
import deepcopy
5 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
8 class DbException(Exception):
10 def __init__(self
, message
, http_code
=HTTPStatus
.NOT_FOUND
):
11 # TODO change to http.HTTPStatus instead of int that allows .value and .name
12 self
.http_code
= http_code
13 Exception.__init
__(self
, "database exception " + message
)
21 def db_connect(self
, config
):
24 def db_disconnect(self
):
27 def get_list(self
, table
, filter={}):
28 raise DbException("Method 'get_list' not implemented")
30 def get_one(self
, table
, filter={}, fail_on_empty
=True, fail_on_more
=True):
31 raise DbException("Method 'get_one' not implemented")
33 def create(self
, table
, indata
):
34 raise DbException("Method 'create' not implemented")
36 def del_list(self
, table
, filter={}):
37 raise DbException("Method 'del_list' not implemented")
39 def del_one(self
, table
, filter={}, fail_on_empty
=True):
40 raise DbException("Method 'del_one' not implemented")
43 def deep_update(dict_to_change
, dict_reference
, key_list
=None):
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.
72 def _deep_update_array(array_to_change
, _dict_reference
, _key_list
):
74 to_insert_at_index
= {}
75 values_to_edit_delete
= {}
76 indexes_to_edit_delete
= []
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])))
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])))
93 indexes
= [] # indexes to edit or insert
95 if kitem
.startswith('+'):
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
)))
102 if kitem
.startswith('[') and kitem
.endswith(']'):
104 index
= int(kitem
[1:-1])
106 index
+= len(array_to_change
)
108 index
= 0 # skip outside index edition
109 indexes
.append(index
)
111 raise DbException("Wrong format at '{}'. Expecting integer index inside quotes".format(
112 ":".join(_key_list
)))
114 # match_found_skip = False
116 filter_in
= yaml
.safe_load(kitem
)
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
:
126 # match_found_skip = True
130 indexes
.append(index
)
134 while True: # if not match a ValueError exception will be raise
135 index
= array_to_change
.index(filter_in
, index
)
137 # match_found_skip = True
140 indexes
.append(index
)
145 # if match_found_skip:
148 raise DbException("Wrong format at '{}'. Expecting '$+', '$[<index]' or '$[<filter>]'".format(
149 ":".join(_key_list
)))
150 for index
in indexes
:
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
]
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
]
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)))
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
)
176 if values_to_edit_delete
[index
] is None: # None->Anything
178 del (array_to_change
[index
])
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
):
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
)
193 raise DbException("Array edition index out of range at '{}'".format(":".join(_key_list
)))
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
])
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
)
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
):
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
)