Skip to content

Commit 488d502

Browse files
committed
refactor(cli): migrate "import excel" and "import reqif" to a command pattern
1 parent 6b99274 commit 488d502

11 files changed

Lines changed: 180 additions & 221 deletions

File tree

strictdoc/backend/excel/excel_import.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
ExcelToSDocConverter,
77
)
88
from strictdoc.backend.sdoc.models.document import SDocDocument
9-
from strictdoc.cli.cli_arg_parser import ImportExcelCommandConfig
9+
from strictdoc.commands.import_excel_config import ImportExcelCommandConfig
1010

1111

1212
class ExcelImport:

strictdoc/backend/reqif/reqif_import.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
)
1414
from strictdoc.backend.reqif.sdoc_reqif_fields import ReqIFProfile
1515
from strictdoc.backend.sdoc.models.document import SDocDocument
16-
from strictdoc.cli.cli_arg_parser import ImportReqIFCommandConfig
16+
from strictdoc.commands.import_reqif_config import ImportReqIFCommandConfig
1717
from strictdoc.core.project_config import ProjectConfig
1818

1919

strictdoc/cli/cli_arg_parser.py

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,11 @@
44
from strictdoc.cli.command_parser_builder import (
55
COMMAND_REGISTRY,
66
CommandParserBuilder,
7-
SDocArgumentParser,
87
)
98
from strictdoc.helpers.cast import assert_cast
109
from strictdoc.helpers.parallelizer import Parallelizer
1110

1211

13-
class ImportReqIFCommandConfig:
14-
def __init__(
15-
self,
16-
input_path: str,
17-
output_path: str,
18-
profile: str,
19-
reqif_enable_mid: bool,
20-
reqif_import_markup: Optional[str],
21-
):
22-
self.input_path: str = input_path
23-
self.output_path: str = output_path
24-
self.profile: Optional[str] = profile
25-
self.reqif_enable_mid: bool = reqif_enable_mid
26-
self.reqif_import_markup: Optional[str] = reqif_import_markup
27-
28-
29-
class ImportExcelCommandConfig:
30-
def __init__(
31-
self, input_path: str, output_path: str, parser: SDocArgumentParser
32-
) -> None:
33-
self.input_path: str = input_path
34-
self.output_path: str = output_path
35-
self.parser: SDocArgumentParser = parser
36-
37-
3812
class SDocArgsParser:
3913
def __init__(self, args: argparse.Namespace, registry: Dict[str, Any]):
4014
self.args: argparse.Namespace = args
@@ -57,38 +31,6 @@ def run(self, parallelizer: Parallelizer) -> bool:
5731

5832
return True
5933

60-
@property
61-
def is_import_command_reqif(self) -> bool:
62-
return (
63-
str(self.args.command) == "import"
64-
and str(self.args.import_format) == "reqif"
65-
)
66-
67-
@property
68-
def is_import_command_excel(self) -> bool:
69-
return (
70-
str(self.args.command) == "import"
71-
and str(self.args.import_format) == "excel"
72-
)
73-
74-
@property
75-
def is_server_command(self) -> bool:
76-
return str(self.args.command) == "server"
77-
78-
def get_import_config_reqif(self, _: Any) -> ImportReqIFCommandConfig:
79-
return ImportReqIFCommandConfig(
80-
self.args.input_path,
81-
self.args.output_path,
82-
self.args.profile,
83-
self.args.reqif_enable_mid,
84-
self.args.reqif_import_markup,
85-
)
86-
87-
def get_import_config_excel(self, _: Any) -> ImportExcelCommandConfig:
88-
return ImportExcelCommandConfig(
89-
self.args.input_path, self.args.output_path, self.args.parser
90-
)
91-
9234

9335
def create_sdoc_args_parser(
9436
testing_args: Optional[argparse.Namespace] = None,
Lines changed: 4 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
import argparse
22
import sys
3-
from typing import Any, Dict, NoReturn, Optional
3+
from typing import Any, Dict, NoReturn
44

55
from strictdoc import __version__
6-
from strictdoc.backend.reqif.sdoc_reqif_fields import ReqIFProfile
7-
from strictdoc.backend.sdoc.constants import SDocMarkup
8-
from strictdoc.commands._shared import _check_reqif_profile
96
from strictdoc.commands.about_command import AboutCommand
107
from strictdoc.commands.export import ExportCommand
8+
from strictdoc.commands.import_excel import ImportExcelCommand
9+
from strictdoc.commands.import_reqif import ImportReqIFCommand
1110
from strictdoc.commands.manage_autouid_command import ManageAutoUIDCommand
1211
from strictdoc.commands.server import ServerCommand
1312
from strictdoc.commands.version_command import VersionCommand
1413

15-
EXCEL_PARSERS = ["basic"]
16-
17-
1814
COMMAND_REGISTRY: Dict[str, Any] = {
1915
"about": AboutCommand,
2016
"export": ExportCommand,
17+
"import": {"excel": ImportExcelCommand, "reqif": ImportReqIFCommand},
2118
"manage": {"auto-uid": ManageAutoUIDCommand},
2219
"server": ServerCommand,
2320
"version": VersionCommand,
@@ -30,22 +27,6 @@ def formatter(prog: str) -> argparse.RawTextHelpFormatter:
3027
)
3128

3229

33-
def _check_reqif_import_markup(markup: Optional[str]) -> str:
34-
if markup is None or markup not in SDocMarkup.ALL:
35-
valid_text_markups_string = ", ".join(SDocMarkup.ALL)
36-
message = f"invalid choice: '{markup}' (choose from {valid_text_markups_string})"
37-
raise argparse.ArgumentTypeError(message)
38-
return markup
39-
40-
41-
def add_config_argument(parser: argparse.ArgumentParser) -> None:
42-
parser.add_argument(
43-
"--config",
44-
type=str,
45-
help="Path to the StrictDoc TOML config file.",
46-
)
47-
48-
4930
class SDocArgumentParser(argparse.ArgumentParser):
5031
def error(self, message: str) -> NoReturn:
5132
self.print_usage(sys.stderr)
@@ -93,8 +74,6 @@ def build(self) -> SDocArgumentParser:
9374
)
9475
command_subparsers.required = True
9576

