diff --git a/scripts/pylib/build_helpers/domains.py b/scripts/pylib/build_helpers/domains.py index f4c2f085fd2..475844e9a61 100644 --- a/scripts/pylib/build_helpers/domains.py +++ b/scripts/pylib/build_helpers/domains.py @@ -56,7 +56,15 @@ logger.addHandler(handler) class Domains: - def __init__(self, data): + def __init__(self, domains_yaml): + try: + data = yaml.safe_load(domains_yaml) + pykwalify.core.Core(source_data=data, + schema_data=schema).validate() + except (yaml.YAMLError, pykwalify.errors.SchemaError): + logger.critical(f'malformed domains.yaml') + exit(1) + self._build_dir = data['build_dir'] self._domains = { d['name']: Domain(d['name'], d['build_dir']) @@ -72,32 +80,22 @@ class Domains: @staticmethod def from_file(domains_file): - '''Load domains from domains.yaml. - - Exception raised: - - ``FileNotFoundError`` if the domains file is not found. + '''Load domains from a domains.yaml file. ''' try: with open(domains_file, 'r') as f: - domains = yaml.safe_load(f.read()) + domains_yaml = f.read() except FileNotFoundError: logger.critical(f'domains.yaml file not found: {domains_file}') exit(1) - try: - pykwalify.core.Core(source_data=domains, schema_data=schema)\ - .validate() - except pykwalify.errors.SchemaError: - logger.critical(f'ERROR: Malformed yaml in file: {domains_file}') - exit(1) - - return Domains(domains) + return Domains(domains_yaml) @staticmethod - def from_data(domains_data): - '''Load domains from domains dictionary. + def from_yaml(domains_yaml): + '''Load domains from a string with YAML contents. ''' - return Domains(domains_data) + return Domains(domains_yaml) def get_domains(self, names=None, default_flash_order=False): if names is None: diff --git a/scripts/west_commands/build_helpers.py b/scripts/west_commands/build_helpers.py index 88845e50f4b..ca6845f01da 100644 --- a/scripts/west_commands/build_helpers.py +++ b/scripts/west_commands/build_helpers.py @@ -151,9 +151,14 @@ def load_domains(path): domains_file = Path(path) / 'domains.yaml' if not domains_file.is_file(): - return Domains.from_data({'default': 'app', - 'build_dir': path, - 'domains': [{'name': 'app', 'build_dir': path}], - 'flash_order': ['app']}) + return Domains.from_yaml(f'''\ +default: app +build_dir: {path} +domains: + - name: app + build_dir: {path} +flash_order: + - app +''') return Domains.from_file(domains_file)