Coverage for trlcdoc/convert.py: 97%

35 statements  

« prev     ^ index     » next       coverage.py v7.17.5, created at 2026-11-28 14:00 +0000

1"""Convert the TRLC documents with pyTRLCConverter.""" 

2 

3# trlcdoc - A utility for producing project documentation from TRLC and LOBSTER contents. 

4# Copyright (c) 2026 - 2026 Norbert Schulz github@schulznorbert.de 

5# 

6# This file is part of trlcdoc program. 

7# 

8# The trlcdoc program is free software: you can redistribute it and/or modify it under 

9# the terms of the GNU General Public License as published by the Free Software Foundation, 

10# either version 3 of the License, or (at your option) any later version. 

11# 

12# The trlcdoc program is distributed in the hope that it will be useful, but 

13# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 

14# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 

15# 

16# You should have received a copy of the GNU General Public License along with pyTRLCConverter. 

17# If not, see <https://www.gnu.org/licenses/>. 

18 

19# Imports ********************************************************************** 

20 

21import logging 

22from typing import Any 

23 

24from .config import TRLC_DOCUMENT_TYPES, Config 

25from .plantuml import java_headless_env 

26from .util import TrlcdocError, clean_dir, find_tool, run 

27 

28# Variables ******************************************************************** 

29 

30LOG = logging.getLogger(__name__) 

31 

32# Classes ********************************************************************** 

33 

34# Functions ******************************************************************** 

35 

36 

37def convert_documents(cfg: Config, fmt: str) -> int: 

38 # lobster-trace: SwRequirements.sw_req_trlc_conversion 

39 """Convert all TRLC documents into the given format. 

40 

41 Args: 

42 cfg (Config): Project configuration. 

43 fmt (str): Output format, rst or markdown. 

44 

45 Returns: 

46 int: Number of converted documents. 

47 """ 

48 converted = 0 

49 for doc in cfg.documents: 

50 if doc["type"] in TRLC_DOCUMENT_TYPES: 

51 if fmt in doc["pytrlcconverter"]["converter"]: 

52 convert_document(cfg, doc, fmt) 

53 converted += 1 

54 else: 

55 LOG.info("Skipping %s: no %s converter configured.", doc["id"], fmt) 

56 return converted 

57 

58 

59def convert_document(cfg: Config, doc: dict[str, Any], fmt: str) -> list[str]: 

60 # lobster-trace: SwRequirements.sw_req_trlc_conversion 

61 # lobster-trace: SwRequirements.sw_req_plantuml 

62 """Convert one TRLC document. 

63 

64 Args: 

65 cfg (Config): Project configuration. 

66 doc (dict[str, Any]): Document of type trlc or test_results. 

67 fmt (str): Output format, rst or markdown. 

68 

69 Returns: 

70 list[str]: The executed command. 

71 

72 Raises: 

73 TrlcdocError: If the test results are missing or the conversion fails. 

74 """ 

75 sources = cfg.trlc_sources(doc) 

76 if doc["type"] == "test_results" and not any(cfg.test_results_dir.glob("*.trlc")): 

77 raise TrlcdocError(f"No test results in {cfg.test_results_dir}, run the tests step first.") 

78 

79 out_dir = clean_dir(cfg.convert_dir(fmt, doc["id"]), cfg.output) 

80 LOG.info("Converting %s -> %s", doc["id"], out_dir) 

81 

82 cmd = [find_tool("pyTRLCConverter")] 

83 cmd.extend(f"--source={source}" for source in sources) 

84 cmd.append(f"--include={cfg.trlc_model}") 

85 for source in cfg.included_sources(doc): 

86 cmd.extend([f"--include={source}", f"--exclude={source}"]) 

87 options = doc["pytrlcconverter"] 

88 cmd.extend([f"--out={out_dir}", f"--project={cfg.file_reference(options['converter'][fmt])}"]) 

89 if "translation" in options: 

90 cmd.append(f"--translation={cfg.file_reference(options['translation'])}") 

91 if "render_cfg" in options: 

92 cmd.append(f"--renderCfg={cfg.file_reference(options['render_cfg'])}") 

93 cmd.append(fmt) 

94 

95 run(cmd, cfg.root, env=java_headless_env()) 

96 return cmd 

97 

98 

99# Main *************************************************************************