Skip to content

Commit

Permalink
fix: Single page templates work again
Browse files Browse the repository at this point in the history
  • Loading branch information
vik378 committed Jul 5, 2024
1 parent d3d5a4d commit 1d9196c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion capella_model_explorer/backend/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import fastapi
import markupsafe
import prometheus_client
import yaml # type: ignore
import yaml
from fastapi import APIRouter, FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
Expand Down
28 changes: 15 additions & 13 deletions capella_model_explorer/backend/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import operator
import traceback
from pathlib import Path
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, TypedDict

import capellambse
import yaml # type: ignore
import yaml
from pydantic import BaseModel, Field


Expand Down Expand Up @@ -74,7 +74,9 @@ class Template(BaseModel):
template: Path = Field(..., title="Template File Path")
description: str = Field(..., title="Template Description")

scope: Optional[TemplateScope] = Field({}, title="Template Scope")
scope: Optional[TemplateScope] = Field(
default_factory=dict, title="Template Scope"
)
single: Optional[bool] = Field(None, title="Single Instance Flag")
isStable: Optional[bool] = Field(None, title="Stable Template Flag")
isDocument: Optional[bool] = Field(None, title="Document Template Flag")
Expand All @@ -84,9 +86,7 @@ class Template(BaseModel):
error: Optional[str] = Field(None, title="Broken Template Flag")
traceback: Optional[str] = Field(None, title="Template Error Traceback")
instanceCount: Optional[int] = Field(None, title="Number of Instances")
instanceList: Optional[List[InstanceDetails]] = Field(
None, title="List of Instances"
)
instanceList: Optional[List[Dict]] = Field(None, title="List of Instances")

def find_instances(self, model: capellambse.MelodyModel):
if self.single:
Expand All @@ -103,7 +103,6 @@ def find_instances(self, model: capellambse.MelodyModel):
filters=self.scope.filters,
)
else:
self.error = "Template scope not defined"
return []
except Exception as e:
self.error = f"Template scope error: {e}"
Expand All @@ -115,14 +114,15 @@ def compute_instance_count(self, model: capellambse.MelodyModel):

def compute_instance_list(self, model: capellambse.MelodyModel):
self.instanceList = [
InstanceDetails(**simple_object(obj))
for obj in self.find_instances(model)
simple_object(obj) for obj in self.find_instances(model)
]


class TemplateCategory(BaseModel):
idx: str = Field(..., title="Category Identifier")
templates: List[Template] = Field([], title="Templates in this category")
templates: List[Template] = Field(
default_factory=list, title="Templates in this category"
)

def __add__(self, other):
if not isinstance(other, TemplateCategory):
Expand All @@ -133,7 +133,9 @@ def __add__(self, other):


class TemplateCategories(BaseModel):
categories: List[TemplateCategory] = Field([], title="Template Categories")
categories: List[TemplateCategory] = Field(
default_factory=list, title="Template Categories"
)

def __getitem__(self, category_id: str):
results = [cat for cat in self.categories if cat.idx == category_id]
Expand Down Expand Up @@ -179,10 +181,10 @@ def flat(self):
class TemplateLoader:
def __init__(self, model: capellambse.MelodyModel) -> None:
self.model = model
self.templates = TemplateCategories(categories=[])
self.templates = TemplateCategories()

def index_path(self, path: Path) -> TemplateCategories:
self.templates = TemplateCategories(categories=[]) # reset templates
self.templates = TemplateCategories() # reset templates
for template_file in path.glob("**/*.yaml"):
templates_data = yaml.safe_load(
template_file.read_text(encoding="utf8")
Expand Down
3 changes: 0 additions & 3 deletions templates/system-analysis/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ categories:
single: true
isExperimental: true
isDocument: true
scope:
type: SystemComponent
below: sa
- template: system-analysis/system-interface.html.j2
idx: system-interfaces
name: System Interfaces
Expand Down

0 comments on commit 1d9196c

Please sign in to comment.