Skip to content
Snippets Groups Projects
Verified Commit 363b1659 authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

Unify object_type some more within the merkle and from_disk modules

parent 40a40f50
No related branches found
No related tags found
No related merge requests found
......@@ -121,7 +121,7 @@ class Content(MerkleLeaf):
"""
__slots__ = [] # type: List[str]
type = "content"
object_type: Final = "content"
@classmethod
def from_bytes(cls, *, mode, data):
......@@ -300,7 +300,7 @@ class Directory(MerkleNode):
"""
__slots__ = ["__entries"]
type = "directory"
object_type: Final = "directory"
@classmethod
def from_disk(
......@@ -352,14 +352,14 @@ class Directory(MerkleNode):
@staticmethod
def child_to_directory_entry(name, child):
if isinstance(child, Directory):
if child.object_type == "directory":
return {
"type": "dir",
"perms": DentryPerms.directory,
"target": child.hash,
"name": name,
}
elif isinstance(child, Content):
elif child.object_type == "content":
return {
"type": "file",
"perms": child.data["perms"],
......@@ -367,7 +367,7 @@ class Directory(MerkleNode):
"name": name,
}
else:
raise ValueError("unknown child")
raise ValueError(f"unknown child {child}")
def get_data(self, **kwargs):
return {
......
# Copyright (C) 2017 The Software Heritage developers
# Copyright (C) 2017-2020 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
......@@ -8,7 +8,7 @@
import abc
import collections
from typing import Iterator, List, Optional, Set
from typing import Iterator, List, Set
def deep_update(left, right):
......@@ -111,7 +111,6 @@ class MerkleNode(dict, metaclass=abc.ABCMeta):
__slots__ = ["parents", "data", "__hash", "collected"]
type = None # type: Optional[str] # TODO: make this an enum
"""Type of the current node (used as a classifier for :func:`collect`)"""
def __init__(self, data=None):
......@@ -234,7 +233,7 @@ class MerkleNode(dict, metaclass=abc.ABCMeta):
"""
if not self.collected:
self.collected = True
return {self.type: {self.hash: self.get_data(**kwargs)}}
return {self.object_type: {self.hash: self.get_data(**kwargs)}}
else:
return {}
......
# Copyright (C) 2017 The Software Heritage developers
# Copyright (C) 2017-2020 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
......@@ -9,7 +9,7 @@ from swh.model import merkle
class MerkleTestNode(merkle.MerkleNode):
type = "tested_merkle_node_type"
object_type = "tested_merkle_node_type"
def __init__(self, data):
super().__init__(data)
......@@ -23,7 +23,7 @@ class MerkleTestNode(merkle.MerkleNode):
class MerkleTestLeaf(merkle.MerkleLeaf):
type = "tested_merkle_leaf_type"
object_type = "tested_merkle_leaf_type"
def __init__(self, data):
super().__init__(data)
......@@ -62,7 +62,11 @@ class TestMerkleLeaf(unittest.TestCase):
collected = self.instance.collect()
self.assertEqual(
collected,
{self.instance.type: {self.instance.hash: self.instance.get_data(),},},
{
self.instance.object_type: {
self.instance.hash: self.instance.get_data(),
},
},
)
collected2 = self.instance.collect()
self.assertEqual(collected2, {})
......@@ -162,7 +166,7 @@ class TestMerkleNode(unittest.TestCase):
def test_collect(self):
collected = self.root.collect()
self.assertEqual(len(collected[self.root.type]), len(self.nodes))
self.assertEqual(len(collected[self.root.object_type]), len(self.nodes))
for node in self.nodes.values():
self.assertTrue(node.collected)
collected2 = self.root.collect()
......@@ -229,7 +233,7 @@ class TestMerkleNode(unittest.TestCase):
# Ensure we collected root, root/b, and both new children
collected_after_update = self.root.collect()
self.assertCountEqual(
collected_after_update[MerkleTestNode.type],
collected_after_update[MerkleTestNode.object_type],
[
self.nodes[b"root"].hash,
self.nodes[b"root/b"].hash,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment