File: utils.py

package info (click to toggle)
exhale 0.3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,616 kB
  • sloc: python: 9,057; cpp: 1,260; javascript: 915; f90: 29; ansic: 18; makefile: 16
file content (66 lines) | stat: -rw-r--r-- 2,248 bytes parent folder | download
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf8 -*-
########################################################################################
# This file is part of exhale.  Copyright (c) 2017-2024, Stephen McDowell.             #
# Full BSD 3-Clause license available here:                                            #
#                                                                                      #
#                https://github.com/svenevs/exhale/blob/master/LICENSE                 #
########################################################################################
"""
Utility functionality for the testing suite.
"""

from __future__ import unicode_literals
try:
    from collections.abc import Mapping
except ImportError:
    # TODO: remove when dropping python 2.7
    from collections import Mapping


def deep_update(orig, override):
    """
    Update leaf values of a data structure (nested dictionaries and lists).

    **Parameters**
        Both ``orig`` and ``override`` should be of the same type.  E.g. both ``orig``
        and ``override`` are a :class:`python:dict`, or both are a :class:`python:list`.

        ``orig`` (:class:`python:dict` or :class:`python:list`)
            The original data structure.

        ``override`` (:class:`python:dict` or :class:`python:list`)
            The overriding data structure.

    **Return**
        :class:`python:dict` or :class:`python:list`
            The ``orig`` object, updated with the ``override`` values.
    """
    if not override:
        return orig

    for key, val in override.items():
        if isinstance(val, Mapping):
            try:
                deep_update(orig[key], val)
            except KeyError:
                orig[key] = val

        elif isinstance(val, list):
            try:
                orig_val = orig[key]

                if not isinstance(orig_val, list):
                    raise KeyError

                for i, v in enumerate(val):
                    if not i and val[i] is None:
                        # ignoring modified list markers
                        continue
                    orig_val.append(v)

            except KeyError:
                orig[key] = val

        else:
            orig[key] = override[key]
    return orig