reqstool-python-decorators 0.0.8__py3-none-any.whl → 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ # Copyright © LFV
@@ -1,17 +1,19 @@
1
1
  # Copyright © LFV
2
2
 
3
+ from collections.abc import Callable
3
4
 
4
- def Requirements(*requirements):
5
- def decorator(func):
6
- func.requirements = requirements
5
+
6
+ def Requirements[T: Callable](*requirements: str) -> Callable[[T], T]:
7
+ def decorator(func: T) -> T:
8
+ func.requirements = requirements # type: ignore[attr-defined]
7
9
  return func
8
10
 
9
11
  return decorator
10
12
 
11
13
 
12
- def SVCs(*svc_ids):
13
- def decorator(func):
14
- func.svc_ids = svc_ids
14
+ def SVCs[T: Callable](*svc_ids: str) -> Callable[[T], T]:
15
+ def decorator(func: T) -> T:
16
+ func.svc_ids = svc_ids # type: ignore[attr-defined]
15
17
  return func
16
18
 
17
19
  return decorator
@@ -2,6 +2,7 @@
2
2
 
3
3
  from enum import Enum, unique
4
4
  import os
5
+ from typing import ReadOnly, TypedDict
5
6
  from ruamel.yaml import YAML
6
7
  import ast
7
8
 
@@ -19,6 +20,35 @@ class DECORATOR_TYPES(Enum):
19
20
  return f"from: {self.from_value}, to: {self.to_value}"
20
21
 
21
22
 
23
+ class DecoratorInfo(TypedDict):
24
+ name: ReadOnly[str]
25
+ args: ReadOnly[list[str]]
26
+
27
+
28
+ class ElementResult(TypedDict):
29
+ fullyQualifiedName: ReadOnly[str]
30
+ elementKind: ReadOnly[str]
31
+ name: ReadOnly[str]
32
+ decorators: ReadOnly[list[DecoratorInfo]]
33
+
34
+
35
+ class FormattedEntry(TypedDict):
36
+ elementKind: str
37
+ fullyQualifiedName: str
38
+
39
+
40
+ class RequirementAnnotations(TypedDict):
41
+ implementations: dict[str, list[FormattedEntry]]
42
+ tests: dict[str, list[FormattedEntry]]
43
+
44
+
45
+ class FormattedData(TypedDict):
46
+ requirement_annotations: RequirementAnnotations
47
+
48
+
49
+ type Results = list[ElementResult]
50
+
51
+
22
52
  class DecoratorProcessor:
23
53
  """
24
54
  A class for collecting and processing Requirements and SVCs annotations on functions and classes in a directory.
@@ -32,7 +62,7 @@ class DecoratorProcessor:
32
62
 
33
63
  decorators_to_search = ["Requirements", "SVCs"]
34
64
 
35
- yaml_language_server = "# yaml-language-server: $schema=https://raw.githubusercontent.com/Luftfartsverket/reqstool-client/main/src/reqstool/resources/schemas/v1/annotations.schema.json\n" # noqa: E501
65
+ yaml_language_server = "# yaml-language-server: $schema=https://raw.githubusercontent.com/reqstool/reqstool-client/main/src/reqstool/resources/schemas/v1/annotations.schema.json\n" # noqa: E501
36
66
 
37
67
  def __init__(self, *args, **kwargs):
38
68
  """
@@ -47,14 +77,14 @@ class DecoratorProcessor:
47
77
 
48
78
  """
49
79
  super().__init__(*args, **kwargs)
50
- self.req_svc_results = []
80
+ self.req_svc_results: Results = []
51
81
 
52
- def find_python_files(self, directory):
82
+ def find_python_files(self, directory: str | os.PathLike) -> list[str]:
53
83
  """
54
84
  Find Python files in the given directory.
55
85
 
56
86
  Parameters:
57
- - `directory` (str): The directory to search for Python files.
87
+ - `directory` (str | PathLike): The directory to search for Python files.
58
88
 
59
89
  Returns:
60
90
  - `python_files` (list): List of Python files found in the directory.
