[go: up one dir, main page]

Menu

[cc83e9]: / tools / dict_util.py  Maximize  Restore  History

Download this file

51 lines (44 with data), 2.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Drakkar-Software OctoBot
# Copyright (c) Drakkar-Software, All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3.0 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.
def find_nested_value(dict_, field):
if field in dict_:
return True, dict_[field]
else:
for value in dict_.values():
if isinstance(value, dict):
found_value, possible_value = find_nested_value(value, field)
if found_value:
return found_value, possible_value
elif isinstance(value, list):
for item in value:
if isinstance(item, dict):
found_value, possible_value = find_nested_value(item, field)
if found_value:
return found_value, possible_value
return False, field
def get_value_or_default(dictionary, key, default=None, strict=False):
if dictionary and key in dictionary:
value = dictionary[key]
return value if strict else value or default
return default
def check_and_merge_values_from_reference(current_dict, reference_dict, exception_list, logger=None):
for key, val in reference_dict.items():
if key not in current_dict:
current_dict[key] = val
if logger is not None:
logger.warning(f"Missing {key} in configuration, added default value: {val}")
elif isinstance(val, dict) and key not in exception_list:
check_and_merge_values_from_reference(current_dict[key], val, exception_list, logger=logger)