Skip to content

Commit 65aa406

Browse files
authored
Merge pull request #154 from strictdoc-project/stanislaw/examples
examples/04_convert_reqif_to_json: --stdout parameter
2 parents 3ac0a51 + 12b6a63 commit 65aa406

3 files changed

Lines changed: 39 additions & 12 deletions

File tree

tests/integration/examples/04_convert_reqif_to_json/expected/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
"ReqIF.Text",
2424
"Status"
2525
]
26-
}
26+
}

tests/integration/examples/04_convert_reqif_to_json/script.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,28 +187,51 @@ def main():
187187
"input_file", type=str, help="Path to the input ReqIF file."
188188
)
189189
main_parser.add_argument(
190-
"output_dir", type=str, help="Path to the output dir."
190+
"--output-dir",
191+
type=str,
192+
help="Path to the output dir.",
193+
default="output/",
194+
)
195+
main_parser.add_argument(
196+
"--stdout",
197+
help="Makes the script write the output ReqIF to standard output.",
198+
action="store_true",
199+
)
200+
main_parser.add_argument(
201+
"--no-filesystem",
202+
help=(
203+
"Disables writing to the file system. "
204+
"Should be used in combination with --stdout."
205+
),
206+
action="store_true",
191207
)
192208

193209
args = main_parser.parse_args()
194210
input_file: str = args.input_file
211+
should_use_file_system: bool = not args.no_filesystem
212+
should_use_stdout: bool = args.stdout
213+
195214
if input_file.endswith(".reqifz"):
196215
reqifz_bundle: ReqIFZBundle = ReqIFZParser.parse(input_file)
197216
assert len(reqifz_bundle.reqif_bundles) == 1
198217
reqif_bundle = (list(reqifz_bundle.reqif_bundles.values()))[0]
199218
else:
200219
reqif_bundle = ReqIFParser.parse(input_file)
201220

202-
path_to_output_dir = args.output_dir
203-
Path(path_to_output_dir).mkdir(exist_ok=True)
204-
205221
req_dict = ReqIFToDictConverter.convert(reqif_bundle)
222+
reqif_json = json.dumps(req_dict.get_as_dict(), indent=4)
206223

207-
path_to_output_file = os.path.join(path_to_output_dir, "output.json")
208-
with open(path_to_output_file, "w", encoding="utf8") as json_file:
209-
json.dump(
210-
req_dict.get_as_dict(), json_file, ensure_ascii=False, indent=4
211-
)
224+
if should_use_file_system:
225+
path_to_output_dir = args.output_dir
226+
Path(path_to_output_dir).mkdir(exist_ok=True)
227+
228+
path_to_output_file = os.path.join(path_to_output_dir, "output.json")
229+
with open(path_to_output_file, "w", encoding="utf8") as json_file:
230+
json_file.write(reqif_json)
231+
json_file.write("\n")
232+
233+
if should_use_stdout:
234+
print(reqif_json) # noqa: T201
212235

213236

214237
main()

tests/integration/examples/04_convert_reqif_to_json/test.itest

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ RUN: mkdir -p %S/Output
22
RUN: %reqif passthrough %S/sample.reqif %S/Output/sample.reqif
33
RUN: %diff %S/sample.reqif %S/Output/sample.reqif
44

5-
RUN: python %S/script.py %S/sample.reqif %S/Output/reqif
5+
RUN: python %S/script.py %S/sample.reqif --output-dir %S/Output/reqif
66
RUN: %diff %S/expected/output.json %S/Output/reqif/output.json
77

8-
RUN: python %S/script.py %S/sample.reqifz %S/Output/reqifz
8+
RUN: mkdir %S/Output/reqif_stdout
9+
RUN: python %S/script.py %S/sample.reqif --stdout --no-filesystem | tee %S/Output/reqif_stdout/output.json
10+
RUN: %diff %S/expected/output.json %S/Output/reqif_stdout/output.json
11+
12+
RUN: python %S/script.py %S/sample.reqifz --output-dir %S/Output/reqifz
913
RUN: %diff %S/expected/output.json %S/Output/reqifz/output.json
1014

1115
TODO:

0 commit comments

Comments
 (0)