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:
parent
39dfa7ef72
commit
b5715b6560
@ -23,6 +23,7 @@ from functools import partial, reduce, wraps
|
||||
from itertools import groupby
|
||||
from operator import itemgetter
|
||||
from weakref import WeakSet
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
import ctypes
|
||||
import logging
|
||||
@ -590,6 +591,30 @@ class LoadSyntaxError(Exception):
|
||||
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
|
||||
BAD_CHARS = string.punctuation + string.whitespace
|
||||
TRANS_TABLE = str.maketrans(BAD_CHARS, '_' * len(BAD_CHARS))
|
||||
|
Loading…
x
Reference in New Issue
Block a user