|
| 1 | +import argparse |
1 | 2 | import sys |
| 3 | +from pathlib import Path |
2 | 4 | from typing import Dict, Optional |
3 | 5 |
|
4 | 6 | from strictdoc.backend.sdoc.errors.document_tree_error import DocumentTreeError |
|
9 | 11 | SourceFileTraceabilityInfo, |
10 | 12 | ) |
11 | 13 | from strictdoc.backend.sdoc_source_code.source_writer import SourceWriter |
| 14 | +from strictdoc.cli.base_command import BaseCommand, CLIValidationError |
| 15 | +from strictdoc.commands.manage_autouid_config import ManageAutoUIDCommandConfig |
12 | 16 | from strictdoc.core.analyzers.document_stats import ( |
13 | 17 | DocumentStats, |
14 | 18 | DocumentTreeStats, |
15 | 19 | ) |
16 | 20 | from strictdoc.core.analyzers.document_uid_analyzer import DocumentUIDAnalyzer |
17 | | -from strictdoc.core.project_config import ProjectConfig |
| 21 | +from strictdoc.core.project_config import ProjectConfig, ProjectConfigLoader |
18 | 22 | from strictdoc.core.traceability_index import TraceabilityIndex |
19 | 23 | from strictdoc.core.traceability_index_builder import TraceabilityIndexBuilder |
20 | 24 | from strictdoc.helpers.parallelizer import Parallelizer |
@@ -42,15 +46,71 @@ def generate_code_hash( |
42 | 46 | return bytes(get_sha256(hash_input), encoding="utf8") |
43 | 47 |
|
44 | 48 |
|
45 | | -class ManageAutoUIDCommand: |
46 | | - @staticmethod |
47 | | - def execute( |
48 | | - *, project_config: ProjectConfig, parallelizer: Parallelizer |
49 | | - ) -> None: |
| 49 | +class ManageAutoUIDCommand(BaseCommand): |
| 50 | + HELP = "Generates missing requirements UIDs automatically." |
| 51 | + DETAILED_HELP = """\ |
| 52 | +This command generates missing requirement UID automatically. |
| 53 | +The UIDs are generated based on the nearest section PREFIX (if provided) or |
| 54 | +the document's PREFIX (if provided or "REQ-" by default). |
| 55 | +""" |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def add_arguments(cls, parser: argparse.ArgumentParser) -> None: |
| 59 | + command_parser_auto_uid = parser |
| 60 | + |
| 61 | + command_parser_auto_uid.add_argument( |
| 62 | + "input_path", |
| 63 | + type=str, |
| 64 | + help="Path to the project tree.", |
| 65 | + ) |
| 66 | + command_parser_auto_uid.add_argument( |
| 67 | + "--include-sections", |
| 68 | + action="store_true", |
| 69 | + help=( |
| 70 | + "By default, the command only generates the UID for " |
| 71 | + "requirements. This option enables the generation of UID for " |
| 72 | + "sections." |
| 73 | + ), |
| 74 | + ) |
| 75 | + parser.add_argument( |
| 76 | + "--config", |
| 77 | + type=str, |
| 78 | + help="Path to the StrictDoc TOML config file.", |
| 79 | + ) |
| 80 | + |
| 81 | + def __init__(self, args: argparse.Namespace) -> None: |
| 82 | + self.args = args |
| 83 | + self.config: ManageAutoUIDCommandConfig = ManageAutoUIDCommandConfig( |
| 84 | + **vars(args) |
| 85 | + ) |
| 86 | + |
| 87 | + def run(self, parallelizer: Parallelizer) -> None: # noqa: ARG002 |
50 | 88 | """ |
51 | 89 | @relation(SDOC-SRS-85, scope=function) |
52 | 90 | """ |
53 | 91 |
|
| 92 | + manage_config: ManageAutoUIDCommandConfig = self.config |
| 93 | + try: |
| 94 | + manage_config.validate() |
| 95 | + except CLIValidationError as exception_: |
| 96 | + raise exception_ |
| 97 | + |
| 98 | + project_config = ProjectConfigLoader.load_from_path_or_get_default( |
| 99 | + path_to_config=manage_config.get_path_to_config(), |
| 100 | + ) |
| 101 | + |
| 102 | + # FIXME: Encapsulate all this in project_config.integrate_manage_autouid_config(), |
| 103 | + # following the example of integrate_export_config(). |
| 104 | + project_config.input_paths = [manage_config.input_path] |
| 105 | + project_config.source_root_path = str( |
| 106 | + Path(manage_config.input_path).resolve() |
| 107 | + ) |
| 108 | + project_config.auto_uid_mode = True |
| 109 | + project_config.autouuid_include_sections = ( |
| 110 | + manage_config.include_sections |
| 111 | + ) |
| 112 | + project_config.validate_and_finalize() |
| 113 | + |
54 | 114 | # FIXME: Traceability Index is coupled with HTML output. |
55 | 115 | project_config.export_output_html_root = "NOT_RELEVANT" |
56 | 116 |
|
|
0 commit comments