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 054d0a3
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 @@ -75,12 +75,17 @@ def read_template(template_name: str):
if not template_name in self.templates:
return {"error": f"Template {template_name} not found"}
base = self.templates[urlparse.quote(template_name)]
filters = base.get("filters") or None
variable = base["variable"]
below = variable.get("below") or None
attr = variable.get("attr") 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 054d0a3

Please sign in to comment.