from __future__ import annotations from range import Range class Structure: def __init__(self, name: str, bits: int, ranges: dict[tuple[int, int], Range], start: int = 0) -> None: self.name: str = name self.bits: int = bits self.ranges: dict[tuple[int, int], Range] = ranges self.start: int = start @staticmethod 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_, int(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))