def to_string(self, indent=0): """ indent is the initial indentation of the serialized list, NOT the extra indentation added at each recursion. Using code should take into account that the last line is CR-LF terminated, and so a split on CRLF will generate an extra blank line """ string = [] # a list of strings spaces = SP.get_spaces(indent) if self._bin_hash is None: if self._hashtype == HashTypes.SHA1: top = "%s%s %s/\n" % (spaces, SHA1_HEX_NONE, self.name) elif self._hashtype == HashTypes.SHA2: top = "%s%s %s/\n" % (spaces, SHA2_HEX_NONE, self.name) elif self._hashtype == HashTypes.SHA3: top = "%s%s %s/\n" % (spaces, SHA3_HEX_NONE, self.name) else: top = "%s%s %s/\n" % (spaces, self.hex_hash, self.name) string.append(top) # <--- LEVEL 0 NODE my_indent = indent + 1 # <--- LEVEL 1 NODE for node in self.nodes: if isinstance(node, MerkleLeaf): string.append(node.to_string(my_indent)) else: # recurse string.append(node.to_string_not_top(my_indent)) return ''.join(string)
def to_string(self, indent=0): if self._bin_hash is None: if self._hashtype == HashTypes.SHA1: hash_ = SHA1_HEX_NONE elif self._hashtype == HashTypes.SHA2: hash_ = SHA2_HEX_NONE elif self._hashtype == HashTypes.SHA3: hash_ = SHA3_HEX_NONE else: hash_ = self.hex_hash string = "%s%s %s\n" % (SP.get_spaces(indent), hash_, self.name) return string
def to_string_not_top(self, indent=0): """ indent is the indentation to be used for the top node""" string = [] # a list of strings spaces = SP.get_spaces(indent) if self._bin_hash is None: if self._hashtype == HashTypes.SHA1: top = "%s%s %s/\n" % (spaces, SHA1_HEX_NONE, self.name) elif self._hashtype == HashTypes.SHA2: top = "%s%s %s/\n" % (spaces, SHA2_HEX_NONE, self.name) elif self._hashtype == HashTypes.SHA3: top = "%s%s %s/\n" % (spaces, SHA3_HEX_NONE, self.name) else: top = "%s%s %s/\n" % (spaces, self.hex_hash, self.name) string.append(top) indent += 1 # <--- LEVEL 2+ NODE for node in self.nodes: if isinstance(node, MerkleLeaf): string.append(node.to_string(indent)) else: # recurse string.append(node.to_string_not_top(indent)) return ''.join(string)