Changeset View
Changeset View
Standalone View
Standalone View
swh/model/merkle.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
"""Merkle tree data structure""" | """Merkle tree data structure""" | ||||
import abc | import abc | ||||
import collections | from collections.abc import Mapping | ||||
from typing import Iterator, List, Set | from typing import Iterator, List, Set | ||||
def deep_update(left, right): | def deep_update(left, right): | ||||
"""Recursively update the left mapping with deeply nested values from the right | """Recursively update the left mapping with deeply nested values from the right | ||||
mapping. | mapping. | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | Example: | ||||
... 'key4': 'value1/2/4', | ... 'key4': 'value1/2/4', | ||||
... }, | ... }, | ||||
... }, | ... }, | ||||
... } | ... } | ||||
True | True | ||||
""" | """ | ||||
for key, rvalue in right.items(): | for key, rvalue in right.items(): | ||||
if isinstance(rvalue, collections.Mapping): | if isinstance(rvalue, Mapping): | ||||
new_lvalue = deep_update(left.get(key, {}), rvalue) | new_lvalue = deep_update(left.get(key, {}), rvalue) | ||||
left[key] = new_lvalue | left[key] = new_lvalue | ||||
else: | else: | ||||
left[key] = rvalue | left[key] = rvalue | ||||
return left | return left | ||||
class MerkleNode(dict, metaclass=abc.ABCMeta): | class MerkleNode(dict, metaclass=abc.ABCMeta): | ||||
▲ Show 20 Lines • Show All 235 Lines • Show Last 20 Lines |