96-
self.add_import_command(command_subparsers)
97-
9877
# Dynamically add subcommands
9978
for name, cmd in COMMAND_REGISTRY.items():
10079
if isinstance(cmd, dict): # command family
@@ -121,104 +100,3 @@ def build(self) -> SDocArgumentParser:
121100
cmd.add_arguments(cmd_parser)
122101

123102
return main_parser
124-
125-
@staticmethod
126-
def add_import_command(
127-
parent_command_parser: "argparse._SubParsersAction[SDocArgumentParser]",
128-
) -> None:
129-
command_parser_import = parent_command_parser.add_parser(
130-
"import",
131-
help="Create StrictDoc files from other formats.",
132-
description="Create StrictDoc files from other formats.",
133-
formatter_class=formatter,
134-
)
135-
command_parser_import_subparsers = command_parser_import.add_subparsers(
136-
title="import_format", dest="import_format"
137-
)
138-
command_parser_import_subparsers.required = True
139-
140-
# Command: Import -> ReqIF.
141-
command_parser_import_reqif = (
142-
command_parser_import_subparsers.add_parser(
143-
"reqif",
144-
help="Create StrictDoc file from ReqIF document.",
145-
description="Create StrictDoc file from ReqIF document.",
146-
formatter_class=formatter,
147-
)
148-
)
149-
150-
command_parser_import_reqif.add_argument(
151-
"profile",
152-
type=_check_reqif_profile,
153-
help=(
154-
"An argument that selects the ReqIF import/export profile. "
155-
f"Possible values: {{{', '.join(ReqIFProfile.ALL)}}}"
156-
),
157-
)
158-
command_parser_import_reqif.add_argument(
159-
"input_path",
160-
type=str,
161-
help="Path to the input ReqIF file.",
162-
)
163-
command_parser_import_reqif.add_argument(
164-
"output_path",
165-
type=str,
166-
help="Path to the output SDoc file.",
167-
)
168-
command_parser_import_reqif.add_argument(
169-
"--reqif-enable-mid",
170-
default=False,
171-
action="store_true",
172-
help=(
173-
"Controls whether StrictDoc's MID field will be mapped to ReqIF "
174-
"SPEC-OBJECT's IDENTIFIER and vice versa when exporting/importing."
175-
),
176-
)
177-
command_parser_import_reqif.add_argument(
178-
"--reqif-import-markup",
179-
default=None,
180-
type=_check_reqif_import_markup,
181-
help=(
182-
"Controls which MARKUP option the imported SDoc documents will have. "
183-
"This value is RST as what StrictDoc has by default but very often "
184-
"the requirements tools use the (X)HTML markup for multiline fields in "
185-
"which case HTML is the best option."
186-
),
187-
)
188-
189-
# Command: Import -> Excel.
190-
command_parser_import_excel = (
191-
command_parser_import_subparsers.add_parser(
192-
"excel",
193-
help="Create StrictDoc file from Excel document.",
194-
description="Create StrictDoc file Excel ReqIF document.",
195-
formatter_class=formatter,
196-
)
197-
)
198-
199-
def check_excel_parser(parser: str) -> str:
200-
if parser not in EXCEL_PARSERS:
201-
message = (
202-
f"invalid choice: '{parser}' (choose from {EXCEL_PARSERS})"
203-
)
204-
raise argparse.ArgumentTypeError(message)
205-
return parser
206-
207-
command_parser_import_excel.add_argument(
208-
"parser",
209-
type=check_excel_parser,
210-
help=(
211-
"An argument that selects the ReqIF parser. "
212-
f"Possible values: {{{', '.join(EXCEL_PARSERS)}}}"
213-
),
214-
)
215-
command_parser_import_excel.add_argument(
216-
"input_path",
217-
type=str,
218-
help="Path to the input ReqIF file.",
219-
)
220-
command_parser_import_excel.add_argument(
221-
"output_path",
222-
type=str,
223-
help="Path to the output SDoc file.",
224-
)