@@ -66,17 +96,14 @@ class DecoratorProcessor:
66
96
  python_files.append(os.path.join(root, file))
67
97
  return python_files
68
98
 
69
- def get_functions_and_classes(self, file_path, decorator_names):
99
+ def get_functions_and_classes(self, file_path: str | os.PathLike, decorator_names: list[str]) -> None:
70
100
  """
71
101
  Get information about functions and classes, if annotated with "Requirements" or "SVCs":
72
102
  decorator filepath, elementKind, name and decorators is saved to list that is returned.
73
103
 
74
104
  Parameters:
75
- - `file_path` (str): The path to the Python file.
76
- - `decorator_names` (list): List of decorator names to search for.
77
-
78
- Returns:
79
- - `results` (list): List of dictionaries containing information about functions and classes.
105
+ - `file_path` (str | PathLike): The path to the Python file.
106
+ - `decorator_names` (list[str]): List of decorator names to search for.
80
107
 
81
108
  Each dictionary includes:
82
109
  - `fullyQualifiedName` (str): The fully qualified name of the file.
@@ -85,33 +112,33 @@ class DecoratorProcessor:
85
112
  - `decorators` (list): List of dictionaries with decorator info including name and arguments e.g. "REQ_001".
86
113
  """
87
114
  with open(file_path, "r") as file:
88
- tree = ast.parse(file.read(), filename=file_path)
115
+ tree = ast.parse(file.read(), filename=str(file_path))
89
116
 
90
117
  for node in ast.walk(tree):
91
118
  if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
92
- decorators_info = []
119
+ decorators_info: list[DecoratorInfo] = []
93
120
  for decorator_node in getattr(node, "decorator_list", []):
94
121
  if isinstance(decorator_node, ast.Call) and isinstance(decorator_node.func, ast.Name):
95
122
  decorator_name = decorator_node.func.id
96
- args = [arg.s for arg in decorator_node.args if isinstance(arg, ast.Constant)]
123
+ args = [arg.value for arg in decorator_node.args if isinstance(arg, ast.Constant)]
97
124
  decorators_info.append({"name": decorator_name, "args": args})
98
125
 
99
126
  if any(decorator_info["name"] in decorator_names for decorator_info in decorators_info):
100
127
  self.req_svc_results.append(
101
128
  {
102
129
  "fullyQualifiedName": str(file_path).replace("/", "."),
103
- "elementKind": str(type(node)).split(".")[-1][:-5].upper(),
130
+ "elementKind": node.__class__.__name__.removesuffix("Def").upper(),
104
131
  "name": node.name,
105
132
  "decorators": decorators_info,
106
133
  }
107
134
  )
108
135
 
109
- def write_to_yaml(self, output_file, formatted_data):
136
+ def write_to_yaml(self, output_file: str | os.PathLike, formatted_data) -> None:
110
137
  """
111
138
  Write formatted data to a YAML file.
112
139
 
113
140
  Parameters:
114
- - `output_file` (str): The path to the output YAML file.
141
+ - `output_file` (str | PathLike): The path to the output YAML file.
115
142
  - `formatted_data` (dict): The formatted data to be written to the YAML file.
116
143
 
117
144
  Writes the formatted data to the specified YAML file.
@@ -135,7 +162,7 @@ class DecoratorProcessor:
135
162
  mapping = {item.from_value: item.to_value for item in DECORATOR_TYPES}
136
163
  return mapping.get(input_str, input_str)
137
164
 
138
- def format_results(self, results):
165
+ def format_results(self, results: Results) -> FormattedData:
139
166
  """
140
167
  Format the collected results into a structured data format for YAML.
141
168
 
@@ -143,17 +170,17 @@ class DecoratorProcessor:
143
170
  - `results` (list): List of dictionaries containing information about functions and classes.
144
171
 
145
172
  Returns:
146
- - `formatted_data` (dict): Formatted data in a structured `yaml_language_server` compatible format.
173
+ - `formatted_data` (FormattedData): Formatted data in a structured `yaml_language_server` compatible format.
147
174
 
148
175
  This function formats a list of decorated data into the structure required by the `yaml_language_server`.
