cdb1644fd33483061959739c096b4a8ad5091fdb
1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Telefonica S.A.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
20 from http
import HTTPStatus
21 from copy
import deepcopy
23 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
26 class DbException(Exception):
28 def __init__(self
, message
, http_code
=HTTPStatus
.NOT_FOUND
):
29 self
.http_code
= http_code
30 Exception.__init
__(self
, "database exception " + str(message
))
35 def __init__(self
, logger_name
='db', master_password
=None):
38 :param logger_name: logging name
39 :param master_password: master password used for encrypt decrypt methods
41 self
.logger
= logging
.getLogger(logger_name
)
42 self
.master_password
= master_password
44 def db_connect(self
, config
):
47 :param config: Configuration of database
48 :return: None or raises DbException on error
52 def db_disconnect(self
):
54 Disconnect from database
59 def get_list(self
, table
, q_filter
=None):
61 Obtain a list of entries matching q_filter
62 :param table: collection or table
63 :param q_filter: Filter
64 :return: a list (can be empty) with the found entries. Raises DbException on error
66 raise DbException("Method 'get_list' not implemented")
68 def get_one(self
, table
, q_filter
=None, fail_on_empty
=True, fail_on_more
=True):
70 Obtain one entry matching q_filter
71 :param table: collection or table
72 :param q_filter: Filter
73 :param fail_on_empty: If nothing matches filter it returns None unless this flag is set tu True, in which case
74 it raises a DbException
75 :param fail_on_more: If more than one matches filter it returns one of then unless this flag is set tu True, so
76 that it raises a DbException
77 :return: The requested element, or None
79 raise DbException("Method 'get_one' not implemented")
81 def del_list(self
, table
, q_filter
=None):
83 Deletes all entries that match q_filter
84 :param table: collection or table
85 :param q_filter: Filter
86 :return: Dict with the number of entries deleted
88 raise DbException("Method 'del_list' not implemented")
90 def del_one(self
, table
, q_filter
=None, fail_on_empty
=True):
92 Deletes one entry that matches q_filter
93 :param table: collection or table
94 :param q_filter: Filter
95 :param fail_on_empty: If nothing matches filter it returns '0' deleted unless this flag is set tu True, in
96 which case it raises a DbException
97 :return: Dict with the number of entries deleted
99 raise DbException("Method 'del_one' not implemented")
101 def create(self
, table
, indata
):
103 Add a new entry at database
104 :param table: collection or table
105 :param indata: content to be added
106 :return: database id of the inserted element. Raises a DbException on error
108 raise DbException("Method 'create' not implemented")
110 def set_one(self
, table
, q_filter
, update_dict
, fail_on_empty
=True):
112 Modifies an entry at database
113 :param table: collection or table
114 :param q_filter: Filter
115 :param update_dict: Plain dictionary with the content to be updated. It is a dot separated keys and a value
116 :param fail_on_empty: If nothing matches filter it returns None unless this flag is set tu True, in which case
117 it raises a DbException
118 :return: Dict with the number of entries modified. None if no matching is found.
120 raise DbException("Method 'set_one' not implemented")
122 def set_list(self
, table
, q_filter
, update_dict
):
124 Modifies al matching entries at database
125 :param table: collection or table
126 :param q_filter: Filter
127 :param update_dict: Plain dictionary with the content to be updated. It is a dot separated keys and a value
128 :return: Dict with the number of entries modified
130 raise DbException("Method 'set_list' not implemented")
132 def replace(self
, table
, _id
, indata
, fail_on_empty
=True):
134 Replace the content of an entry
135 :param table: collection or table
136 :param _id: internal database id
137 :param indata: content to replace
138 :param fail_on_empty: If nothing matches filter it returns None unless this flag is set tu True, in which case
139 it raises a DbException
140 :return: Dict with the number of entries replaced
142 raise DbException("Method 'replace' not implemented")
144 def encrypt(self
, value
, salt
=None):
147 :param value: value to be encrypted
148 :param salt: optional salt to be used
149 :return: Encrypted content of value
151 # for the moment return same value. until all modules call this method
153 # raise DbException("Method 'encrypt' not implemented")
155 def decrypt(self
, value
, salt
=None):
157 Decrypt an encrypted value
158 :param value: value to be decrypted
159 :param salt: optional salt to be used
160 :return: Plain content of value
162 # for the moment return same value. until all modules call this method
164 # raise DbException("Method 'decrypt' not implemented")
167 def deep_update_rfc7396(dict_to_change
, dict_reference
, key_list
=None):
169 Modifies one dictionary with the information of the other following https://tools.ietf.org/html/rfc7396
170 Basically is a recursive python 'dict_to_change.update(dict_reference)', but a value of None is used to delete.
171 It implements an extra feature that allows modifying an array. RFC7396 only allows replacing the entire array.
172 For that, dict_reference should contains a dict with keys starting by "$" with the following meaning:
173 $[index] <index> is an integer for targeting a concrete index from dict_to_change array. If the value is None
174 the element of the array is deleted, otherwise it is edited.
175 $+[index] The value is inserted at this <index>. A value of None has not sense and an exception is raised.
176 $+ The value is appended at the end. A value of None has not sense and an exception is raised.
177 $val It looks for all the items in the array dict_to_change equal to <val>. <val> is evaluated as yaml,
178 that is, numbers are taken as type int, true/false as boolean, etc. Use quotes to force string.
179 Nothing happens if no match is found. If the value is None the matched elements are deleted.
180 $key: val In case a dictionary is passed in yaml format, if looks for all items in the array dict_to_change
181 that are dictionaries and contains this <key> equal to <val>. Several keys can be used by yaml
182 format '{key: val, key: val, ...}'; and all of them mast match. Nothing happens if no match is
183 found. If value is None the matched items are deleted, otherwise they are edited.
184 $+val If no match if found (see '$val'), the value is appended to the array. If any match is found nothing
185 is changed. A value of None has not sense.
186 $+key: val If no match if found (see '$key: val'), the value is appended to the array. If any match is found
187 nothing is changed. A value of None has not sense.
188 If there are several editions, insertions and deletions; editions and deletions are done first in reverse index
189 order; then insertions also in reverse index order; and finally appends in any order. So indexes used at
190 insertions must take into account the deleted items.
191 :param dict_to_change: Target dictionary to be changed.
192 :param dict_reference: Dictionary that contains changes to be applied.
193 :param key_list: This is used internally for recursive calls. Do not fill this parameter.
194 :return: none or raises and exception only at array modification when there is a bad format or conflict.
196 def _deep_update_array(array_to_change
, _dict_reference
, _key_list
):
198 to_insert_at_index
= {}
199 values_to_edit_delete
= {}
200 indexes_to_edit_delete
= []
203 for k
in _dict_reference
:
204 _key_list
[-1] = str(k
)
205 if not isinstance(k
, str) or not k
.startswith("$"):
206 if array_edition
is True:
207 raise DbException("Found array edition (keys starting with '$') and pure dictionary edition in the"
208 " same dict at '{}'".format(":".join(_key_list
[:-1])))
209 array_edition
= False
212 if array_edition
is False:
213 raise DbException("Found array edition (keys starting with '$') and pure dictionary edition in the"
214 " same dict at '{}'".format(":".join(_key_list
[:-1])))
217 indexes
= [] # indexes to edit or insert
219 if kitem
.startswith('+'):
222 if _dict_reference
[k
] is None:
223 raise DbException("A value of None has not sense for insertions at '{}'".format(
224 ":".join(_key_list
)))
226 if kitem
.startswith('[') and kitem
.endswith(']'):
228 index
= int(kitem
[1:-1])
230 index
+= len(array_to_change
)
232 index
= 0 # skip outside index edition
233 indexes
.append(index
)
235 raise DbException("Wrong format at '{}'. Expecting integer index inside quotes".format(
236 ":".join(_key_list
)))
238 # match_found_skip = False
240 filter_in
= yaml
.safe_load(kitem
)
242 raise DbException("Wrong format at '{}'. Expecting '$<yaml-format>'".format(":".join(_key_list
)))
243 if isinstance(filter_in
, dict):
244 for index
, item
in enumerate(array_to_change
):
245 for filter_k
, filter_v
in filter_in
.items():
246 if not isinstance(item
, dict) or filter_k
not in item
or item
[filter_k
] != filter_v
:
250 # match_found_skip = True
254 indexes
.append(index
)
258 while True: # if not match a ValueError exception will be raise
259 index
= array_to_change
.index(filter_in
, index
)
261 # match_found_skip = True
264 indexes
.append(index
)
269 # if match_found_skip:
272 raise DbException("Wrong format at '{}'. Expecting '$+', '$[<index]' or '$[<filter>]'".format(
273 ":".join(_key_list
)))
274 for index
in indexes
:
276 if index
in to_insert_at_index
and to_insert_at_index
[index
] != _dict_reference
[k
]:
277 # Several different insertions on the same item of the array
278 raise DbException("Conflict at '{}'. Several insertions on same array index {}".format(
279 ":".join(_key_list
), index
))
280 to_insert_at_index
[index
] = _dict_reference
[k
]
282 if index
in indexes_to_edit_delete
and values_to_edit_delete
[index
] != _dict_reference
[k
]:
283 # Several different editions on the same item of the array
284 raise DbException("Conflict at '{}'. Several editions on array index {}".format(
285 ":".join(_key_list
), index
))
286 indexes_to_edit_delete
.append(index
)
287 values_to_edit_delete
[index
] = _dict_reference
[k
]
290 to_append
[k
] = _dict_reference
[k
]
291 # elif _dict_reference[k] is not None:
292 # raise DbException("Not found any match to edit in the array, or wrong format at '{}'".format(
293 # ":".join(_key_list)))
295 # edition/deletion is done before insertion
296 indexes_to_edit_delete
.sort(reverse
=True)
297 for index
in indexes_to_edit_delete
:
298 _key_list
[-1] = str(index
)
300 if values_to_edit_delete
[index
] is None: # None->Anything
302 del (array_to_change
[index
])
304 pass # it is not consider an error if this index does not exist
305 elif not isinstance(values_to_edit_delete
[index
], dict): # NotDict->Anything
306 array_to_change
[index
] = deepcopy(values_to_edit_delete
[index
])
307 elif isinstance(array_to_change
[index
], dict): # Dict->Dict
308 deep_update_rfc7396(array_to_change
[index
], values_to_edit_delete
[index
], _key_list
)
309 else: # Dict->NotDict
310 if isinstance(array_to_change
[index
], list): # Dict->List. Check extra array edition
311 if _deep_update_array(array_to_change
[index
], values_to_edit_delete
[index
], _key_list
):
313 array_to_change
[index
] = deepcopy(values_to_edit_delete
[index
])
314 # calling deep_update_rfc7396 to delete the None values
315 deep_update_rfc7396(array_to_change
[index
], values_to_edit_delete
[index
], _key_list
)
317 raise DbException("Array edition index out of range at '{}'".format(":".join(_key_list
)))
319 # insertion with indexes
320 to_insert_indexes
= list(to_insert_at_index
.keys())
321 to_insert_indexes
.sort(reverse
=True)
322 for index
in to_insert_indexes
:
323 array_to_change
.insert(index
, to_insert_at_index
[index
])
326 for k
, insert_value
in to_append
.items():
327 _key_list
[-1] = str(k
)
328 insert_value_copy
= deepcopy(insert_value
)
329 if isinstance(insert_value_copy
, dict):
330 # calling deep_update_rfc7396 to delete the None values
331 deep_update_rfc7396(insert_value_copy
, insert_value
, _key_list
)
332 array_to_change
.append(insert_value_copy
)
342 for k
in dict_reference
:
343 key_list
[-1] = str(k
)
344 if dict_reference
[k
] is None: # None->Anything
345 if k
in dict_to_change
:
346 del dict_to_change
[k
]
347 elif not isinstance(dict_reference
[k
], dict): # NotDict->Anything
348 dict_to_change
[k
] = deepcopy(dict_reference
[k
])
349 elif k
not in dict_to_change
: # Dict->Empty
350 dict_to_change
[k
] = deepcopy(dict_reference
[k
])
351 # calling deep_update_rfc7396 to delete the None values
352 deep_update_rfc7396(dict_to_change
[k
], dict_reference
[k
], key_list
)
353 elif isinstance(dict_to_change
[k
], dict): # Dict->Dict
354 deep_update_rfc7396(dict_to_change
[k
], dict_reference
[k
], key_list
)
355 else: # Dict->NotDict
356 if isinstance(dict_to_change
[k
], list): # Dict->List. Check extra array edition
357 if _deep_update_array(dict_to_change
[k
], dict_reference
[k
], key_list
):
359 dict_to_change
[k
] = deepcopy(dict_reference
[k
])
360 # calling deep_update_rfc7396 to delete the None values
361 deep_update_rfc7396(dict_to_change
[k
], dict_reference
[k
], key_list
)
365 def deep_update(dict_to_change
, dict_reference
):
366 """ Maintained for backward compatibility. Use deep_update_rfc7396 instead"""
367 return deep_update_rfc7396(dict_to_change
, dict_reference
)