Skip to content

Commit

Permalink
use HAMMER logger, fix ruamel indents (#705)
Browse files Browse the repository at this point in the history
* use HAMMER logger, fix ruamel indents

* import context type
  • Loading branch information
bdngo authored Feb 1, 2023
1 parent 9a3ee52 commit cc0927d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
28 changes: 16 additions & 12 deletions hammer/config/config_src.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@

# pylint: disable=invalid-name
import importlib.resources
import json
import numbers
import os
import re
from decimal import Decimal
from typing import Iterable, List, Union, Callable, Any, Dict, Set, NamedTuple, Tuple, Optional
from warnings import warn
from enum import Enum
from importlib import resources
from functools import lru_cache, reduce
from typing import (Any, Callable, Dict, Iterable, List, NamedTuple, Optional,
Set, Tuple, Union)

from hammer.logging import HammerVLSILogging, HammerVLSILoggingContext
from hammer.utils import add_dicts, deepdict, topological_sort

from hammer.utils import deepdict, add_dicts, topological_sort
from .yaml2json import load_yaml # grumble grumble

from functools import reduce, lru_cache
import json
import numbers
import os
import re

# A helper class that writes Decimals as strings
# TODO(ucb-bar/hammer#378) get rid of this and serialize units
Expand Down Expand Up @@ -693,6 +695,8 @@ def __init__(self) -> None:

self.defaults = {} # type: dict

self.logger = HammerVLSILogging().context() # type: HammerVLSILoggingContext

@property
def runtime(self) -> List[dict]:
return [self._runtime]
Expand Down Expand Up @@ -754,7 +758,7 @@ def get_setting(self, key: str, nullvalue: Any = None, check_type: bool = True)
if key not in self.get_config():
raise KeyError("Key " + key + " is missing")
if key not in self.defaults:
warn(f"Key {key} does not have a default implementation")
self.logger.warning(f"Key {key} does not have a default implementation")
if check_type:
self.check_setting(key)
value = self.get_config()[key]
Expand Down Expand Up @@ -782,7 +786,7 @@ def get_setting_suffix(self, key: str, suffix: str, nullvalue: Any = None, check
raise KeyError(f"Both base key: {default} and overriden key: {override} are missing.")

if default not in self.defaults:
warn(f"Base key: {default} does not have a default implementation")
self.logger.warning(f"Base key: {default} does not have a default implementation")
if check_type:
self.check_setting(default)
return nullvalue if value is None else value
Expand Down Expand Up @@ -849,7 +853,7 @@ def check_setting(self, key: str, cfg: Optional[dict] = None) -> bool:
if cfg is None:
cfg = self.get_config()
if key not in self.get_config_types():
warn(f"Key {key} is not associated with a type")
self.logger.warning(f"Key {key} is not associated with a type")
return True
try:
exp_value_type = parse_setting_type(self.get_config_types()[key])
Expand Down Expand Up @@ -976,7 +980,7 @@ def update_types(self, config_types: List[dict], check_type: bool = True) -> Non
if check_type:
for k, v in loaded_cfg.items():
if not self.has_setting(k):
warn(f"Key {k} has a type {v} is not yet implemented")
self.logger.warning(f"Key {k} has a type {v} is not yet implemented")
elif k != "_config_path":
self.check_setting(k)

Expand Down
11 changes: 5 additions & 6 deletions hammer/vlsi/cli_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import sys
import tempfile
from pathlib import Path
import warnings
import importlib.resources

import ruamel.yaml
Expand Down Expand Up @@ -69,15 +68,15 @@ def dump_config_to_json_file(output_path: str, config: dict) -> None:
with open(output_path, "w") as f:
f.write(json.dumps(config, cls=HammerJSONEncoder, indent=4))

def dump_config_to_yaml_file(output_path: str, config: Any) -> None:
def dump_config_to_yaml_file(output_path: str, config: dict) -> None:
"""
Helper function to dump the given config in YAML form
to the given output path while overwriting it if it already exists.
:param output_path: Output path
:param config: Config dictionary to dump
"""
yaml = ruamel.yaml.YAML()
yaml.indent(offset=2)
yaml.indent(offset=2, sequence=4)
with open(output_path, 'w') as f:
yaml.dump(config, f)

Expand Down Expand Up @@ -328,13 +327,13 @@ def info_action(driver: HammerDriver, append_error_func: Callable[[str], None])
next_level = curr_level[key]
break
except KeyError:
print(f"ERROR: Key {key} could not be found at the current level, try again.")
driver.log.error(f"Key {key} could not be found at the current level, try again.")
overall_key.append(key)
if not isinstance(next_level, ruamel.yaml.CommentedMap):
flat_key = '.'.join(overall_key)
if not driver.database.has_setting(flat_key):
val = curr_level.get(key)
warnings.warn(f"{flat_key} is not in the project configuration, the default value is displayed.")
driver.log.warning(f"{flat_key} is not in the project configuration, the default value is displayed.")
else:
val = driver.database.get_setting(flat_key)
if key in curr_level.ca.items:
Expand All @@ -356,7 +355,7 @@ def info_action(driver: HammerDriver, append_error_func: Callable[[str], None])
break
if continue_input.lower() == 'n':
return driver.project_config
print("Please input either [y]es or [n]o.")
driver.log.error("Please input either [y]es or [n]o.")
break
curr_level = next_level

Expand Down

0 comments on commit cc0927d

Please sign in to comment.