|
| 1 | +# Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH |
| 2 | +# |
| 3 | +# See the AUTHORS file(s) distributed with this work for additional |
| 4 | +# information regarding authorship. |
| 5 | +# |
| 6 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | +# |
| 10 | +# SPDX-License-Identifier: MPL-2.0 |
| 11 | + |
| 12 | +import subprocess |
| 13 | + |
| 14 | +from os.path import exists, join |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +from scripts.download_samm_cli import download_samm_cli |
| 18 | + |
| 19 | + |
| 20 | +class SammCli: |
| 21 | + """Class to eecute SAMM CLI functions. |
| 22 | +
|
| 23 | + If there is no downloaded SAMM CLI, code will identify operation system and download a corresponding client. |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__(self): |
| 27 | + self._samm = self._get_client_path() |
| 28 | + self._validate_client() |
| 29 | + |
| 30 | + @staticmethod |
| 31 | + def _get_client_path(): |
| 32 | + """Get path to the SAMM Cli executable file..""" |
| 33 | + base_path = Path(__file__).resolve() |
| 34 | + cli_path = join(base_path.parents[1], "samm-cli", "samm.exe") |
| 35 | + |
| 36 | + return cli_path |
| 37 | + |
| 38 | + def _validate_client(self): |
| 39 | + """Validate SAMM Cli. |
| 40 | +
|
| 41 | + If there is no SAMM Cli executable file, run a script for downloading. |
| 42 | + """ |
| 43 | + if not exists(self._samm): |
| 44 | + # download SAMM CLI |
| 45 | + download_samm_cli() |
| 46 | + |
| 47 | + def _call_function(self, function_name, path_to_model, *args, **kwargs): |
| 48 | + """Run a SAMM Cli function as a subprocess.""" |
| 49 | + call_args = [self._samm, "aspect", path_to_model] + function_name.split() |
| 50 | + |
| 51 | + if args: |
| 52 | + call_args.extend([f"-{param}" for param in args]) |
| 53 | + |
| 54 | + if kwargs: |
| 55 | + for key, value in kwargs.items(): |
| 56 | + if len(key) == 1: |
| 57 | + arg = f"-{key}={value}" |
| 58 | + else: |
| 59 | + key = key.replace("_", "-") |
| 60 | + arg = f"--{key}={value}" |
| 61 | + |
| 62 | + call_args.append(arg) |
| 63 | + |
| 64 | + subprocess.run(call_args, shell=True, check=True) |
| 65 | + |
| 66 | + def validate(self, path_to_model, *args, **kwargs): |
| 67 | + """Validate Aspect Model. |
| 68 | +
|
| 69 | + param path_to_model: local path to the aspect model file (*.ttl) |
| 70 | + possible arguments: |
| 71 | + custom-resolver: use an external resolver for the resolution of the model elements |
| 72 | + """ |
| 73 | + self._call_function("validate", path_to_model, *args, **kwargs) |
| 74 | + |
| 75 | + def to_openapi(self, path_to_model, *args, **kwargs): |
| 76 | + """Generate OpenAPI specification for an Aspect Model. |
| 77 | +
|
| 78 | + param path_to_model: local path to the aspect model file (*.ttl) |
| 79 | + possible arguments: |
| 80 | + - output, o: output file path (default: stdout) |
| 81 | + - api-base-url, b: the base url for the Aspect API used in the OpenAPI specification, b="http://mysite.de" |
| 82 | + - json, j: generate a JSON specification for an Aspect Model (default format is YAML) |
| 83 | + - comment, c: only in combination with --json; generates $comment OpenAPI 3.1 keyword for all |
| 84 | + samm:see attributes |
| 85 | + - parameter-file, p: the path to a file including the parameter for the Aspect API endpoints. |
| 86 | + For detailed description, please have a look at a SAMM CLI documentation (https://eclipse-esmf.github.io/esmf-developer-guide/tooling-guide/samm-cli.html#using-the-cli-to-create-a-json-openapi-specification) # noqa: E501 |
| 87 | + - semantic-version, sv: use the full semantic version from the Aspect Model as the version for the Aspect API |
| 88 | + - resource-path, r: the resource path for the Aspect API endpoints |
| 89 | + For detailed description, please have a look at a SAMM CLI documentation (https://eclipse-esmf.github.io/esmf-developer-guide/tooling-guide/samm-cli.html#using-the-cli-to-create-a-json-openapi-specification) # noqa: E501 |
| 90 | + - include-query-api, q: include the path for the Query Aspect API Endpoint in the OpenAPI specification |
| 91 | + - paging-none, pn: exclude paging information for the Aspect API Endpoint in the OpenAPI specification |
| 92 | + - paging-cursor-based, pc: in case there is more than one paging possibility, it must be cursor based paging |
| 93 | + - paging-offset-based, po: in case there is more than one paging possibility, it must be offset based paging |
| 94 | + - paging-time-based, pt: in case there is more than one paging possibility, it must be time based paging |
| 95 | + - language, l: the language from the model for which an OpenAPI specification should be generated (default: en) |
| 96 | + custom-resolver: use an external resolver for the resolution of the model elements |
| 97 | + """ |
| 98 | + self._call_function("to openapi", path_to_model, *args, **kwargs) |
| 99 | + |
| 100 | + def to_schema(self, path_to_model, *args, **kwargs): |
| 101 | + """Generate JSON schema for an Aspect Model. |
| 102 | +
|
| 103 | + param path_to_model: local path to the aspect model file (*.ttl) |
| 104 | + possible arguments: |
| 105 | + - output, -o: output file path (default: stdout) |
| 106 | + - language, -l: the language from the model for which a JSON schema should be generated (default: en) |
| 107 | + - custom-resolver: use an external resolver for the resolution of the model elements |
| 108 | + """ |
| 109 | + self._call_function("to schema", path_to_model, *args, **kwargs) |
| 110 | + |
| 111 | + def to_json(self, path_to_model, *args, **kwargs): |
| 112 | + """Generate example JSON payload data for an Aspect Model. |
| 113 | +
|
| 114 | + param path_to_model: local path to the aspect model file (*.ttl) |
| 115 | + possible arguments: |
| 116 | + - output, -o: output file path (default: stdout) |
| 117 | + - custom-resolver: use an external resolver for the resolution of the model elements |
| 118 | + """ |
| 119 | + self._call_function("to json", path_to_model, *args, **kwargs) |
| 120 | + |
| 121 | + def to_html(self, path_to_model, *args, **kwargs): |
| 122 | + """Generate HTML documentation for an Aspect Model. |
| 123 | +
|
| 124 | + param path_to_model: local path to the aspect model file (*.ttl) |
| 125 | + possible arguments: |
| 126 | + - output, -o: the output will be saved to the given file |
| 127 | + - css, -c: CSS file with custom styles to be included in the generated HTML documentation |
| 128 | + - language, -l: the language from the model for which the HTML should be generated (default: en) |
| 129 | + - custom-resolver: use an external resolver for the resolution of the model elements |
| 130 | + """ |
| 131 | + self._call_function("to html", path_to_model, *args, **kwargs) |
| 132 | + |
| 133 | + def to_png(self, path_to_model, *args, **kwargs): |
| 134 | + """Generate PNG diagram for Aspect Model. |
| 135 | +
|
| 136 | + param path_to_model: local path to the aspect model file (*.ttl) |
| 137 | + possible arguments: |
| 138 | + - output, -o: output file path (default: stdout); |
| 139 | + as PNG is a binary format, it is strongly recommended to output the result to a file |
| 140 | + by using the -o option or the console redirection operator '>' |
| 141 | + - language, -l: the language from the model for which the diagram should be generated (default: en) |
| 142 | + - custom-resolver: use an external resolver for the resolution of the model elements |
| 143 | + """ |
| 144 | + self._call_function("to png", path_to_model, *args, **kwargs) |
| 145 | + |
| 146 | + def to_svg(self, path_to_model, *args, **kwargs): |
| 147 | + """Generate SVG diagram for Aspect Model. |
| 148 | +
|
| 149 | + param path_to_model: local path to the aspect model file (*.ttl) |
| 150 | + possible arguments: |
| 151 | + - output, -o: the output will be saved to the given file |
| 152 | + - language, -l: the language from the model for which the diagram should be generated (default: en) |
| 153 | + - custom-resolver: use an external resolver for the resolution of the model elements |
| 154 | + """ |
| 155 | + self._call_function("to svg", path_to_model, *args, **kwargs) |
0 commit comments