martin f. krafft | f37f068 | 2013-06-14 16:36:20 +0200 | [diff] [blame] | 1 | # |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # |
| 4 | # This file is part of reclass (http://github.com/madduck/reclass) |
| 5 | # |
| 6 | # Copyright © 2007–13 martin f. krafft <madduck@madduck.net> |
| 7 | # Released under the terms of the Artistic Licence 2.0 |
| 8 | # |
| 9 | from base import BaseDictMerger |
| 10 | |
| 11 | class DictRecursiveUpdate(BaseDictMerger): |
| 12 | |
| 13 | def merge(self, first, second): |
martin f. krafft | a95eaa3 | 2013-06-16 11:56:41 +0200 | [diff] [blame^] | 14 | if second is None: |
| 15 | return first |
| 16 | |
martin f. krafft | f37f068 | 2013-06-14 16:36:20 +0200 | [diff] [blame] | 17 | ret = first.copy() |
| 18 | for k,v in second.iteritems(): |
| 19 | if k in ret: |
| 20 | if isinstance(ret[k], dict): |
| 21 | if isinstance(v, (list, tuple)): |
| 22 | v = dict(v) |
| 23 | ret[k] = self.merge(ret[k], v) |
| 24 | else: |
| 25 | ret[k] = v |
| 26 | else: |
| 27 | ret[k] = v |
| 28 | return ret |