Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 14 additions & 27 deletions chipflow_lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import sys
import tomli
from pathlib import Path
from pydantic import ValidationError
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from ._config_models import Config

__version__ = importlib.metadata.version("chipflow_lib")

Expand Down Expand Up @@ -44,12 +47,19 @@ def _ensure_chipflow_root():

if os.environ["CHIPFLOW_ROOT"] not in sys.path:
sys.path.append(os.environ["CHIPFLOW_ROOT"])
_ensure_chipflow_root.root = Path(os.environ["CHIPFLOW_ROOT"]).absolute()
return _ensure_chipflow_root.root
_ensure_chipflow_root.root = Path(os.environ["CHIPFLOW_ROOT"]).absolute() #type: ignore
return _ensure_chipflow_root.root #type: ignore


def _get_src_loc(src_loc_at=0):
frame = sys._getframe(1 + src_loc_at)
return (frame.f_code.co_filename, frame.f_lineno)


def _parse_config():

def _parse_config() -> 'Config':
"""Parse the chipflow.toml configuration file."""
from .config import _parse_config_file
chipflow_root = _ensure_chipflow_root()
config_file = Path(chipflow_root) / "chipflow.toml"
try:
Expand All @@ -58,26 +68,3 @@ def _parse_config():
raise ChipFlowError(f"Config file not found. I expected to find it at {config_file}")
except tomli.TOMLDecodeError as e:
raise ChipFlowError(f"TOML Error found when loading {config_file}: {e.msg} at line {e.lineno}, column {e.colno}")


def _parse_config_file(config_file):
"""Parse a specific chipflow.toml configuration file."""
from .config_models import Config

with open(config_file, "rb") as f:
config_dict = tomli.load(f)

try:
# Validate with Pydantic
Config.model_validate(config_dict) # Just validate the config_dict
return config_dict # Return the original dict for backward compatibility
except ValidationError as e:
# Format Pydantic validation errors in a user-friendly way
error_messages = []
for error in e.errors():
location = ".".join(str(loc) for loc in error["loc"])
message = error["msg"]
error_messages.append(f"Error at '{location}': {message}")

error_str = "\n".join(error_messages)
raise ChipFlowError(f"Validation error in chipflow.toml:\n{error_str}")
39 changes: 39 additions & 0 deletions chipflow_lib/_appresponse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from dataclasses import dataclass

from pydantic import BaseModel, PlainSerializer, model_serializer

@dataclass
class OmitIfNone:
pass

class AppResponseModel(BaseModel):
@model_serializer
def _serialize(self):
skip_if_none = set()
serialize_aliases = dict()

# Gather fields that should omit if None
for name, field_info in self.model_fields.items():
if any(
isinstance(metadata, OmitIfNone) for metadata in field_info.metadata
):
skip_if_none.add(name)
elif field_info.serialization_alias:
serialize_aliases[name] = field_info.serialization_alias

serialized = dict()

for name, value in self:
# Skip serializing None if it was marked with "OmitIfNone"
if value is None and name in skip_if_none:
continue
serialize_key = serialize_aliases.get(name, name)

# Run Annotated PlainSerializer
for metadata in self.model_fields[name].metadata:
if isinstance(metadata, PlainSerializer):
value = metadata.func(value)

serialized[serialize_key] = value

return serialized
31 changes: 31 additions & 0 deletions chipflow_lib/_config_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# SPDX-License-Identifier: BSD-2-Clause
from typing import Dict, Optional, Literal, Any, List

from pydantic import BaseModel

from .platforms._utils import Process, PowerConfig

Voltage = float

class SiliconConfig(BaseModel):
"""Configuration for silicon in chipflow.toml."""
process: 'Process'
package: Literal["caravel", "cf20", "pga144"]
power: Dict[str, Voltage] = {}
debug: Optional[Dict[str, bool]] = None

# TODO: add validation that top components, clock domains and power domains
# not begin with '_' (unless power domain _core)
class ChipFlowConfig(BaseModel):
"""Root configuration for chipflow.toml."""
project_name: str
top: Dict[str, Any] = {}
steps: Optional[Dict[str, str]] = None
silicon: Optional[SiliconConfig] = None
clock_domains: Optional[List[str]] = None
power: Optional[PowerConfig] = None


class Config(BaseModel):
"""Root configuration model for chipflow.toml."""
chipflow: ChipFlowConfig
17 changes: 9 additions & 8 deletions chipflow_lib/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ def run(argv=sys.argv[1:]):
commands = {}
commands["pin"] = PinCommand(config)

steps = DEFAULT_STEPS | config["chipflow"]["steps"]
for step_name, step_reference in steps.items():
step_cls = _get_cls_by_reference(step_reference, context=f"step `{step_name}`")
try:
commands[step_name] = step_cls(config)
except Exception:
raise ChipFlowError(f"Encountered error while initializing step `{step_name}` "
f"using `{step_reference}`")
if config.chipflow.steps:
steps = DEFAULT_STEPS |config.chipflow.steps
for step_name, step_reference in steps.items():
step_cls = _get_cls_by_reference(step_reference, context=f"step `{step_name}`")
try:
commands[step_name] = step_cls(config)
except Exception:
raise ChipFlowError(f"Encountered error while initializing step `{step_name}` "
f"using `{step_reference}`")

parser = argparse.ArgumentParser(
prog="chipflow",
Expand Down
29 changes: 29 additions & 0 deletions chipflow_lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,38 @@
import os


import tomli
from pydantic import ValidationError

from . import ChipFlowError
from ._config_models import Config

def get_dir_models():
return os.path.dirname(__file__) + "/models"


def get_dir_software():
return os.path.dirname(__file__) + "/software"


def _parse_config_file(config_file) -> 'Config':
"""Parse a specific chipflow.toml configuration file."""

with open(config_file, "rb") as f:
config_dict = tomli.load(f)

try:
# Validate with Pydantic
return Config.model_validate(config_dict) # Just validate the config_dict
except ValidationError as e:
# Format Pydantic validation errors in a user-friendly way
error_messages = []
for error in e.errors():
location = ".".join(str(loc) for loc in error["loc"])
message = error["msg"]
error_messages.append(f"Error at '{location}': {message}")

error_str = "\n".join(error_messages)
raise ChipFlowError(f"Validation error in chipflow.toml:\n{error_str}")


73 changes: 0 additions & 73 deletions chipflow_lib/config_models.py

This file was deleted.

Loading
Loading