libs.faults: add ensure_fault_or_none() and join_faults()
This commit is contained in:
parent
5781869f03
commit
76b859c629
1 changed files with 33 additions and 0 deletions
|
@ -1,6 +1,39 @@
|
||||||
from json import loads, dumps
|
from json import loads, dumps
|
||||||
|
|
||||||
from bundlewrap.metadata import metadata_to_json
|
from bundlewrap.metadata import metadata_to_json
|
||||||
|
from bundlewrap.utils import Fault
|
||||||
|
|
||||||
|
|
||||||
def resolve_faults(dictionary: dict) -> dict:
|
def resolve_faults(dictionary: dict) -> dict:
|
||||||
return loads(metadata_to_json(dictionary))
|
return loads(metadata_to_json(dictionary))
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_fault_or_none(maybe_fault):
|
||||||
|
if maybe_fault is None or isinstance(maybe_fault, Fault):
|
||||||
|
return maybe_fault
|
||||||
|
|
||||||
|
return Fault(maybe_fault, lambda f: f, f=maybe_fault)
|
||||||
|
|
||||||
|
|
||||||
|
def join_faults(faults, by=' '):
|
||||||
|
result = []
|
||||||
|
id_list = []
|
||||||
|
|
||||||
|
for item in faults:
|
||||||
|
result.append(ensure_fault_or_none(item))
|
||||||
|
|
||||||
|
if isinstance(item, Fault):
|
||||||
|
id_list += item.id_list
|
||||||
|
else:
|
||||||
|
id_list.append(item)
|
||||||
|
|
||||||
|
id_list += [
|
||||||
|
'joined_by',
|
||||||
|
by,
|
||||||
|
]
|
||||||
|
|
||||||
|
return Fault(
|
||||||
|
id_list,
|
||||||
|
lambda o: by.join([i.value for i in o]),
|
||||||
|
o=result,
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue