in azext_iot/digitaltwins/providers/model.py [0:0]
def get_model_dependencies(model, model_id_to_model_map=None):
"""Return a list of dependency DTMIs for a given model"""
dependencies = []
# Add everything that would have dependency DTMIs, worry about flattening later
if "contents" in model:
components = [item["schema"] for item in model["contents"] if item["@type"] == "Component"]
dependencies.extend(components)
if "extends" in model:
dependencies.append(model['extends'])
# Go through gathered items, get the DTMI references, and flatten if needed
no_dup = set()
for item in dependencies:
# Models defined in a DTDL can implement extensions of up to two interfaces.
# These interfaces can be in the form of a DTMI reference, or a nested model.
if isinstance(item, str):
# If its just a string, thats a single DTMI reference, so just add that to our set
no_dup.add(item)
# Calculate recursive dependencies if model id to model map is passed
if model_id_to_model_map is not None:
dep_model = model_id_to_model_map[item]
no_dup.update(set(get_model_dependencies(dep_model, model_id_to_model_map)))
elif isinstance(item, dict):
# If its a single nested model, get its dtmi reference, dependencies and add them
no_dup.update(set(get_model_dependencies(item, model_id_to_model_map)))
elif isinstance(item, list):
# If its a list, could have DTMIs or nested models
for sub_item in item:
if isinstance(sub_item, str):
# If there are strings in the list, that's a DTMI reference, so add it
no_dup.add(sub_item)
# Calculate recursive dependencies if model id to model map is passed
if model_id_to_model_map is not None:
sub_dep_model = model_id_to_model_map[sub_item]
no_dup.update(set(get_model_dependencies(sub_dep_model, model_id_to_model_map)))
elif isinstance(sub_item, dict):
# This is a nested model. Now go get its dependencies and add them
no_dup.update(set(get_model_dependencies(sub_item, model_id_to_model_map)))
return list(no_dup)