Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import abc 

2import configparser 

3from typing import Dict, Optional 

4 

5from pystratum_backend.ConstantWorker import ConstantWorker 

6from pystratum_backend.StratumStyle import StratumStyle 

7from pystratum_common.ConstantClass import ConstantClass 

8from pystratum_common.Util import Util 

9 

10 

11class CommonConstantWorker(ConstantWorker): 

12 """ 

13 Abstract parent class for RDBMS specific classes for creating constants based on column widths, and auto increment 

14 columns and labels. 

15 """ 

16 

17 # ------------------------------------------------------------------------------------------------------------------ 

18 def __init__(self, io: StratumStyle, config: configparser.ConfigParser): 

19 """ 

20 Object constructor. 

21 

22 :param PyStratumStyle io: The output decorator. 

23 """ 

24 self._constants: Dict[str, int] = {} 

25 """ 

26 All constants. 

27 """ 

28 

29 self._old_columns: Dict = {} 

30 """ 

31 The previous column names, widths, and constant names (i.e. the content of $myConstantsFilename upon 

32 starting this program). 

33 """ 

34 

35 self._constants_filename: Optional[str] = None 

36 """ 

37 Filename with column names, their widths, and constant names. 

38 """ 

39 

40 self._prefix: Optional[str] = None 

41 """ 

42 The prefix used for designations a unknown constants. 

43 """ 

44 

45 self._class_name: str = '' 

46 """ 

47 The name of the class that acts like a namespace for constants. 

48 """ 

49 

50 self._labels: Dict = {} 

51 """ 

52 All primary key labels, their widths and constant names. 

53 """ 

54 

55 self._io: StratumStyle = io 

56 """ 

57 The output decorator. 

58 """ 

59 

60 self._config = config 

61 """ 

62 The configuration object. 

63 

64 :type: ConfigParser  

65 """ 

66 

67 # ------------------------------------------------------------------------------------------------------------------ 

68 @abc.abstractmethod 

69 def connect(self) -> None: 

70 """ 

71 Connects to the database. 

72 """ 

73 raise NotImplementedError() 

74 

75 # ------------------------------------------------------------------------------------------------------------------ 

76 @abc.abstractmethod 

77 def disconnect(self) -> None: 

78 """ 

79 Disconnects from the database. 

80 """ 

81 raise NotImplementedError() 

82 

83 # ------------------------------------------------------------------------------------------------------------------ 

84 def execute(self) -> int: 

85 """ 

86 Creates the constants class. 

87  

88 :rtype: int 

89 """ 

90 self._read_configuration_file() 

91 

92 if self._constants_filename: 

93 self._io.title('Constants') 

94 

95 self.connect() 

96 self._get_old_columns() 

97 self._get_columns() 

98 self._enhance_columns() 

99 self._merge_columns() 

100 self._write_columns() 

101 self._get_labels() 

102 self._fill_constants() 

103 self.__write_constant_class() 

104 self.disconnect() 

105 self.__log_number_of_constants() 

106 

107 self._io.writeln('') 

108 else: 

109 self._io.log_verbose('Constants not enabled') 

110 

111 return 0 

112 

113 # ------------------------------------------------------------------------------------------------------------------ 

114 def __log_number_of_constants(self) -> None: 

115 """ 

116 Logs the number of constants generated. 

117 """ 

118 n_id = len(self._labels) 

119 n_widths = len(self._constants) - n_id 

120 

121 self._io.writeln('') 

122 self._io.text('Number of constants based on column widths: {0}'.format(n_widths)) 

123 self._io.text('Number of constants based on database IDs : {0}'.format(n_id)) 

124 

125 # ------------------------------------------------------------------------------------------------------------------ 

126 def _read_configuration_file(self) -> None: 

127 """ 

128 Reads parameters from the configuration file. 

129 """ 

130 self._constants_filename = self._config.get('constants', 'columns') 

131 self._prefix = self._config.get('constants', 'prefix') 

132 self._class_name = self._config.get('constants', 'class') 

133 self._label_regex = self._config.get('constants', 'label_regex') 

134 

135 # ------------------------------------------------------------------------------------------------------------------ 

136 @abc.abstractmethod 

137 def _get_old_columns(self) -> None: 

138 """ 

139 Reads from file constants_filename the previous table and column names, the width of the column, 

140 and the constant name (if assigned) and stores this data in old_columns. 

141 """ 

142 raise NotImplementedError() 

143 

144 # ------------------------------------------------------------------------------------------------------------------ 

145 @abc.abstractmethod 

146 def _get_columns(self) -> None: 

147 """ 

148 Retrieves metadata all columns in the MySQL schema. 

149 """ 

150 raise NotImplementedError() 

151 

152 # ------------------------------------------------------------------------------------------------------------------ 

153 @abc.abstractmethod 

154 def _enhance_columns(self) -> None: 

155 """ 

156 Enhances old_columns as follows: 

157 If the constant name is *, is is replaced with the column name prefixed by prefix in uppercase. 

158 Otherwise the constant name is set to uppercase. 

159 """ 

160 raise NotImplementedError() 

161 

162 # ------------------------------------------------------------------------------------------------------------------ 

163 @abc.abstractmethod 

164 def _merge_columns(self) -> None: 

165 """ 

166 Preserves relevant data in old_columns into columns. 

167 """ 

168 raise NotImplementedError() 

169 

170 # ------------------------------------------------------------------------------------------------------------------ 

171 @abc.abstractmethod 

172 def _write_columns(self) -> None: 

173 """ 

174 Writes table and column names, the width of the column, and the constant name (if assigned) to 

175 constants_filename. 

176 """ 

177 raise NotImplementedError() 

178 

179 # ------------------------------------------------------------------------------------------------------------------ 

180 @abc.abstractmethod 

181 def _get_labels(self) -> None: 

182 """ 

183 Gets all primary key labels from the database. 

184 """ 

185 raise NotImplementedError() 

186 

187 # ------------------------------------------------------------------------------------------------------------------ 

188 @abc.abstractmethod 

189 def _fill_constants(self) -> None: 

190 """ 

191 Merges columns and labels (i.e. all known constants) into constants. 

192 """ 

193 raise NotImplementedError() 

194 

195 # ------------------------------------------------------------------------------------------------------------------ 

196 def __write_constant_class(self) -> None: 

197 """ 

198 Inserts new and replaces old (if any) constant declaration statements in the class that acts like a namespace 

199 for constants. 

200 """ 

201 helper = ConstantClass(self._class_name, self._io) 

202 

203 content = helper.source_with_constants(self._constants) 

204 

205 Util.write_two_phases(helper.file_name(), content, self._io) 

206 

207# ----------------------------------------------------------------------------------------------------------------------