36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
from collections import OrderedDict
|
|
from typing import MutableMapping, TypeVar
|
|
|
|
from rainbowadn.core import *
|
|
|
|
__all__ = ('DictResolver',)
|
|
|
|
Mentioned = TypeVar('Mentioned', bound=Mentionable)
|
|
|
|
|
|
class DictResolver(ExtendableResolver):
|
|
def __init__(self):
|
|
self.table: MutableMapping[bytes, bytes] = OrderedDict()
|
|
|
|
async def resolve(self, point: bytes) -> tuple[bytes, 'HashResolver']:
|
|
assert isinstance(point, bytes)
|
|
return self.table[point], self
|
|
|
|
async def save(self, hash_point: HashPoint) -> None:
|
|
assert isinstance(hash_point, HashPoint)
|
|
if hash_point.point in self.table:
|
|
pass
|
|
else:
|
|
value: Mentionable = await hash_point.resolve()
|
|
assert isinstance(value, Mentionable)
|
|
self.table[hash_point.point] = HashPoint.bytes_of_mentioned(value)
|
|
if isinstance(value, RecursiveMentionable):
|
|
await gather(
|
|
*(self.save(hp) for hp in value.points())
|
|
)
|
|
|
|
async def extend(self, hash_point: HashPoint[Mentioned]) -> 'ExtendableResolver':
|
|
assert isinstance(hash_point, HashPoint)
|
|
await self.save(hash_point)
|
|
return self
|