rainbowadn/rainbowadn/testing/dictresolver.py
2022-06-07 20:58:26 +03:00

29 lines
1.1 KiB
Python

from collections import OrderedDict
from typing import MutableMapping
from rainbowadn.hashing.mentionable import Mentionable
from rainbowadn.hashing.hashpoint import HashPoint
from rainbowadn.hashing.hashresolver import HashResolver
from rainbowadn.hashing.recursivementionable import RecursiveMentionable
class DictResolver(HashResolver):
def __init__(self):
self.table: MutableMapping[bytes, bytes] = OrderedDict()
def resolve(self, point: bytes) -> tuple[bytes, 'HashResolver']:
assert isinstance(point, bytes)
return self.table[point], self
def save(self, hash_point: HashPoint) -> None:
assert isinstance(hash_point, HashPoint)
if hash_point.point in self.table:
pass
else:
value: Mentionable = hash_point.resolve()
assert isinstance(value, Mentionable)
self.table[hash_point.point] = HashPoint.bytes_of_mentioned(value)
if isinstance(value, RecursiveMentionable):
for hash_point in value.points():
self.save(hash_point)