Skip to content

Commit

Permalink
merge: Modeling rules and validation (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wuestengecko committed Jun 6, 2024
2 parents 813b341 + add991f commit 5c1e479
Show file tree
Hide file tree
Showing 10 changed files with 1,488 additions and 16 deletions.
16 changes: 13 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,19 @@ repos:
pass_filenames: false
args: [capellambse, tests]
additional_dependencies:
- mypy==1.9.0
- capellambse[cli,decl,docs,httpfiles,png,termgraphics,test]
- mypy==1.10.0
- click==8.1.7
- sphinx
- diskcache==5.0
- jinja2==3.1.3
- markupsafe==2.0
- platformdirs==4.2.0
- pytest-cov
- pytest==8.0.0
- requests-mock==1.11.0
- spacy==3.7.5
- sphinx-argparse-cli
- sphinx==7.2.3
- tomli; python_version<'3.11'
- types-Pygments==2.15.0.1
- types-colorama==0.4.15.11
- types-docutils==0.20.0.1
Expand All @@ -118,6 +127,7 @@ repos:
- types-requests==2.25.11
- types-setuptools==68.0.0.1
- types-six==1.16.21.8
- typing-extensions==4.6.0
- repo: https://github.com/pylint-dev/pylint
rev: v3.1.0
hooks:
Expand Down
37 changes: 37 additions & 0 deletions capellambse/extensions/validation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG
# SPDX-License-Identifier: Apache-2.0
"""The module provides management and evaluation of validation rules.
Validation rules are conditions ensuring that specific modeling
guidelines are followed. These rules apply to particular types of model
elements or to diagrams, and values of metrics. By evaluating each rule,
the module generates validation results, indicating whether the
corresponding guideline has been satisfied or not. This way, the module
helps maintain the quality and consistency of the model.
"""

from ._validate import *

from . import rules # isort: skip


def init() -> None:
# pylint: disable=redefined-outer-name # false-positive
import capellambse
from capellambse.model import common as c

c.set_accessor(
capellambse.MelodyModel,
"validation",
c.AlternateAccessor(ModelValidation),
)
capellambse.MelodyModel.validate = property( # type: ignore[attr-defined]
lambda self: self.validation.validate
)

c.set_accessor(
c.GenericElement, "validation", c.AlternateAccessor(ElementValidation)
)
c.GenericElement.validate = property( # type: ignore[attr-defined]
lambda self: self.validation.validate
)
47 changes: 47 additions & 0 deletions capellambse/extensions/validation/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import logging
import typing as t

import click
import jinja2

import capellambse


@click.command()
@click.option("-m", "--model", required=True, type=capellambse.ModelCLI())
@click.option(
"-o",
"--output",
type=click.File("w", atomic=True),
required=True,
help="Output file to render the template into",
)
@click.option("-t", "--template", help="An optional custom template to render")
def _main(
model: capellambse.MelodyModel,
template: str | None,
output: t.IO[str],
) -> None:
logging.basicConfig()

loader: jinja2.BaseLoader
if template is None:
loader = jinja2.PackageLoader("capellambse", "extensions/validation")
template = "report-template.html.jinja"
else:
loader = jinja2.FileSystemLoader(".")
env = jinja2.Environment(loader=loader)

with output:
env.get_template(template).stream(
model=model,
results=model.validation.validate(),
).dump(output)


if __name__ == "__main__":
_main()
Loading

0 comments on commit 5c1e479

Please sign in to comment.