rivet/range.py

57 lines
1.7 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
2024-04-12 21:33:09 +00:00
from typing import Union, Optional
2023-11-24 13:34:44 +00:00
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 = "",
2024-04-12 21:33:09 +00:00
values: Optional[dict[str, Union[str, dict]]] = None,
dependsOn: Optional[tuple[int, int]] = None) -> None:
2023-11-24 13:34:44 +00:00
2024-04-12 21:33:09 +00:00
self.start: int = start
self.end: int = end
self.name: str = name
self.description: str = description
self.values: Optional[dict[str, Union[str, dict]]] = values
self.dependsOn: Optional[tuple[int, int]] = dependsOn
self.lastValueY: int = -1
2023-11-24 13:34:44 +00:00
@property
def bits(self) -> int:
return self.end - self.start + 1
2024-04-12 21:33:09 +00:00
@staticmethod
def load(start: int, end: int, data: dict) -> Range:
2023-11-24 13:34:44 +00:00
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)
2024-04-12 21:33:09 +00:00
@staticmethod
2023-11-24 13:34:44 +00:00
def parseSpan(span: str) -> tuple[int, int]:
startEnd = span.split("-")
2024-04-12 21:33:09 +00:00
if len(startEnd) == 1:
startEnd.append(startEnd[0])
2023-11-24 13:34:44 +00:00
start = int(startEnd[1])
end = int(startEnd[0])
2024-04-12 21:33:09 +00:00
return (start, end)