Launchpad automatic translations update.
[odoo/odoo.git] / addons / document / dict_tools.py
1 # -*- encoding: utf-8 -*-
2 # Copyright P. Christeas <p_christ@hol.gr> 2008-2010
3 # Copyright 2010 OpenERP SA. http://www.openerp.com
4 #
5 # This program is Free Software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public License
7 # as published by the Free Software Foundation; version 2 of the License.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 ###############################################################################
18
19
20 def dict_merge(*dicts):
21     """ Return a dict with all values of dicts
22     """
23     res = {}
24     for d in dicts:
25         res.update(d)
26     return res
27
28 def dict_merge2(*dicts):
29     """ Return a dict with all values of dicts.
30         If some key appears twice and contains iterable objects, the values
31         are merged (instead of overwritten).
32     """
33     res = {}
34     for d in dicts:
35         for k in d.keys():
36             if k in res and isinstance(res[k], (list, tuple)):
37                 res[k] = res[k] + d[k]
38             elif k in res and isinstance(res[k], dict):
39                 res[k].update(d[k])
40             else:
41                 res[k] = d[k]
42     return res
43
44 def dict_filter(srcdic, keys, res=None):
45     ''' Return a copy of srcdic that has only keys set.
46     If any of keys are missing from srcdic, the result won't have them, 
47     either.
48     @param res If given, result will be updated there, instead of a new dict.
49     '''
50     if res is None:
51         res = {}
52     for k in keys:
53         if k in srcdic:
54             res[k] = srcdic[k]
55     return res
56
57 #eof
58
59 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: