1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-01-31 02:00:45 +00:00

utils/misc: Move load_struct_from_yaml() from WA to devlib

This is copied from WA (workload-automation/wa/utils/misc.py).
Hence, published another PR [1] removes the implementation from WA.

OTOH, this patch uses ``ruamel`` instead of ``yaml`` because of the
latter's design issues.

And also this patch fixes pylint issues in ``load_struct_from_yaml()``.

[1] https://github.com/ARM-software/workload-automation/pull/1248

Signed-off-by: Metin Kaya <metin.kaya@arm.com>
This commit is contained in:
Metin Kaya 2024-02-05 10:17:31 +00:00 committed by Marc Bonnici
parent 39dfa7ef72
commit b5715b6560
2 changed files with 26 additions and 0 deletions

View File

@ -23,6 +23,7 @@ from functools import partial, reduce, wraps
from itertools import groupby from itertools import groupby
from operator import itemgetter from operator import itemgetter
from weakref import WeakSet from weakref import WeakSet
from ruamel.yaml import YAML
import ctypes import ctypes
import logging import logging
@ -590,6 +591,30 @@ class LoadSyntaxError(Exception):
return message.format(self.filepath, self.lineno, self.message) return message.format(self.filepath, self.lineno, self.message)
def load_struct_from_yaml(filepath):
"""
Parses a config structure from a YAML file.
The structure should be composed of basic Python types.
:param filepath: Input file which contains YAML data.
:type filepath: str
:raises LoadSyntaxError: if there is a syntax error in YAML data.
:return: A dictionary which contains parsed YAML data
:rtype: Dict
"""
try:
yaml = YAML(typ='safe', pure=True)
with open(filepath, 'r', encoding='utf-8') as file_handler:
return yaml.load(file_handler)
except yaml.YAMLError as ex:
message = ex.message if hasattr(ex, 'message') else ''
lineno = ex.problem_mark.line if hasattr(ex, 'problem_mark') else None
raise LoadSyntaxError(message, filepath=filepath, lineno=lineno) from ex
RAND_MOD_NAME_LEN = 30 RAND_MOD_NAME_LEN = 30
BAD_CHARS = string.punctuation + string.whitespace BAD_CHARS = string.punctuation + string.whitespace
TRANS_TABLE = str.maketrans(BAD_CHARS, '_' * len(BAD_CHARS)) TRANS_TABLE = str.maketrans(BAD_CHARS, '_' * len(BAD_CHARS))

View File

@ -105,6 +105,7 @@ params = dict(
'lxml', # More robust xml parsing 'lxml', # More robust xml parsing
'nest_asyncio', # Allows running nested asyncio loops 'nest_asyncio', # Allows running nested asyncio loops
'future', # for the "past" Python package 'future', # for the "past" Python package
'ruamel.yaml >= 0.15.72', # YAML formatted config parsing
], ],
extras_require={ extras_require={
'daq': ['daqpower>=2'], 'daq': ['daqpower>=2'],