rivet/range.py

53 lines
1.5 KiB
Python
Raw Normal View History

2023-11-24 13:34:44 +00:00
from __future__ import annotations
2024-03-24 10:33:34 +00:00
2023-11-24 13:34:44 +00:00
from typing import Union
2024-03-24 10:33:34 +00:00
2023-11-24 13:34:44 +00:00
class Range:
def __init__(self,
start: int,
end: int,
name: str,
description: str = "",
values: dict[str, Union[str, dict]] = None,
dependsOn: str = None) -> None:
self.start = start
self.end = end
self.name = name
self.description = description
self.values = values
self.dependsOn = dependsOn
self.lastValueY = -1
2023-11-24 13:34:44 +00:00
@property
def bits(self) -> int:
return self.end - self.start + 1
def load(start: int, end: int, data: dict):
values = None
bits = end - start + 1
if "values" in data:
values = {}
for val, desc in data["values"].items():
val = str(val).zfill(bits)
values[val] = desc
dependsOn = data.get("depends-on", None)
if dependsOn is not None:
dependsOn = Range.parseSpan(str(dependsOn))
return Range(start,
end,
str(data["name"]),
data.get("description", ""),
values,
dependsOn)
def parseSpan(span: str) -> tuple[int, int]:
startEnd = span.split("-")
if len(startEnd) == 1: startEnd.append(startEnd[0])
start = int(startEnd[1])
end = int(startEnd[0])
return (start, end)