Skip to content

Commit

Permalink
manifest: refactor _is_submodule_dict_ok(); add nested _assert()
Browse files Browse the repository at this point in the history
Add nested _assert() function that raises an exception and reduces
"return False" duplication. Because of the added scaffolding this adds
about 10 lines, on other hand it brings all conditions together in one
place, removes negations and makes it easier to add conditions.

Signed-off-by: Marc Herbert <[email protected]>
  • Loading branch information
marc-hb authored and mbolivar-nordic committed Feb 9, 2021
1 parent fac7c84 commit 8e72a9c
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/west/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,18 340,29 @@ def _update_disabled_groups(disabled_groups: Set[str],
"along with as much information as you can, such as the "
"stack trace that preceded this message.")

def _is_submodule_dict_ok(value: Any) -> bool:
# Check whether value is a dict that contains the expected
def _is_submodule_dict_ok(subm: Any) -> bool:
# Check whether subm is a dict that contains the expected
# submodule fields of proper types.

if not isinstance(value, dict):
return False
class _failed(Exception):
pass

def _assert(cond):
if not cond:
raise _failed()

if len(value) != 2:
return False # Not enough keys, or too many.
try:
_assert(isinstance(subm, dict))
_assert(len(subm) == 2)
# Allowed keys
for k in subm:
_assert(k in ['path', 'name'])
_assert(isinstance(subm[k], str))

except _failed:
return False

return ('name' in value and isinstance(value['name'], str) and
'path' in value and isinstance(value['path'], str))
return True

#
# Public functions
Expand Down

0 comments on commit 8e72a9c

Please sign in to comment.