Skip to content

Commit 1c8d951

Browse files
authored
fix: Update schema url add output parameter and rename class (#12)
* fix: Update schema url added output parameter and rename class * fix: Add inline commet for flake8 to ignore E501 line too long * fix: Clean up code and update documentation * fix: Specify E501 in inline comment for flake8 to ignore * WIP * WIP
1 parent 206bcd8 commit 1c8d951

3 files changed

Lines changed: 33 additions & 25 deletions

File tree

docs/modules/ROOT/pages/usage.adoc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,16 @@ def test_somefunction():
4242
Import processor:
4343

4444
```
45-
from reqstool_python_decorators.processors.decorator_processor import ProcessDecorator
45+
from reqstool_python_decorators.processors.decorator_processor import DecoratorProcessor
4646
```
4747

4848
Main function to collect decorators data and generate yaml file:
4949

5050
```
51-
process_decorated_data(path_to_python_files)
51+
process_decorated_data(path_to_python_files, output_file)
5252
```
5353

54-
Here `path_to_python_files` is the directories to search through to find decorated code.
54+
`path_to_python_files` is the directories to search through to find decorated code.
5555

56-
*Note:*
56+
`output_file` is output file(path) the yaml file is stored to
5757

58-
Yaml file is saved into the `/dist` folder, make sure the folder exists before running the `process_decorated_data` function.

src/reqstool_python_decorators/processors/decorator_processor.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def get_from_to(self):
1818
return f"from: {self.from_value}, to: {self.to_value}"
1919

2020

21-
class ProcessDecorator:
21+
class DecoratorProcessor:
2222
"""
2323
A class for collecting and processing Requirements and SVCs annotations on functions and classes in a directory.
2424
@@ -30,11 +30,8 @@ class ProcessDecorator:
3030
"""
3131

3232
decorators_to_search = ["Requirements", "SVCs"]
33-
reqsvc_yaml_path = "dist/all_annotations.yml"
3433

35-
yaml_language_server = (
36-
"# yaml-language-server: $schema=https://schemas.se/requirements-tool/v1/annotations.schema.json\n"
37-
)
34+
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
3835

3936
def __init__(self, *args, **kwargs):
4037
"""
@@ -176,12 +173,24 @@ def format_results(self, results):
176173

177174
return formatted_data
178175

179-
def process_decorated_data(self, path_to_python_files):
176+
def create_dir_from_path(self, filepath: str):
177+
"""
178+
Creates directory of provided filepath if it does not exists
179+
180+
Parameters:
181+
- `filepath` (str): Filepath to check and create directory from.
182+
"""
183+
directory = os.path.dirname(filepath)
184+
if not os.path.exists(directory):
185+
os.makedirs(directory)
186+
187+
def process_decorated_data(self, path_to_python_files, output_file="build/annotations.yml"):
180188
"""
181189
"Main" function, runs all functions resulting in a yaml file containing decorated data.
182190
183191
Parameters:
184192
- `path_to_python_files` (list): List of directories containing Python files.
193+
- `output_file` (str): Set path for output file, defaults to build/annotations.yml
185194
186195
This method takes a list of directories containing Python files, collects decorated data from these files,
187196
formats the collected data, and writes the formatted results to YAML file for Requirements and SVCs annotations.
@@ -191,6 +200,8 @@ def process_decorated_data(self, path_to_python_files):
191200
python_files = self.find_python_files(directory=path)
192201
for file_path in python_files:
193202
self.get_functions_and_classes(file_path=file_path, decorator_names=self.decorators_to_search)
203+
194204
formatted_reqsvc_data = self.format_results(results=self.req_svc_results)
195-
reqsvc_yaml_path = "dist/annotations.yml"
196-
self.write_to_yaml(output_file=reqsvc_yaml_path, formatted_data=formatted_reqsvc_data)
205+
206+
self.create_dir_from_path(output_file)
207+
self.write_to_yaml(output_file=output_file, formatted_data=formatted_reqsvc_data)

tests/unit/reqstool_decorators/processors/test_processors.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import pytest
2-
from src.reqstool_python_decorators.processors.decorator_processor import ProcessDecorator
2+
from src.reqstool_python_decorators.processors.decorator_processor import DecoratorProcessor
33
from ruamel.yaml import YAML
44

55

66
@pytest.fixture
77
def process_decorator_instance():
8-
return ProcessDecorator()
8+
return DecoratorProcessor()
99

1010

11-
def test_find_python_files(process_decorator_instance: ProcessDecorator, tmpdir):
11+
def test_find_python_files(process_decorator_instance: DecoratorProcessor, tmpdir):
1212
tmpdir.join("pythonfile.py").write("content")
1313
result = process_decorator_instance.find_python_files(tmpdir)
1414
assert result == [str(tmpdir.join("pythonfile.py"))]
1515

1616

17-
def test_get_functions_and_classes(process_decorator_instance: ProcessDecorator, tmpdir):
17+
def test_get_functions_and_classes(process_decorator_instance: DecoratorProcessor, tmpdir):
1818
file_path = str(tmpdir.join("test_file.py"))
1919
tmpdir.join("test_file.py").write('@SVCs("SVC_001")\nclass Test:\n pass')
2020
process_decorator_instance.get_functions_and_classes(file_path, ["SVCs"])
@@ -24,20 +24,18 @@ def test_get_functions_and_classes(process_decorator_instance: ProcessDecorator,
2424
assert process_decorator_instance.req_svc_results[0]["elementKind"] == "CLASS"
2525

2626

27-
def test_map_type_known_type(process_decorator_instance: ProcessDecorator):
27+
def test_map_type_known_type(process_decorator_instance: DecoratorProcessor):
2828
result = process_decorator_instance.map_type("FUNCTION")
2929
assert result == "METHOD"
3030

3131

32-
def test_map_type_unknown_type(process_decorator_instance: ProcessDecorator):
32+
def test_map_type_unknown_type(process_decorator_instance: DecoratorProcessor):
3333
result = process_decorator_instance.map_type("CLASS")
3434
assert result == "CLASS"
3535

3636

37-
def test_write_to_yaml(process_decorator_instance: ProcessDecorator, tmp_path):
38-
yaml_language_server = (
39-
"# yaml-language-server: $schema=https://schemas.se/requirements-tool/v1/annotations.schema.json"
40-
)
37+
def test_write_to_yaml(process_decorator_instance: DecoratorProcessor, tmp_path):
38+
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
4139

4240
test_output_file = tmp_path / "test_output.yml"
4341
sample_formatted_data = """
@@ -59,7 +57,7 @@ def test_write_to_yaml(process_decorator_instance: ProcessDecorator, tmp_path):
5957
assert sample_formatted_data == loaded_data
6058

6159

62-
@pytest.mark.skip(reason="Test manually and check structure of the annotations.yml file generated in dist folder")
63-
def test_process_decorated_data(process_decorator_instance: ProcessDecorator):
60+
@pytest.mark.skip(reason="Test manually and check structure of the annotations.yml file generated in build folder")
61+
def test_process_decorated_data(process_decorator_instance: DecoratorProcessor):
6462
paths = ["tests"]
6563
process_decorator_instance.process_decorated_data(path_to_python_files=paths)

0 commit comments

Comments
 (0)