Skip to content

Commit 1a2f7e9

Browse files
committed
fix circular dependency issue with Config
1 parent e6582d3 commit 1a2f7e9

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

chipflow_lib/__init__.py

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import sys
99
import tomli
1010
from pathlib import Path
11-
from pydantic import ValidationError
1211
from typing import TYPE_CHECKING
1312

1413
if TYPE_CHECKING:
@@ -52,8 +51,15 @@ def _ensure_chipflow_root():
5251
return _ensure_chipflow_root.root #type: ignore
5352

5453

54+
def _get_src_loc(src_loc_at=0):
55+
frame = sys._getframe(1 + src_loc_at)
56+
return (frame.f_code.co_filename, frame.f_lineno)
57+
58+
59+
5560
def _parse_config() -> 'Config':
5661
"""Parse the chipflow.toml configuration file."""
62+
from .config import _parse_config_file
5763
chipflow_root = _ensure_chipflow_root()
5864
config_file = Path(chipflow_root) / "chipflow.toml"
5965
try:
@@ -62,29 +68,3 @@ def _parse_config() -> 'Config':
6268
raise ChipFlowError(f"Config file not found. I expected to find it at {config_file}")
6369
except tomli.TOMLDecodeError as e:
6470
raise ChipFlowError(f"TOML Error found when loading {config_file}: {e.msg} at line {e.lineno}, column {e.colno}")
65-
66-
67-
def _parse_config_file(config_file) -> 'Config':
68-
"""Parse a specific chipflow.toml configuration file."""
69-
70-
with open(config_file, "rb") as f:
71-
config_dict = tomli.load(f)
72-
73-
try:
74-
# Validate with Pydantic
75-
return Config.model_validate(config_dict) # Just validate the config_dict
76-
except ValidationError as e:
77-
# Format Pydantic validation errors in a user-friendly way
78-
error_messages = []
79-
for error in e.errors():
80-
location = ".".join(str(loc) for loc in error["loc"])
81-
message = error["msg"]
82-
error_messages.append(f"Error at '{location}': {message}")
83-
84-
error_str = "\n".join(error_messages)
85-
raise ChipFlowError(f"Validation error in chipflow.toml:\n{error_str}")
86-
87-
def _get_src_loc(src_loc_at=0):
88-
frame = sys._getframe(1 + src_loc_at)
89-
return (frame.f_code.co_filename, frame.f_lineno)
90-

chipflow_lib/config.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,38 @@
22
import os
33

44

5+
import tomli
6+
from pydantic import ValidationError
7+
8+
from . import ChipFlowError
9+
from .config_models import Config
10+
511
def get_dir_models():
612
return os.path.dirname(__file__) + "/models"
713

814

915
def get_dir_software():
1016
return os.path.dirname(__file__) + "/software"
17+
18+
19+
def _parse_config_file(config_file) -> 'Config':
20+
"""Parse a specific chipflow.toml configuration file."""
21+
22+
with open(config_file, "rb") as f:
23+
config_dict = tomli.load(f)
24+
25+
try:
26+
# Validate with Pydantic
27+
return Config.model_validate(config_dict) # Just validate the config_dict
28+
except ValidationError as e:
29+
# Format Pydantic validation errors in a user-friendly way
30+
error_messages = []
31+
for error in e.errors():
32+
location = ".".join(str(loc) for loc in error["loc"])
33+
message = error["msg"]
34+
error_messages.append(f"Error at '{location}': {message}")
35+
36+
error_str = "\n".join(error_messages)
37+
raise ChipFlowError(f"Validation error in chipflow.toml:\n{error_str}")
38+
39+

0 commit comments

Comments
 (0)