Coverage for pystratum_common/backend/CommonConstantWorker.py: 0%
76 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-13 08:46 +0200
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-13 08:46 +0200
1import abc
2import configparser
3from typing import Dict, Optional
5from pystratum_backend.ConstantWorker import ConstantWorker
6from pystratum_backend.StratumIO import StratumIO
8from pystratum_common.ConstantClass import ConstantClass
9from pystratum_common.Util import Util
12class CommonConstantWorker(ConstantWorker):
13 """
14 Abstract parent class for RDBMS specific classes for creating constants based on column widths, and auto increment
15 columns and labels.
16 """
18 # ------------------------------------------------------------------------------------------------------------------
19 def __init__(self, io: StratumIO, config: configparser.ConfigParser):
20 """
21 Object constructor.
23 :param PyStratumIO io: The output decorator.
24 """
25 self._constants: Dict[str, int] = {}
26 """
27 All constants.
28 """
30 self._old_columns: Dict = {}
31 """
32 The previous column names, widths, and constant names (i.e. the content of $myConstantsFilename upon
33 starting this program).
34 """
36 self._constants_filename: Optional[str] = None
37 """
38 Filename with column names, their widths, and constant names.
39 """
41 self._prefix: Optional[str] = None
42 """
43 The prefix used for designations a unknown constants.
44 """
46 self._class_name: str = ''
47 """
48 The name of the class that acts like a namespace for constants.
49 """
51 self._labels: Dict = {}
52 """
53 All primary key labels, their widths and constant names.
54 """
56 self._io: StratumIO = io
57 """
58 The output decorator.
59 """
61 self._config = config
62 """
63 The configuration object.
65 :type: ConfigParser
66 """
68 # ------------------------------------------------------------------------------------------------------------------
69 @abc.abstractmethod
70 def connect(self) -> None:
71 """
72 Connects to the database.
73 """
74 raise NotImplementedError()
76 # ------------------------------------------------------------------------------------------------------------------
77 @abc.abstractmethod
78 def disconnect(self) -> None:
79 """
80 Disconnects from the database.
81 """
82 raise NotImplementedError()
84 # ------------------------------------------------------------------------------------------------------------------
85 def execute(self) -> int:
86 """
87 Creates the constants class.
89 :rtype: int
90 """
91 self._read_configuration_file()
93 if self._constants_filename:
94 self._io.title('Constants')
96 self.connect()
97 self._get_old_columns()
98 self._get_columns()
99 self._enhance_columns()
100 self._merge_columns()
101 self._write_columns()
102 self._get_labels()
103 self._fill_constants()
104 self.__write_constant_class()
105 self.disconnect()
106 self.__log_number_of_constants()
108 self._io.write_line('')
109 else:
110 self._io.log_verbose('Constants not enabled')
112 return 0
114 # ------------------------------------------------------------------------------------------------------------------
115 def __log_number_of_constants(self) -> None:
116 """
117 Logs the number of constants generated.
118 """
119 n_id = len(self._labels)
120 n_widths = len(self._constants) - n_id
122 self._io.write_line('')
123 self._io.text('Number of constants based on column widths: {0}'.format(n_widths))
124 self._io.text('Number of constants based on database IDs : {0}'.format(n_id))
126 # ------------------------------------------------------------------------------------------------------------------
127 def _read_configuration_file(self) -> None:
128 """
129 Reads parameters from the configuration file.
130 """
131 self._constants_filename = self._config.get('constants', 'columns')
132 self._prefix = self._config.get('constants', 'prefix')
133 self._class_name = self._config.get('constants', 'class')
134 self._label_regex = self._config.get('constants', 'label_regex')
136 # ------------------------------------------------------------------------------------------------------------------
137 @abc.abstractmethod
138 def _get_old_columns(self) -> None:
139 """
140 Reads from file constants_filename the previous table and column names, the width of the column,
141 and the constant name (if assigned) and stores this data in old_columns.
142 """
143 raise NotImplementedError()
145 # ------------------------------------------------------------------------------------------------------------------
146 @abc.abstractmethod
147 def _get_columns(self) -> None:
148 """
149 Retrieves metadata all columns in the MySQL schema.
150 """
151 raise NotImplementedError()
153 # ------------------------------------------------------------------------------------------------------------------
154 @abc.abstractmethod
155 def _enhance_columns(self) -> None:
156 """
157 Enhances old_columns as follows:
158 If the constant name is *, it is replaced with the column name prefixed by prefix in uppercase.
159 Otherwise, the constant name is set to uppercase.
160 """
161 raise NotImplementedError()
163 # ------------------------------------------------------------------------------------------------------------------
164 @abc.abstractmethod
165 def _merge_columns(self) -> None:
166 """
167 Preserves relevant data in old_columns into columns.
168 """
169 raise NotImplementedError()
171 # ------------------------------------------------------------------------------------------------------------------
172 @abc.abstractmethod
173 def _write_columns(self) -> None:
174 """
175 Writes table and column names, the width of the column, and the constant name (if assigned) to
176 constants_filename.
177 """
178 raise NotImplementedError()
180 # ------------------------------------------------------------------------------------------------------------------
181 @abc.abstractmethod
182 def _get_labels(self) -> None:
183 """
184 Gets all primary key labels from the database.
185 """
186 raise NotImplementedError()
188 # ------------------------------------------------------------------------------------------------------------------
189 @abc.abstractmethod
190 def _fill_constants(self) -> None:
191 """
192 Merges columns and labels (i.e. all known constants) into constants.
193 """
194 raise NotImplementedError()
196 # ------------------------------------------------------------------------------------------------------------------
197 def __write_constant_class(self) -> None:
198 """
199 Inserts new and replaces old (if any) constant declaration statements in the class that acts like a namespace
200 for constants.
201 """
202 helper = ConstantClass(self._class_name, self._io)
203 content = helper.source_with_constants(self._constants)
204 Util.write_two_phases(helper.file_name(), content, self._io)
206# ----------------------------------------------------------------------------------------------------------------------