strictdoc/cli/main.py

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@
1616

1717
from strictdoc import environment
1818
from strictdoc.cli.cli_arg_parser import (
19-
ImportExcelCommandConfig,
20-
ImportReqIFCommandConfig,
2119
SDocArgsParser,
2220
create_sdoc_args_parser,
2321
)
24-
from strictdoc.core.actions.import_action import ImportAction
25-
from strictdoc.core.project_config import ProjectConfig, ProjectConfigLoader
2622
from strictdoc.helpers.coverage import register_code_coverage_hook
2723
from strictdoc.helpers.exception import (
2824
ExceptionInfo,
@@ -35,36 +31,10 @@
3531
def _main_internal(parallelizer: Parallelizer, parser: SDocArgsParser) -> None:
3632
register_code_coverage_hook()
3733

38-
project_config: ProjectConfig
39-
4034
if parser.run(parallelizer):
4135
return
4236

43-
#
44-
# FIXME: Migrate the remaining commands.
45-
#
46-
if parser.is_import_command_reqif:
47-
project_config = ProjectConfigLoader.load_from_path_or_get_default(
48-
path_to_config=os.getcwd(),
49-
)
50-
import_reqif_config: ImportReqIFCommandConfig = (
51-
parser.get_import_config_reqif(environment.path_to_strictdoc)
52-
)
53-
import_action = ImportAction()
54-
import_action.do_import(import_reqif_config, project_config)
55-
56-
elif parser.is_import_command_excel:
57-
project_config = ProjectConfigLoader.load_from_path_or_get_default(
58-
path_to_config=os.getcwd(),
59-
)
60-
import_excel_config: ImportExcelCommandConfig = (
61-
parser.get_import_config_excel(environment.path_to_strictdoc)
62-
)
63-
import_action = ImportAction()
64-
import_action.do_import(import_excel_config, project_config)
65-
66-
else:
67-
raise NotImplementedError
37+
raise NotImplementedError
6838

6939

7040
def _main() -> None:

strictdoc/commands/_shared.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ def _check_reqif_profile(profile: str) -> str:
1212
message = f"invalid choice: '{profile}' (choose from {valid_profiles})"
1313
raise argparse.ArgumentTypeError(message)
1414
return profile
15+
16+
17+
def add_config_argument(parser: argparse.ArgumentParser) -> None:
18+
parser.add_argument(
19+
"--config",
20+
type=str,
21+
help="Path to the StrictDoc TOML config file.",
22+
)

strictdoc/commands/import_excel.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import argparse
2+
import os
3+
4+
from strictdoc.cli.base_command import BaseCommand
5+
from strictdoc.commands.import_excel_config import ImportExcelCommandConfig
6+
from strictdoc.core.actions.import_action import ImportAction
7+
from strictdoc.core.project_config import ProjectConfigLoader
8+
from strictdoc.helpers.parallelizer import Parallelizer
9+
10+
EXCEL_PARSERS = ["basic"]
11+
12+
13+
class ImportExcelCommand(BaseCommand):
14+
HELP = "Create StrictDoc file from Excel document."
15+
DETAILED_HELP = HELP
16+
17+
@classmethod
18+
def add_arguments(cls, parser: argparse.ArgumentParser) -> None:
19+
def check_excel_parser(parser: str) -> str:
20+
if parser not in EXCEL_PARSERS:
21+
message = (
22+
f"invalid choice: '{parser}' (choose from {EXCEL_PARSERS})"
23+
)
24+
raise argparse.ArgumentTypeError(message)
25+
return parser
26+
27+
parser.add_argument(
28+
"parser",
29+
type=check_excel_parser,
30+
help=(
31+
"An argument that selects the Excel parser. "
32+
f"Possible values: {{{', '.join(EXCEL_PARSERS)}}}"
33+
),
34+
)
35+
parser.add_argument(
36+
"input_path",
37+
type=str,
38+
help="Path to the input Excel file.",
39+
)
40+
parser.add_argument(
41+
"output_path",
42+
type=str,
43+
help="Path to the output SDoc file.",
44+
)
45+
46+
def __init__(self, args: argparse.Namespace) -> None:
47+
self.args = args
48+
self.config: ImportExcelCommandConfig = ImportExcelCommandConfig(
49+
**vars(args)
50+
)
51+
52+
def run(self, parallelizer: Parallelizer) -> None: # noqa: ARG002
53+
project_config = ProjectConfigLoader.load_from_path_or_get_default(
54+
path_to_config=os.getcwd(),
55+
)
56+
57+
import_action = ImportAction()
58+
import_action.do_import(self.config, project_config)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass
5+
class ImportExcelCommandConfig:
6+
debug: bool
7+
command: str
8+
subcommand: str
9+
parser: str
10+
input_path: str
11+
output_path: str

0 commit comments

Comments
 (0)