From 6084ed82e7aaf783c5af292873a29aaa19caf2f6 Mon Sep 17 00:00:00 2001 From: MingZhu Yan <69898423+trdthg@users.noreply.github.com> Date: Thu, 7 Nov 2024 13:34:30 +0800 Subject: [PATCH] [ISAC]: Replaces the use of round_trip_dump, which has been deprecated since 0.18.0. (#549) - replace deprecated `ruamel.yaml.round_trip_dump` with `utils.dump_yaml`. - `dump_yaml` will return string if outfile is None. - Add two new params to dump_yaml: `indent` and `block_seq_indent` for compatibility with previous usage. Co-authored-by: Umer Shahid --- riscv-isac/riscv_isac/coverage.py | 8 +++---- riscv-isac/riscv_isac/utils.py | 39 +++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/riscv-isac/riscv_isac/coverage.py b/riscv-isac/riscv_isac/coverage.py index 57e3f6cb4..470837573 100644 --- a/riscv-isac/riscv_isac/coverage.py +++ b/riscv-isac/riscv_isac/coverage.py @@ -693,9 +693,9 @@ def __add__(self, o): return temp -def pretty_print_yaml(yaml): +def pretty_print_yaml(yaml_obj): res = '''''' - for line in ruamel.yaml.round_trip_dump(yaml, indent=5, block_seq_indent=3).splitlines(True): + for line in utils.dump_yaml(yaml_obj, indent=5, block_seq_indent=3).splitlines(True): res += line return res @@ -1524,7 +1524,7 @@ def compute(trace_file, test_name, cgf, parser_name, decoder_name, detailed, xle if dump is not None: dump_f = open(dump, 'w') - dump_f.write(ruamel.yaml.round_trip_dump(cgf, indent=5, block_seq_indent=3)) + dump_f.write(utils.dump_yaml(cgf, indent=5, block_seq_indent=3)) dump_f.close() sys.exit(0) @@ -1686,7 +1686,7 @@ def compute(trace_file, test_name, cgf, parser_name, decoder_name, detailed, xle rpt_str = gen_report(rcgf, detailed) logger.info('Writing out updated cgf : ' + test_name + '.cgf') dump_file = open(test_name+'.cgf', 'w') - dump_file.write(ruamel.yaml.round_trip_dump(rcgf, indent=5, block_seq_indent=3)) + dump_file.write(utils.dump_yaml(rcgf, indent=5, block_seq_indent=3)) dump_file.close() if sig_addrs: diff --git a/riscv-isac/riscv_isac/utils.py b/riscv-isac/riscv_isac/utils.py index 200a6267e..1817f5b13 100644 --- a/riscv-isac/riscv_isac/utils.py +++ b/riscv-isac/riscv_isac/utils.py @@ -1,6 +1,7 @@ # See LICENSE.incore for details """Common Utils """ +import io import sys import os import subprocess @@ -13,17 +14,21 @@ import yaml as pyyaml from elftools.elf.elffile import ELFFile -yaml = YAML(typ="rt") -yaml.default_flow_style = False -yaml.explicit_start = True -yaml.allow_unicode = True -yaml.allow_duplicate_keys = False +def create_yaml(typ="rt", indent=None, block_seq_indent=None): + yaml = YAML(typ=typ) + yaml.default_flow_style = False + yaml.explicit_start = True + yaml.allow_unicode = True + yaml.allow_duplicate_keys = False + if indent is not None: + yaml.indent = indent + if block_seq_indent is not None: + yaml.block_seq_indent = block_seq_indent + return yaml -safe_yaml = YAML(typ="safe") -safe_yaml.default_flow_style = False -safe_yaml.explicit_start = True -safe_yaml.allow_unicode = True -safe_yaml.allow_duplicate_keys = False +yaml = create_yaml() + +safe_yaml = create_yaml(typ="safe") def collect_label_address(elf, label): with open(elf, 'rb') as f: @@ -47,8 +52,18 @@ def get_value_at_location(elf_path, location, bytes): return int.from_bytes(value, byteorder='little', signed=False) return None -def dump_yaml(foo, outfile): - yaml.dump(foo, outfile) +def dump_yaml(foo, outfile = None, indent = None, block_seq_indent = None): + """ + Dump yaml to outfile. If outfile is None, dump to string. If indent or + block_seq_indent is set, create a new yaml object with suchconfig. + """ + if indent is not None or block_seq_indent is not None: + yaml = create_yaml(indent=indent, block_seq_indent=block_seq_indent) + if outfile is None: + buf = io.StringIO() + yaml.dump(foo, buf) + return buf.getvalue() + return yaml.dump(foo, outfile) def load_yaml_file(foo): try: