Skip to content

Commit

Permalink
feat: Allow subsetting
Browse files Browse the repository at this point in the history
  • Loading branch information
huyenngn committed Apr 3, 2024
1 parent 990f1c4 commit 52a6324
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions capella_model_explorer/backend/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,14 @@ def read_template(template_name: str):
variable = base["variable"]
below = variable.get("below") or None
attr = variable.get("attr") or None
filters = variable.get("filters") or None
try:
objects = find_objects(
self.model, variable["type"], below=below, attr=attr
self.model,
variable["type"],
below=below,
attr=attr,
filters=filters,
)
base["objects"] = [
{"idx": obj.uuid, "name": obj.name} for obj in objects
Expand Down Expand Up @@ -175,11 +180,23 @@ def index_templates(
return templates_grouped, templates


def find_objects(model, obj_type, below=None, attr=None):
def find_objects(model, obj_type, below=None, attr=None, filters=None):
if attr:
getter = operator.attrgetter(attr)
return getter(model)
if below:
objects = getter(model)
elif below:
getter = operator.attrgetter(below)
return model.search(obj_type, below=getter(model))
return model.search(obj_type)
objects = model.search(obj_type, below=getter(model))
else:
objects = model.search(obj_type)

if filters:
objects = [
obj
for obj in objects
if all(
getattr(obj, key) == value for key, value in filters.items()
)
]

return objects

0 comments on commit 52a6324

Please sign in to comment.