mirror of
https://github.com/TinyTapeout/tt-support-tools.git
synced 2026-11-03 04:36:09 +01:00
refactor: add more types
include types for the new info.yaml
This commit is contained in:
+112
@@ -0,0 +1,112 @@
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from tile_sizes import tile_sizes
|
||||
|
||||
YAML_VERSION = 6
|
||||
|
||||
|
||||
class ProjectYamlError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class PinoutSection:
|
||||
def __init__(self, yaml_data: Dict[str, Any]):
|
||||
self.ui = self._pins(yaml_data, "ui", 8)
|
||||
self.uo = self._pins(yaml_data, "uo", 8)
|
||||
self.uio = self._pins(yaml_data, "uio", 8)
|
||||
|
||||
def _pins(self, yaml_data: Dict[str, Any], name: str, count: int) -> List[str]:
|
||||
result: List[str] = []
|
||||
for i in range(count):
|
||||
pin = yaml_data.get(f"{name}[{i}]")
|
||||
if pin is None:
|
||||
raise ProjectYamlError(f"Missing '{name}[{i}]' in 'pinout' section")
|
||||
result.append(pin)
|
||||
return result
|
||||
|
||||
|
||||
class ProjectInfo:
|
||||
def __init__(self, yaml_data: Dict[str, Any]):
|
||||
# Validate Version
|
||||
yaml_version = yaml_data.get("yaml_version")
|
||||
if yaml_version is None:
|
||||
raise ProjectYamlError("Missing 'yaml_version'")
|
||||
if yaml_version != YAML_VERSION:
|
||||
raise ProjectYamlError(
|
||||
f"Unsupported YAML version: {yaml_data['yaml_version']}, expected {YAML_VERSION}"
|
||||
)
|
||||
|
||||
# Read "project" Section
|
||||
project_section: Optional[Dict[str, Any]] = yaml_data.get("project")
|
||||
if project_section is None:
|
||||
raise ProjectYamlError("Missing 'project' section")
|
||||
|
||||
title = project_section.get("title")
|
||||
if title is None:
|
||||
raise ProjectYamlError("Missing key 'title' in 'project' section")
|
||||
if title == "":
|
||||
raise ProjectYamlError("Project title cannot be empty")
|
||||
self.title: str = title
|
||||
|
||||
author = project_section.get("author")
|
||||
if author is None:
|
||||
raise ProjectYamlError("Missing key 'author' in 'project' section")
|
||||
if author == "":
|
||||
raise ProjectYamlError("Project author cannot be empty")
|
||||
self.author: str = author
|
||||
|
||||
description = project_section.get("description")
|
||||
if description is None:
|
||||
raise ProjectYamlError("Missing key 'description' in 'project' section")
|
||||
if description == "":
|
||||
raise ProjectYamlError("Project description cannot be empty")
|
||||
self.description: str = description
|
||||
|
||||
tiles = project_section.get("tiles")
|
||||
if tiles is None:
|
||||
raise ProjectYamlError("Missing key 'tiles' in 'project' section")
|
||||
if tiles not in tile_sizes.keys():
|
||||
raise ProjectYamlError(
|
||||
f"Invalid value for 'tiles' in 'project' section: {tiles}"
|
||||
)
|
||||
self.tiles: str = tiles
|
||||
|
||||
language = project_section.get("language")
|
||||
if language is None:
|
||||
raise ProjectYamlError("Missing key 'language' in 'project' section")
|
||||
if language == "":
|
||||
raise ProjectYamlError("Project language cannot be empty")
|
||||
self.language: str = language
|
||||
|
||||
if language == "Wokwi":
|
||||
wokwi_id = project_section.get("wokwi_id")
|
||||
if wokwi_id is None:
|
||||
raise ProjectYamlError("Missing key 'wokwi_id' in 'project' section")
|
||||
if wokwi_id == "" or wokwi_id == "0":
|
||||
raise ProjectYamlError("Please provide a valid Wokwi project ID")
|
||||
self.wokwi_id: Optional[int] = wokwi_id
|
||||
self.top_module = f"tt_um_wokwi_{wokwi_id}"
|
||||
self.source_files = [f"tt_um_wokwi_{self.wokwi_id}.v", f"cells.v"]
|
||||
else:
|
||||
top_module = project_section.get("top_module")
|
||||
if top_module is None:
|
||||
raise ProjectYamlError("Missing key 'top_module' in 'project' section")
|
||||
if not top_module.startswith("tt_um_"):
|
||||
raise ProjectYamlError(
|
||||
"Top module must start with 'tt_um_' (e.g. tt_um_my_project)"
|
||||
)
|
||||
self.top_module: str = top_module
|
||||
|
||||
if "source_files" not in project_section:
|
||||
raise ProjectYamlError(
|
||||
"Missing key 'source_files' in 'project' section"
|
||||
)
|
||||
if len(project_section["source_files"]) == 0:
|
||||
raise ProjectYamlError("No source files specified")
|
||||
self.source_files: List[str] = project_section["source_files"]
|
||||
|
||||
self.discord: Optional[str] = project_section.get("discord")
|
||||
self.clock_hz: Optional[int] = project_section["clock_hz"]
|
||||
if "pinout" not in yaml_data:
|
||||
raise ProjectYamlError("Missing 'pinout' section")
|
||||
self.pinout = PinoutSection(yaml_data["pinout"])
|
||||
Reference in New Issue
Block a user