Skip to content

Commit cc0927d

Browse files
authored
use HAMMER logger, fix ruamel indents (#705)
* use HAMMER logger, fix ruamel indents * import context type
1 parent 9a3ee52 commit cc0927d

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

hammer/config/config_src.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@
66

77
# pylint: disable=invalid-name
88
import importlib.resources
9+
import json
10+
import numbers
11+
import os
12+
import re
913
from decimal import Decimal
10-
from typing import Iterable, List, Union, Callable, Any, Dict, Set, NamedTuple, Tuple, Optional
11-
from warnings import warn
1214
from enum import Enum
1315
from importlib import resources
16+
from functools import lru_cache, reduce
17+
from typing import (Any, Callable, Dict, Iterable, List, NamedTuple, Optional,
18+
Set, Tuple, Union)
19+
20+
from hammer.logging import HammerVLSILogging, HammerVLSILoggingContext
21+
from hammer.utils import add_dicts, deepdict, topological_sort
1422

15-
from hammer.utils import deepdict, add_dicts, topological_sort
1623
from .yaml2json import load_yaml # grumble grumble
1724

18-
from functools import reduce, lru_cache
19-
import json
20-
import numbers
21-
import os
22-
import re
2325

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

694696
self.defaults = {} # type: dict
695697

698+
self.logger = HammerVLSILogging().context() # type: HammerVLSILoggingContext
699+
696700
@property
697701
def runtime(self) -> List[dict]:
698702
return [self._runtime]
@@ -754,7 +758,7 @@ def get_setting(self, key: str, nullvalue: Any = None, check_type: bool = True)
754758
if key not in self.get_config():
755759
raise KeyError("Key " + key + " is missing")
756760
if key not in self.defaults:
757-
warn(f"Key {key} does not have a default implementation")
761+
self.logger.warning(f"Key {key} does not have a default implementation")
758762
if check_type:
759763
self.check_setting(key)
760764
value = self.get_config()[key]
@@ -782,7 +786,7 @@ def get_setting_suffix(self, key: str, suffix: str, nullvalue: Any = None, check
782786
raise KeyError(f"Both base key: {default} and overriden key: {override} are missing.")
783787

784788
if default not in self.defaults:
785-
warn(f"Base key: {default} does not have a default implementation")
789+
self.logger.warning(f"Base key: {default} does not have a default implementation")
786790
if check_type:
787791
self.check_setting(default)
788792
return nullvalue if value is None else value
@@ -849,7 +853,7 @@ def check_setting(self, key: str, cfg: Optional[dict] = None) -> bool:
849853
if cfg is None:
850854
cfg = self.get_config()
851855
if key not in self.get_config_types():
852-
warn(f"Key {key} is not associated with a type")
856+
self.logger.warning(f"Key {key} is not associated with a type")
853857
return True
854858
try:
855859
exp_value_type = parse_setting_type(self.get_config_types()[key])
@@ -976,7 +980,7 @@ def update_types(self, config_types: List[dict], check_type: bool = True) -> Non
976980
if check_type:
977981
for k, v in loaded_cfg.items():
978982
if not self.has_setting(k):
979-
warn(f"Key {k} has a type {v} is not yet implemented")
983+
self.logger.warning(f"Key {k} has a type {v} is not yet implemented")
980984
elif k != "_config_path":
981985
self.check_setting(k)
982986

hammer/vlsi/cli_driver.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import sys
1111
import tempfile
1212
from pathlib import Path
13-
import warnings
1413
import importlib.resources
1514

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

72-
def dump_config_to_yaml_file(output_path: str, config: Any) -> None:
71+
def dump_config_to_yaml_file(output_path: str, config: dict) -> None:
7372
"""
7473
Helper function to dump the given config in YAML form
7574
to the given output path while overwriting it if it already exists.
7675
:param output_path: Output path
7776
:param config: Config dictionary to dump
7877
"""
7978
yaml = ruamel.yaml.YAML()
80-
yaml.indent(offset=2)
79+
yaml.indent(offset=2, sequence=4)
8180
with open(output_path, 'w') as f:
8281
yaml.dump(config, f)
8382

@@ -328,13 +327,13 @@ def info_action(driver: HammerDriver, append_error_func: Callable[[str], None])
328327
next_level = curr_level[key]
329328
break
330329
except KeyError:
331-
print(f"ERROR: Key {key} could not be found at the current level, try again.")
330+
driver.log.error(f"Key {key} could not be found at the current level, try again.")
332331
overall_key.append(key)
333332
if not isinstance(next_level, ruamel.yaml.CommentedMap):
334333
flat_key = '.'.join(overall_key)
335334
if not driver.database.has_setting(flat_key):
336335
val = curr_level.get(key)
337-
warnings.warn(f"{flat_key} is not in the project configuration, the default value is displayed.")
336+
driver.log.warning(f"{flat_key} is not in the project configuration, the default value is displayed.")
338337
else:
339338
val = driver.database.get_setting(flat_key)
340339
if key in curr_level.ca.items:
@@ -356,7 +355,7 @@ def info_action(driver: HammerDriver, append_error_func: Callable[[str], None])
356355
break
357356
if continue_input.lower() == 'n':
358357
return driver.project_config
359-
print("Please input either [y]es or [n]o.")
358+
driver.log.error("Please input either [y]es or [n]o.")
360359
break
361360
curr_level = next_level
362361

0 commit comments

Comments
 (0)