Skip to content

Commit

Permalink
[remote] default serialization
Browse files Browse the repository at this point in the history
demote importlib message from  warning to debug
  • Loading branch information
kammoh committed Jan 28, 2025
1 parent 7d42a38 commit 20d19e7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
62 changes: 47 additions & 15 deletions src/xeda/design.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
from enum import Enum, auto
from functools import cached_property
from pathlib import Path
from typing import Any, Dict, List, Optional, Sequence, Tuple, Type, TypeVar, Union
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Type, TypeVar, Union
import yaml
from urllib.parse import parse_qs, urlparse

import yaml.scanner

from pydantic.fields import ModelField

from .dataclass import (
Field,
Expand Down Expand Up @@ -251,6 +251,16 @@ def __repr__(self) -> str:
s += f" standard: {self.standard}"
return s

def __json_encoder__(self) -> str:
return json.dumps(
{
"file": (self.file),
"type": (self.type),
"variant": (self.variant),
"standard": (self.standard),
}
)


DefineType = Any
# the order matters!
Expand Down Expand Up @@ -520,7 +530,7 @@ class LanguageSettings(XedaBaseModel):
)

@validator("standard", pre=True)
def two_digit_standard(cls, value, values):
def two_digit_standard(cls, value):
if not value:
return None
if isinstance(value, int):
Expand All @@ -529,6 +539,10 @@ def two_digit_standard(cls, value, values):
raise ValueError("standard should be of type string")
return value

@classmethod
def from_version(cls, version: str | int):
return cls(version=cls.two_digit_standard(version)) # type: ignore


class VhdlSettings(LanguageSettings):
synopsys: bool = False
Expand All @@ -538,6 +552,15 @@ class Language(XedaBaseModel):
vhdl: VhdlSettings = VhdlSettings() # type: ignore
verilog: LanguageSettings = LanguageSettings() # type: ignore

@validator("verilog", "vhdl", pre=True, always=True)
def _language_settings(cls, value, field: Optional[ModelField]):
if isinstance(value, (str, int)) and field is not None:
if field.name == "vhdl":
return VhdlSettings.from_version(value)
elif field.name == "verilog":
return LanguageSettings.from_version(value)
return value


class RtlDep(XedaBaseModel):
pos: int = 0
Expand Down Expand Up @@ -708,7 +731,11 @@ class Design(XedaBaseModel):
dependencies: List[DesignReference] = []
rtl: RtlSettings
tb: TbSettings = TbSettings() # type: ignore
language: Language = Language()
language: Language = Field(
Language(),
alias="hdl",
description="HDL language settings",
)
flow: Dict[str, Dict[str, Any]] = Field(
dict(),
alias="flows",
Expand Down Expand Up @@ -1038,19 +1065,24 @@ def tb_hash(self) -> str:
return hashlib.sha3_256(r).hexdigest()

# pylint: disable=arguments-differ
def dict(
self,
*,
include=None,
exclude=None,
by_alias: bool = False,
skip_defaults: Optional[bool] = None,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
) -> Dict[str, Any]:
def dict(self) -> Dict[str, Any]:
return super().dict(
exclude_unset=True,
exclude_defaults=True,
exclude={"rtl_hash", "tb_hash", "rtl_fingerprint"},
)

def json(
self,
encoder: Optional[Callable[[Any], Any]] = None,
models_as_dict: bool = True,
**dumps_kwargs,
) -> str:
return super().json(
exclude_unset=True,
exclude_defaults=True,
exclude={"rtl_hash", "tb_hash", "rtl_fingerprint"},
encoder=encoder,
models_as_dict=models_as_dict,
**dumps_kwargs,
)
4 changes: 2 additions & 2 deletions src/xeda/flow_runner/default_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ def get_flow_class(
) -> Type[Flow]:
_mod, flow_class = registered_flows.get(flow_name, (None, None))
if flow_class is None:
log.warning(
"Flow %s was not found in registered flows. Trying to load using importlib.import_module",
log.debug(
"Flow %s was not found in registered flows. Trying to load using `importlib`.",
flow_name,
)
try:
Expand Down
10 changes: 9 additions & 1 deletion src/xeda/flow_runner/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ def translate_filename(src: DesignSource) -> str:
new_design["flow"] = design.flow
design_file = temp_dir / f"{design.name}.xeda.json"
with open(design_file, "w") as f:
json.dump(new_design, f)
json.dump(
new_design,
f,
default=lambda obj: (
obj.__json_encoder__
if hasattr(obj, "__json_encoder__")
else obj.__dict__ if hasattr(obj, "__dict__") else str(obj)
),
)
with zipfile.ZipFile(zip_file, mode="w") as archive:
for src in design.sources_of_type("*", rtl=True, tb=True):
archive.write(src.path, arcname=remote_sources_path / translate_filename(src))
Expand Down

0 comments on commit 20d19e7

Please sign in to comment.