rivet/structure.py
2023-11-24 14:34:44 +01:00

35 lines
1.2 KiB
Python

from __future__ import annotations
from range import Range
class Structure:
def __init__(self,
name: str,
bits: int,
ranges: dict[str, Range],
start: int = 0) -> None:
self.name = name
self.bits = bits
self.ranges = ranges
self.start = start
def load(id_: str, data: dict) -> Structure:
ranges = {}
for rSpan, rData in data["ranges"].items():
rStart, rEnd = Range.parseSpan(str(rSpan))
ranges[(rStart, rEnd)] = Range.load(rStart, rEnd, rData)
for range_ in ranges.values():
if range_.values is not None and range_.dependsOn is not None:
bits = ranges[range_.dependsOn].bits
values = {}
for v, d in range_.values.items():
v = str(int(v)).zfill(bits)
values[v] = d
range_.values = values
return Structure(id_, data["bits"], ranges, data.get("start", 0))
def getSortedRanges(self) -> list[Range]:
ranges = self.ranges.values()
return list(sorted(ranges, key=lambda r: r.end))