36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
"""Read the archived IR schema examples without a Python numerical engine."""
|
|
from dataclasses import is_dataclass
|
|
from enum import Enum
|
|
from functools import lru_cache
|
|
import gzip
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from app.simulation.ir import schema
|
|
|
|
|
|
def _decode(value):
|
|
if isinstance(value, list):
|
|
return tuple(_decode(item) for item in value)
|
|
if isinstance(value, dict):
|
|
if 'enum' in value:
|
|
cls = getattr(schema, value['enum'])
|
|
if not isinstance(cls, type) or not issubclass(cls, Enum):
|
|
raise ValueError('Invalid IR enum in fixture')
|
|
return cls(value['value'])
|
|
cls = getattr(schema, value['type'])
|
|
if not isinstance(cls, type) or not is_dataclass(cls):
|
|
raise ValueError('Invalid IR record in fixture')
|
|
return cls(**{key: _decode(item) for key, item in value['fields'].items()})
|
|
return value
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def _references():
|
|
path = Path(__file__).parent / 'data/system-ir-v2-reference.json.gz'
|
|
return json.loads(gzip.decompress(path.read_bytes()))['programs']
|
|
|
|
|
|
def reference_ir(name):
|
|
return _decode(_references()[name])
|