Coverage for packages/pyswig/src/pyswig/fileconfig.py: 95%

79 statements  

« prev     ^ index     » next       coverage.py v7.14.3, created at 2026-06-26 21:05 +0000

1# Copyright (c) 2015-2020 Michel Gillet 

2# SPDX-License-Identifier: MIT 

3 

4from __future__ import annotations 

5 

6from pathlib import Path 

7from typing import TYPE_CHECKING 

8 

9from pyswig.types import SourceFileEntry, TagRecord 

10 

11if TYPE_CHECKING: 

12 from pyswig.pyswig import PySwig 

13 

14 

15class FileConfig: 

16 """Class holding the information about a set of files with same source and output directory""" 

17 

18 def __init__(self) -> None: 

19 """Constructor""" 

20 self.m_src_output_dir: str | None = None 

21 self.m_source: list[SourceFileEntry] | None = None 

22 self.m_output_file_ext: str | None = None 

23 self.m_tags: list[TagRecord] = [] 

24 self.m_do_swig = True 

25 self.m_base_inc_dir: str | None = None 

26 self.m_input_dir: str | None = None 

27 self.m_pyswig: PySwig | None = None 

28 self.m_script_path: str | None = None 

29 self.m_base_abs_path: str | None = None 

30 

31 def set_script_path(self, script_path: str) -> None: 

32 """Set the path to the Python script setting up the whole work 

33 

34 Args: 

35 script_path: the path to the Python script setting up the whole work. 

36 """ 

37 self.m_script_path = script_path 

38 self.m_base_abs_path = str(Path(script_path).resolve().parent) 

39 

40 def get_script_path(self) -> str | None: 

41 """Get the path to the Python script setting up the whole work 

42 

43 Returns: 

44 the path to the Python script setting up the whole work. 

45 """ 

46 return self.m_script_path 

47 

48 def set_base_abs_path(self, base_abs_path: str) -> None: 

49 self.m_base_abs_path = base_abs_path 

50 

51 def get_base_abs_path(self) -> str | None: 

52 return self.m_base_abs_path 

53 

54 def set_base_inc_dir(self, base_inc_dir: str) -> None: 

55 """Set the folder to be used as the base of all indlude files generated by <swig_inc/> 

56 

57 Args: 

58 base_inc_dir: Base path prefix for headers added by the <swig_inc/> annotation. 

59 """ 

60 self.m_base_inc_dir = base_inc_dir 

61 

62 def get_base_inc_dir(self) -> str | None: 

63 """Get the folder to be used as the base of all indlude files generated by <swig_inc/> 

64 

65 Returns: 

66 This is set the beginning of the path for header added by <swig_inc/> annotation. 

67 """ 

68 return self.m_base_inc_dir 

69 

70 def set_pyswig(self, pyswig: PySwig) -> None: 

71 """Set the pySwig instance""" 

72 self.m_pyswig = pyswig 

73 

74 def get_pyswig(self) -> PySwig | None: 

75 """Get the pySwig instance""" 

76 return self.m_pyswig 

77 

78 def set_src_output_dir(self, src_output_dir: str) -> None: 

79 """Set the ouput directory where all generated files will be stored 

80 

81 Args: 

82 src_output_dir: the ouput directory where all generated files will be stored 

83 """ 

84 self.m_src_output_dir = src_output_dir 

85 

86 def get_src_output_dir(self, local: bool = True) -> str | None: 

87 """Get the ouput directory where all generated files will be stored 

88 

89 Args: 

90 local: If True, return the local output path; otherwise use the Swig instance path. 

91 Returns: 

92 the ouput directory where all generated files will be stored 

93 """ 

94 if local: 

95 return self.m_src_output_dir 

96 if self.m_src_output_dir is not None: 

97 return self.m_src_output_dir 

98 pyswig = self.get_pyswig() 

99 if pyswig is None: 

100 return None 

101 return pyswig.get_src_output_dir() 

102 

103 def set_output_file_ext(self, ext: str) -> None: 

104 """Set the extension that the generated files input to Swig will have 

105 

106 Args: 

107 ext: the extension that the generated files input to Swig will have 

108 """ 

109 self.m_output_file_ext = ext 

110 

111 def get_output_file_ext(self, local: bool = True) -> str | None: 

112 """get the extension that the generated files input to Swig will have 

113 

114 Args: 

115 local: If True, return the local extension; otherwise use the Swig instance value. 

116 Returns: 

117 the extension that the generated files input to Swig will have 

118 """ 

119 if local: 

120 return self.m_output_file_ext 

121 if self.m_output_file_ext is not None: 

122 return self.m_output_file_ext 

123 pyswig = self.get_pyswig() 

124 if pyswig is None: 

125 return None 

126 return pyswig.get_output_file_ext() 

127 

128 def set_source_files(self, source: list[SourceFileEntry]) -> None: 

129 """Set the list of source files to process 

130 

131 Args: 

132 source: the list of source files to process 

133 """ 

134 self.m_source = source 

135 

136 def get_source_files(self) -> list[SourceFileEntry] | None: 

137 """Get the list of source file to process 

138 

139 Returns: 

140 the list of source files to process 

141 """ 

142 return self.m_source 

143 

144 def set_input_dir(self, input_dir: str) -> None: 

145 """Set the base of the input directory, so that source files are relative to it 

146 

147 Args: 

148 input_dir: the base of the input directory 

149 """ 

150 self.m_input_dir = input_dir 

151 

152 def get_input_dir(self) -> str | None: 

153 """Get the base of the input directory for source files.""" 

154 return self.m_input_dir 

155 

156 def add_tags(self, the_file: str | None, tags: list[str] | None) -> None: 

157 """Record tags metadata parsed from a source header.""" 

158 self.m_tags.append(TagRecord(the_file, tags)) 

159 

160 def get_tags(self) -> list[TagRecord]: 

161 """Return tags collected while parsing source headers.""" 

162 return list(self.m_tags) 

163 

164 def do_swig(self, enable: bool = True) -> None: 

165 """Enable or disable Swig output generation for this file set.""" 

166 self.m_do_swig = enable 

167 

168 def is_swig_enabled(self) -> bool: 

169 """Return whether Swig output files are generated for this file set.""" 

170 return self.m_do_swig 

171 

172 def save_tags(self, name: str) -> None: 

173 """Write collected tags to a Python list literal file.""" 

174 if not self.m_tags: 

175 return 

176 with open(name, "w", encoding="utf-8") as fout: 

177 fout.write("tags=[\n") 

178 for tag in self.m_tags: 

179 fout.write(f" [{tag.file!r}, {tag.tags!r}],\n") 

180 fout.write("]\n")