-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
144 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from pydantic import BaseModel, Field | ||
from pathlib import Path | ||
from typing import List, Dict, Optional | ||
import yaml | ||
import capellambse | ||
|
||
|
||
class TemplateScope(BaseModel): | ||
type: str = Field(..., title="Model Element Type") | ||
below: Optional[str] = Field(None, title="Model element to search below, scope limiter") | ||
|
||
class Template(BaseModel): | ||
idx: str = Field(..., title="Template Identifier") | ||
name: str = Field(..., title="Template Name") | ||
template: Path = Field(..., title="Template File Path") | ||
description: str = Field(..., title="Template Description") | ||
scope: TemplateScope = Field(..., title="Template Scope") | ||
|
||
@property | ||
def instances(self): | ||
# eval the scope and return object list | ||
pass | ||
|
||
|
||
class TemplateCategory(BaseModel): | ||
idx: str = Field(..., title="Category Identifier") | ||
templates: List[Template] = Field([], title="Templates in this category") | ||
|
||
def __getitem__(self, template_id: str): | ||
return self.by_id(template_id) | ||
|
||
def __add__(self, other): | ||
if not isinstance(other, TemplateCategory): | ||
return NotImplemented | ||
return TemplateCategory(templates=self.templates + other.templates) | ||
|
||
|
||
|
||
class TemplateCategories(BaseModel): | ||
categories: List[TemplateCategory] = Field([], title="Template Categories") | ||
|
||
def __getitem__(self, category_id: str): | ||
return [cat for cat in self.categories if cat.idx == category_id].first() | ||
|
||
def __add__(self, other): | ||
if not isinstance(other, TemplateCategories): | ||
return NotImplemented | ||
for category in other.categories: | ||
category_id = category.idx | ||
if category_id in [cat.idx for cat in self.categories]: | ||
self.categories[category_id].templates += category.templates | ||
else: | ||
self.categories.append(TemplateCategory(idx=category_id, templates=category.templates)) | ||
return self | ||
|
||
def __len__(self): | ||
return len(self.categories) | ||
|
||
@property | ||
def flat(self): | ||
return { | ||
template.idx: template | ||
for category in self.categories | ||
for template in category.templates | ||
} | ||
|
||
class TemplateLoader(): | ||
def __init__(self, model: capellambse.MelodyModel) -> None: | ||
self.model = model | ||
self.templates = TemplateCategories() | ||
|
||
def index_path(self, path: Path) -> TemplateCategories: | ||
self.templates = TemplateCategories() # reset templates | ||
for template_file in path.glob("**/*.yaml"): | ||
with template_file.open() as f: | ||
templates_data = yaml.safe_load(template_file.read_text(encoding="utf8")) | ||
if "categories" in templates_data: | ||
self.templates += TemplateCategories(**templates_data) | ||
return self.templates | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,37 @@ | ||
# Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import capella_model_explorer | ||
import capellambse | ||
from pathlib import Path | ||
from capella_model_explorer.backend.templates import TemplateLoader, Template, TemplateCategory | ||
|
||
|
||
def test_add_some_tests_here(): ... | ||
def test_template_loading(): | ||
template_raw = { | ||
"idx": "test", | ||
"name": "Test Template", | ||
"template": "test_template.jinja", | ||
"description": "This is a test template", | ||
"scope": {"type": "System", "below": "System"}, | ||
} | ||
template = Template(**template_raw) | ||
|
||
def test_category_loading(): | ||
template_raw = { | ||
"idx": "test", | ||
"name": "Test Template", | ||
"template": "test_template.jinja", | ||
"description": "This is a test template", | ||
"scope": {"type": "System", "below": "System"}, | ||
} | ||
category_raw = { | ||
"idx": "test", | ||
"templates": [template_raw], | ||
} | ||
category = TemplateCategory(**category_raw) | ||
|
||
def test_index_templates(): | ||
model = capellambse.loadcli("coffee-machine") | ||
templates = TemplateLoader(model).index_path(Path(".")) | ||
assert len(templates.flat) == 3 | ||
assert len(templates) == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
categories: | ||
- idx: oa | ||
templates: | ||
- idx: operational-activity | ||
name: Operational Activity | ||
description: Specifies an operational activity. | ||
template: operational-analysis/operational-activity.html.j2 | ||
scope: | ||
type: OperationalActivity | ||
below: oa | ||
- idx: operational-capability | ||
name: Operational Capability | ||
description: Specifies an operational capability. | ||
template: operational-analysis/operational-capability.html.j2 | ||
scope: | ||
type: OperationalCapability | ||
below: oa | ||
- idx: operational-entity | ||
name: Operational Entity | ||
description: Specifies an operational entity. | ||
template: operational-analysis/operational-entity.html.j2 | ||
scope: | ||
type: Entity | ||
below: oa |