Skip to content

Commit 5918258

Browse files
committed
utils/misc: Move load_struct_from_yaml() from WA to devlib
This is copied from ``workload-automation``/wa/utils/misc.py. [1] removes the implementation from WA. Replace ``yaml.load()`` with ``yaml.safe_load()``. And fix pylint issues. [1] TODO: paste WA PR link here. Signed-off-by: Metin Kaya <metin.kaya@arm.com>
1 parent 16b64c8 commit 5918258

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

devlib/utils/misc.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import types
3838
import warnings
3939
import wrapt
40+
import yaml
4041

4142

4243
try:
@@ -590,6 +591,30 @@ def __str__(self):
590591
return message.format(self.filepath, self.lineno, self.message)
591592

592593

594+
def load_struct_from_yaml(filepath):
595+
'''
596+
Parses a config structure from a YAML file.
597+
The structure should be composed of basic Python types.
598+
599+
Args:
600+
filepath (str): Input file which contains YAML data.
601+
602+
Raises:
603+
LoadSyntaxError: if there is a syntax error in YAML data.
604+
605+
Returns:
606+
dict: which contains parsed YAML data.
607+
'''
608+
609+
try:
610+
with open(filepath, 'r', encoding='utf-8') as file_handler:
611+
return yaml.safe_load(file_handler)
612+
except yaml.YAMLError as ex:
613+
message = ex.message if hasattr(ex, 'message') else ''
614+
lineno = ex.problem_mark.line if hasattr(ex, 'problem_mark') else None
615+
raise LoadSyntaxError(message, filepath=filepath, lineno=lineno) from ex
616+
617+
593618
RAND_MOD_NAME_LEN = 30
594619
BAD_CHARS = string.punctuation + string.whitespace
595620
TRANS_TABLE = str.maketrans(BAD_CHARS, '_' * len(BAD_CHARS))

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def _load_path(filepath):
105105
'lxml', # More robust xml parsing
106106
'nest_asyncio', # Allows running nested asyncio loops
107107
'future', # for the "past" Python package
108+
'pyYAML>=5.1b3', # YAML formatted config parsing
108109
],
109110
extras_require={
110111
'daq': ['daqpower>=2'],

0 commit comments

Comments
 (0)