manifest: accept strings as Manifest.from_data args

It's convenient when using this API to be able to pass a string
containing YAML data instead of requiring the caller to load the YAML
data every time.

Signed-off-by: Marti Bolivar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Marti Bolivar 2019-10-08 09:38:10 -07:00 committed by Carles Cufí
parent cf3e3c08d7
commit 428ef11b38
2 changed files with 15 additions and 1 deletions

View File

@ -142,7 +142,8 @@ class Manifest:
def from_data(source_data, manifest_path=None, topdir=None):
'''Create and return a new Manifest object given parsed YAML data.
:param source_data: Parsed YAML data as a Python object.
:param source_data: Parsed YAML data as a Python object,
or a string with YAML data inside.
:param manifest_path: fallback ManifestProject path attribute
:param topdir: If given, absolute paths in the result will be
rooted here.
@ -163,6 +164,8 @@ class Manifest:
Raises MalformedManifest in case of validation errors.
'''
if isinstance(source_data, str):
source_data = yaml.safe_load(source_data)
return Manifest(source_data=source_data, topdir=topdir,
manifest_path=manifest_path)

View File

@ -757,6 +757,17 @@ def test_get_projects_unknown():
with pytest.raises(ValueError):
manifest.get_projects(['unknown'])
def test_load_str():
# We can load manifest data as a string.
manifest = Manifest.from_data('''\
manifest:
projects:
- name: foo
url: https://foo.com
''')
assert manifest.projects[-1].name == 'foo'
# Invalid manifests should raise MalformedManifest.
@pytest.mark.parametrize('invalid',