Skip to content

Commit

Permalink
[ISAC]: Replaces the use of round_trip_dump, which has been deprecate…
Browse files Browse the repository at this point in the history
…d 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 <[email protected]>
  • Loading branch information
trdthg and UmerShahidengr authored Nov 7, 2024
1 parent ca8f7a9 commit 6084ed8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
8 changes: 4 additions & 4 deletions riscv-isac/riscv_isac/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
39 changes: 27 additions & 12 deletions riscv-isac/riscv_isac/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# See LICENSE.incore for details

"""Common Utils """
import io
import sys
import os
import subprocess
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 6084ed8

Please sign in to comment.