scripts: domains: Support initialization only from YAML

By requiring a full-on YAML fragment, the Domains class can rely on
pykwalify to do the heavy lifting with validating types and structure.
Remove support for loading domains from a dictionary, so that we don't
ever have to replicate the same checks on the input, and we can instead
have a single entry point.

Signed-off-by: Grzegorz Swiderski <grzegorz.swiderski@nordicsemi.no>
This commit is contained in:
Grzegorz Swiderski 2023-09-28 09:36:18 +02:00 committed by Carles Cufí
commit 652b0a8a98
2 changed files with 24 additions and 21 deletions

View file

@ -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:

View file

@ -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)