149
176
  It includes version information, requirement annotations, and relevant element information.
150
177
  """
151
178
 
152
- formatted_data = {}
153
- implementations = {}
154
- tests = {}
155
- requirement_annotations = {"implementations": implementations, "tests": tests}
156
- formatted_data["requirement_annotations"] = requirement_annotations
179
+ implementations: dict[str, list[FormattedEntry]] = {}
180
+ tests: dict[str, list[FormattedEntry]] = {}
181
+ formatted_data: FormattedData = {
182
+ "requirement_annotations": {"implementations": implementations, "tests": tests}
183
+ }
157
184
 
158
185
  for result in results:
159
186
  for decorator_info in result["decorators"]:
@@ -174,31 +201,35 @@ class DecoratorProcessor:
174
201
 
175
202
  return formatted_data
176
203
 
177
- def create_dir_from_path(self, filepath: str) -> None:
204
+ def create_dir_from_path(self, filepath: str | os.PathLike) -> None:
178
205
  """
179
206
  Creates directory of provided filepath if it does not exists
180
207
 
181
208
  Parameters:
182
- - `filepath` (str): Filepath to check and create directory from.
209
+ - `filepath` (str | PathLike): Filepath to check and create directory from.
183
210
  """
184
211
  directory = os.path.dirname(filepath)
185
212
  if not os.path.exists(directory):
186
213
  os.makedirs(directory)
187
214
 
188
215
  def process_decorated_data(
189
- self, path_to_python_files: str, output_file: str = "build/reqstool/annotations.yml"
216
+ self,
217
+ path_to_python_files: list[str | os.PathLike],
218
+ output_file: str | os.PathLike = "build/reqstool/annotations.yml",
190
219
  ) -> None:
191
220
  """
192
221
  "Main" function, runs all functions resulting in a yaml file containing decorated data.
193
222
 
194
223
  Parameters:
195
224
  - `path_to_python_files` (list): List of directories containing Python files.
196
- - `output_file` (str): Set path for output file, defaults to build/annotations.yml
225
+ - `output_file` (str | PathLike): Set path for output file, defaults to build/annotations.yml
197
226
 
198
227
  This method takes a list of directories containing Python files, collects decorated data from these files,
199
228
  formats the collected data, and writes the formatted results to YAML file for Requirements and SVCs annotations.
