Skip to content

Commit

Permalink
hierachical keys in design read from json
Browse files Browse the repository at this point in the history
  • Loading branch information
kammoh committed Jun 17, 2023
1 parent 8adfbfe commit 857319a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/xeda/design.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@
validation_errors,
validator,
)
from .utils import WorkingDirectory, hierarchical_merge, removeprefix, settings_to_dict, toml_load
from .utils import (
WorkingDirectory,
expand_hierarchy,
hierarchical_merge,
removeprefix,
settings_to_dict,
toml_load,
)

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -596,10 +603,8 @@ class Design(XedaBaseModel):

@validator("flow", pre=True, always=True)
def _flow_settings(cls, value):
print(f"in flow validator value={value} {type(value)}")
if value:
value = settings_to_dict(value)
print(f"in flow validator value set to {value} {type(value)}")
return value

@validator("dependencies", pre=True, always=True)
Expand Down Expand Up @@ -746,6 +751,7 @@ def from_file(
elif design_file.suffix == ".json":
with open(design_file, "r") as f:
design_dict = json.load(f)
design_dict = expand_hierarchy(design_dict)
else:
raise ValueError(f"File extension `{design_file.suffix}` is not supported.")
design_dict = hierarchical_merge(design_dict, overrides)
Expand Down
15 changes: 8 additions & 7 deletions src/xeda/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from typing import Any, Dict, Iterable, List, Optional, OrderedDict, Tuple, Type, TypeVar, Union
import unittest
from xml.etree import ElementTree
from git import Sequence
from pyparsing import Mapping

from varname import argname
Expand Down Expand Up @@ -277,7 +276,6 @@ def get_hierarchy(dct: Dict[str, Any], path):


def set_hierarchy(dct: Dict[str, Any], path, value):
print(f"set_hierarchy k={path} v={value}")
if isinstance(path, str):
path = re.split(SEP, path)
k = path[0]
Expand All @@ -292,7 +290,6 @@ def set_hierarchy(dct: Dict[str, Any], path, value):
if k not in dct:
dct[k] = {}
set_hierarchy(dct[k], path[1:], value)
print(f"set_hierarchy dct={dct}")


def append_flag(flag_list: List[str], flag: str) -> List[str]:
Expand Down Expand Up @@ -474,6 +471,13 @@ def conv(v):
return v


def expand_hierarchy(d: Dict[str, Any]) -> Dict[str, Any]:
expanded: DictStrHier = {}
for k, v in d.items():
set_hierarchy(expanded, k, conv(v))
return expanded


def settings_to_dict(
settings: Union[List[str], Tuple[str, ...], Mapping[str, StrOrDictStrHier]],
hierarchical_keys: bool = True,
Expand All @@ -496,8 +500,5 @@ def settings_to_dict(
if isinstance(settings, dict):
if not hierarchical_keys:
return settings
expanded: DictStrHier = {}
for k, v in settings.items():
set_hierarchy(expanded, k, conv(v))
return expanded
return expand_hierarchy(settings)
raise TypeError(f"Unsupported type: {type(settings)}")

0 comments on commit 857319a

Please sign in to comment.