Update python2 to python3
[vfc/nfvo/lcm.git] / lcm / pub / utils / values.py
1 # Copyright 2016 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import logging
16
17 logger = logging.getLogger(__name__)
18
19
20 def ignore_case_get(args, key, def_val=""):
21     if not key:
22         return def_val
23     if key in args:
24         return args[key]
25     for old_key in args:
26         if old_key.upper() == key.upper():
27             return args[old_key]
28     return def_val
29
30
31 def remove_none_key(data, none_list=None):
32     none_list = none_list if none_list else [None, '', 'NULL', 'None', 'null', {}, '{}']
33     if isinstance(data, dict):
34         data = dict([(k, remove_none_key(v)) for k, v in list(data.items()) if v not in none_list])
35     if isinstance(data, list):
36         data = [remove_none_key(s) for s in data if s not in none_list]
37     return data
38
39
40 def update_value(origin_data, new_data):
41     logger.debug(origin_data)
42     if not isinstance(origin_data, dict):
43         str_data = origin_data.encode('utf-8')
44         logger.debug(str_data)
45         origin_data = eval(str_data)
46     logger.debug(isinstance(origin_data, dict))
47     logger.debug(new_data)
48     for k, v in list(new_data.items()):
49         if k not in origin_data:
50             origin_data[k] = v
51         else:
52             if isinstance(origin_data[k], list):
53                 origin_data[k] = origin_data[k].extend(v)
54             else:
55                 origin_data[k] = v
56     return origin_data