200
229
  """
201
230
 
231
+ self.req_svc_results = []
232
+
202
233
  for path in path_to_python_files:
203
234
  python_files = self.find_python_files(directory=path)
204
235
  for file_path in python_files:
@@ -0,0 +1,73 @@
1
+ Metadata-Version: 2.4
2
+ Name: reqstool-python-decorators
3
+ Version: 0.1.0
4
+ Summary: Reqstool Python Decorators
5
+ Project-URL: Homepage, https://reqstool.github.io
6
+ Project-URL: Source, https://github.com/reqstool/reqstool-python-decorators.git
7
+ Project-URL: Documentation, https://github.com/reqstool/reqstool-python-decorators.git
8
+ Author: reqstool
9
+ License-File: LICENSE
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Requires-Python: >=3.13
15
+ Requires-Dist: ruamel-yaml==0.19.1
16
+ Description-Content-Type: text/markdown
17
+
18
+ [![Commit Activity](https://img.shields.io/github/commit-activity/m/reqstool/reqstool-python-decorators?label=commits&style=for-the-badge)](https://github.com/reqstool/reqstool-python-decorators/pulse)
19
+ [![GitHub Issues](https://img.shields.io/github/issues/reqstool/reqstool-python-decorators?style=for-the-badge&logo=github)](https://github.com/reqstool/reqstool-python-decorators/issues)
20
+ [![License](https://img.shields.io/github/license/reqstool/reqstool-python-decorators?style=for-the-badge&logo=opensourceinitiative)](https://opensource.org/license/mit/)
21
+ [![Build](https://img.shields.io/github/actions/workflow/status/reqstool/reqstool-python-decorators/build.yml?style=for-the-badge&logo=github)](https://github.com/reqstool/reqstool-python-decorators/actions/workflows/build.yml)
22
+ [![Documentation](https://img.shields.io/badge/Documentation-blue?style=for-the-badge&link=docs)](https://reqstool.github.io)
23
+
24
+ # Reqstool Python Decorators
25
+
26
+ Python decorators for [reqstool](https://github.com/reqstool/reqstool-client) requirements traceability. Provides `@Requirements` and `@SVCs` decorators for linking Python code to requirements and software verification cases.
27
+
28
+ ## Prerequisites
29
+
30
+ - Python >= 3.13
31
+
32
+ ## Installation
33
+
34
+ ```bash
35
+ pip install reqstool-python-decorators
36
+ ```
37
+
38
+ ## Usage
39
+
40
+ ```python
41
+ from reqstool_python_decorators.decorators.decorators import Requirements, SVCs
42
+
43
+ @Requirements("REQ_111", "REQ_222")
44
+ def somefunction():
45
+ pass
46
+
47
+ @SVCs("SVC_111", "SVC_222")
48
+ def test_somefunction():
49
+ pass
50
+ ```
51
+
52
+ ### Processor
53
+
54
+ ```python
55
+ from reqstool_python_decorators.processors.decorator_processor import DecoratorProcessor
56
+
57
+ # Collect decorators and generate annotations.yml
58
+ process_decorated_data(path_to_python_files, output_file)
59
+ ```
60
+
61
+ Used together with the [Hatch Plugin](https://github.com/reqstool/reqstool-python-hatch-plugin) or [Poetry Plugin](https://github.com/reqstool/reqstool-python-poetry-plugin).
62
+
63
+ ## Documentation
64
+
65
+ Full documentation can be found [here](https://reqstool.github.io).
66
+
67
+ ## Contributing
68
+
69
+ See the organization-wide [CONTRIBUTING.md](https://github.com/reqstool/.github/blob/main/CONTRIBUTING.md).
70
+
71
+ ## License
72
+
73
+ MIT License.
@@ -0,0 +1,9 @@
1
+ reqstool_python_decorators/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ reqstool_python_decorators/decorators/__init__.py,sha256=9xL1ExocYj8K05rdGyEKT9CLX7bgWbOG3hsvDhWWnFc,19
3
+ reqstool_python_decorators/decorators/decorators.py,sha256=haaZ-Y1bjr6BXiWarkVxH0uZr4yPACTNfqy3q935M_M,472
4
+ reqstool_python_decorators/processors/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ reqstool_python_decorators/processors/decorator_processor.py,sha256=o5Z74nuLi3c9O42A-UFej5B7qWAWxxBWqqjrRT_EPSs,9414
6
+ reqstool_python_decorators-0.1.0.dist-info/METADATA,sha256=nd6XuKMovId-VTH4quycWNdySz5tdfcen6dEXoqh4xk,2805
7
+ reqstool_python_decorators-0.1.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
8
+ reqstool_python_decorators-0.1.0.dist-info/licenses/LICENSE,sha256=FVfPRmqjYUYTodYsh2C7Y70MWH64m0aWgTeIbqPj-P0,1060
9
+ reqstool_python_decorators-0.1.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.27.0
2
+ Generator: hatchling 1.29.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,99 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: reqstool-python-decorators
3
- Version: 0.0.8
4
- Summary: Reqstool Python Decorators
5
- Project-URL: Source, https://github.com/Luftfartsverket/reqstool-python-decorators.git
6
- Author-email: LFV SYSDEV <sysdev@lfv.com>
7
- License-File: LICENSE
8
- Classifier: Development Status :: 4 - Beta
9
- Classifier: License :: OSI Approved :: MIT License
10
- Classifier: Operating System :: OS Independent
11
- Classifier: Programming Language :: Python :: 3
12
- Requires-Python: >=3.10
13
- Requires-Dist: ruamel-yaml>=0.18.5
14
- Description-Content-Type: text/markdown
15
-
16
-
17
- [![Commit Activity](https://img.shields.io/github/commit-activity/m/Luftfartsverket/reqstool-python-decorators?label=commits&style=for-the-badge)](https://github.com/Luftfartsverket/reqstool-python-decorators/pulse)
18
- [![GitHub Issues](https://img.shields.io/github/issues/Luftfartsverket/reqstool-python-decorators?style=for-the-badge&logo=github)](https://github.com/Luftfartsverket/reqstool-python-decorators/issues)
19
- [![License](https://img.shields.io/github/license/Luftfartsverket/reqstool-python-decorators?style=for-the-badge&logo=opensourceinitiative)](https://opensource.org/license/mit/)
20
- [![Build](https://img.shields.io/github/actions/workflow/status/Luftfartsverket/reqstool-python-decorators/build.yml?style=for-the-badge&logo=github)](https://github.com/Luftfartsverket/reqstool-python-decorators/actions/workflows/build.yml)
21
- [![Static Badge](https://img.shields.io/badge/Documentation-blue?style=for-the-badge&link=docs)](https://luftfartsverket.github.io/reqstool-python-decorators/reqstool-python-decorators/0.0.1/index.html)
22
-
23
- # Reqstool Python Decorators
24
-
25
- ## Description
26
-
27
- This provides decorators and collecting of decorated code, formatting it and writing to yaml file.
28
-
29
- ## Installation
30
-
31
- The package name is `reqstool-python-decorators`.
32
-
33
- * Using pip install:
34
-
35
- ```
36
- $pip install reqstool-python-decorators
37
- ```
38
-
39
- ## Usage
40
-
41
- ### pyproject.toml
42
-
43
- * Hatch
44
-
45
- ```
46
- dependencies = [
47
- "reqstool-python-decorators == <version>"
48
- ]
49
- ```
50
-
51
- * Poetry
52
-
53
- ```
54
- [tool.poetry.dependencies]
55
- reqstool-python-decorators = "<version>"
56
- ```
57
-
58
- ### Decorators
59
-
60
- Import decorators:
61
-
62
- ```
63
- from reqstool-decorators.decorators.decorators import Requirements, SVCs
64
- ```
65
-
66
- Example usage of the decorators:
67
-
68
- ```
69
- @Requirements("REQ_111", "REQ_222")
70
- def somefunction():
71
- ```
72
-
73
- ```
74
- @SVCs("SVC_111", "SVC_222")
75
- def test_somefunction():
76
- ```
77
-
78
- ### Processor
79
-
80
- Import processor:
81
-
82
- ```
83
- from reqstool.processors.decorator_processor import DecoratorProcessor
84
- ```
85
-
86
- Main function to collect decorators data and generate yaml file:
87
-
88
- ```
89
- process_decorated_data(path_to_python_files, output_file)
90
- ```
91
-
92
- `path_to_python_files` is the directories to search through to find decorated code.
93
-
94
- (Optional) `output_file` is output file(path) the yaml file is stored to. Default is `/build/reqstool/annotations.yml`.
95
-
96
-
97
- ## License
98
-
99
- This project is licensed under the MIT License - see the LICENSE.md file for details.
@@ -1,8 +0,0 @@
1
- reqstool_python_decorators/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- reqstool_python_decorators/decorators/decorators.py,sha256=7TnBcVour2VSH76rM4FtwmCDi9JY9gxqOvAL6o1kI_g,282
3
- reqstool_python_decorators/processors/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- reqstool_python_decorators/processors/decorator_processor.py,sha256=zZHl3uDJZXDIlPvIz3IxyO02IImJfNEGCb3IbjLuhfc,8536
5
- reqstool_python_decorators-0.0.8.dist-info/METADATA,sha256=8BVcRKlnD_XLex-VgOMpr50wcK7_BI3ScPhRFi0ehD8,2906
6
- reqstool_python_decorators-0.0.8.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
- reqstool_python_decorators-0.0.8.dist-info/licenses/LICENSE,sha256=FVfPRmqjYUYTodYsh2C7Y70MWH64m0aWgTeIbqPj-P0,1060
8
- reqstool_python_decorators-0.0.8.dist-info/RECORD,,