42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
import abc
|
|
from typing import Generic, Optional, Type, TypeVar
|
|
|
|
from rainbowadn.core import *
|
|
from .inlining import *
|
|
|
|
__all__ = ('IStatic', 'IStaticFactory',)
|
|
|
|
StaticInlined = TypeVar('StaticInlined')
|
|
|
|
|
|
class IStatic(Mentionable, abc.ABC):
|
|
@classmethod
|
|
def size(cls) -> int:
|
|
raise NotImplementedError
|
|
|
|
@classmethod
|
|
def from_bytes(cls: Type[StaticInlined], source: bytes, resolver: HashResolver) -> StaticInlined:
|
|
raise NotImplementedError
|
|
|
|
def __factory__(self: StaticInlined) -> RainbowFactory[StaticInlined]:
|
|
return self.factory()
|
|
|
|
@classmethod
|
|
def factory(cls: Type[StaticInlined]) -> RainbowFactory[StaticInlined]:
|
|
assert issubclass(cls, IStatic)
|
|
return IStaticFactory(cls)
|
|
|
|
|
|
class IStaticFactory(Inlining[StaticInlined], Generic[StaticInlined]):
|
|
def __init__(self, cls: Type[StaticInlined]):
|
|
assert issubclass(cls, IStatic)
|
|
self.cls = cls
|
|
|
|
def size(self) -> Optional[int]:
|
|
return self.cls.size()
|
|
|
|
def from_bytes(self, source: bytes, resolver: HashResolver) -> StaticInlined:
|
|
assert isinstance(source, bytes)
|
|
assert isinstance(resolver, HashResolver)
|
|
return self.cls.from_bytes(source, resolver)
|