Conditions | 11 |
Total Lines | 134 |
Code Lines | 80 |
Lines | 0 |
Ratio | 0 % |
Tests | 43 |
CRAP Score | 11.0105 |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like tabpy.tabpy_server.app.app.TabPyApp._parse_config() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | 1 | import concurrent.futures |
|
214 | 1 | def _parse_config(self, config_file): |
|
215 | """Provide consistent mechanism for pulling in configuration. |
||
216 | |||
217 | Attempt to retain backward compatibility for |
||
218 | existing implementations by grabbing port |
||
219 | setting from CLI first. |
||
220 | |||
221 | Take settings in the following order: |
||
222 | |||
223 | 1. CLI arguments if present |
||
224 | 2. config file |
||
225 | 3. OS environment variables (for ease of |
||
226 | setting defaults if not present) |
||
227 | 4. current defaults if a setting is not present in any location |
||
228 | |||
229 | Additionally provide similar configuration capabilities in between |
||
230 | config file and environment variables. |
||
231 | For consistency use the same variable name in the config file as |
||
232 | in the os environment. |
||
233 | For naming standards use all capitals and start with 'TABPY_' |
||
234 | """ |
||
235 | 1 | self.settings = {} |
|
236 | 1 | self.subdirectory = "" |
|
237 | 1 | self.tabpy_state = None |
|
238 | 1 | self.python_service = None |
|
239 | 1 | self.credentials = {} |
|
240 | |||
241 | 1 | pkg_path = os.path.dirname(tabpy.__file__) |
|
242 | |||
243 | 1 | parser = configparser.ConfigParser(os.environ) |
|
244 | 1 | logger.info(f"Parsing config file {config_file}") |
|
245 | |||
246 | 1 | file_exists = False |
|
247 | 1 | if os.path.isfile(config_file): |
|
248 | 1 | try: |
|
249 | 1 | with open(config_file, 'r') as f: |
|
250 | 1 | parser.read_string(f.read()) |
|
251 | 1 | file_exists = True |
|
252 | except Exception: |
||
253 | pass |
||
254 | |||
255 | 1 | if not file_exists: |
|
256 | 1 | logger.warning( |
|
257 | f"Unable to open config file {config_file}, " |
||
258 | "using default settings." |
||
259 | ) |
||
260 | |||
261 | 1 | settings_parameters = [ |
|
262 | (SettingsParameters.Port, ConfigParameters.TABPY_PORT, 9004, None), |
||
263 | (SettingsParameters.ServerVersion, None, __version__, None), |
||
264 | (SettingsParameters.EvaluateEnabled, ConfigParameters.TABPY_EVALUATE_ENABLE, |
||
265 | True, parser.getboolean), |
||
266 | (SettingsParameters.EvaluateTimeout, ConfigParameters.TABPY_EVALUATE_TIMEOUT, |
||
267 | 30, parser.getfloat), |
||
268 | (SettingsParameters.UploadDir, ConfigParameters.TABPY_QUERY_OBJECT_PATH, |
||
269 | os.path.join(pkg_path, "tmp", "query_objects"), None), |
||
270 | (SettingsParameters.TransferProtocol, ConfigParameters.TABPY_TRANSFER_PROTOCOL, |
||
271 | "http", None), |
||
272 | (SettingsParameters.CertificateFile, ConfigParameters.TABPY_CERTIFICATE_FILE, |
||
273 | None, None), |
||
274 | (SettingsParameters.KeyFile, ConfigParameters.TABPY_KEY_FILE, None, None), |
||
275 | (SettingsParameters.StateFilePath, ConfigParameters.TABPY_STATE_PATH, |
||
276 | os.path.join(pkg_path, "tabpy_server"), None), |
||
277 | (SettingsParameters.StaticPath, ConfigParameters.TABPY_STATIC_PATH, |
||
278 | os.path.join(pkg_path, "tabpy_server", "static"), None), |
||
279 | (ConfigParameters.TABPY_PWD_FILE, ConfigParameters.TABPY_PWD_FILE, None, None), |
||
280 | (SettingsParameters.LogRequestContext, ConfigParameters.TABPY_LOG_DETAILS, |
||
281 | "false", None), |
||
282 | (SettingsParameters.MaxRequestSizeInMb, ConfigParameters.TABPY_MAX_REQUEST_SIZE_MB, |
||
283 | 100, None), |
||
284 | ] |
||
285 | |||
286 | 1 | for setting, parameter, default_val, parse_function in settings_parameters: |
|
287 | 1 | self._set_parameter(parser, setting, parameter, default_val, parse_function) |
|
288 | |||
289 | 1 | if not os.path.exists(self.settings[SettingsParameters.UploadDir]): |
|
290 | 1 | os.makedirs(self.settings[SettingsParameters.UploadDir]) |
|
291 | |||
292 | # set and validate transfer protocol |
||
293 | 1 | self.settings[SettingsParameters.TransferProtocol] = self.settings[ |
|
294 | SettingsParameters.TransferProtocol |
||
295 | ].lower() |
||
296 | |||
297 | 1 | self._validate_transfer_protocol_settings() |
|
298 | |||
299 | # if state.ini does not exist try and create it - remove |
||
300 | # last dependence on batch/shell script |
||
301 | 1 | self.settings[SettingsParameters.StateFilePath] = os.path.realpath( |
|
302 | os.path.normpath( |
||
303 | os.path.expanduser(self.settings[SettingsParameters.StateFilePath]) |
||
304 | ) |
||
305 | ) |
||
306 | 1 | state_config, self.tabpy_state = self._build_tabpy_state() |
|
307 | |||
308 | 1 | self.python_service = PythonServiceHandler(PythonService()) |
|
309 | 1 | self.settings["compress_response"] = True |
|
310 | 1 | self.settings[SettingsParameters.StaticPath] = os.path.abspath( |
|
311 | self.settings[SettingsParameters.StaticPath] |
||
312 | ) |
||
313 | 1 | logger.debug( |
|
314 | f"Static pages folder set to " |
||
315 | f'"{self.settings[SettingsParameters.StaticPath]}"' |
||
316 | ) |
||
317 | |||
318 | # Set subdirectory from config if applicable |
||
319 | 1 | if state_config.has_option("Service Info", "Subdirectory"): |
|
320 | 1 | self.subdirectory = "/" + state_config.get("Service Info", "Subdirectory") |
|
321 | |||
322 | # If passwords file specified load credentials |
||
323 | 1 | if ConfigParameters.TABPY_PWD_FILE in self.settings: |
|
324 | 1 | if not self._parse_pwd_file(): |
|
325 | 1 | msg = ( |
|
326 | "Failed to read passwords file " |
||
327 | f"{self.settings[ConfigParameters.TABPY_PWD_FILE]}" |
||
328 | ) |
||
329 | 1 | logger.critical(msg) |
|
330 | 1 | raise RuntimeError(msg) |
|
331 | else: |
||
332 | 1 | logger.info( |
|
333 | "Password file is not specified: " "Authentication is not enabled" |
||
334 | ) |
||
335 | |||
336 | 1 | features = self._get_features() |
|
337 | 1 | self.settings[SettingsParameters.ApiVersions] = {"v1": {"features": features}} |
|
338 | |||
339 | 1 | self.settings[SettingsParameters.LogRequestContext] = ( |
|
340 | self.settings[SettingsParameters.LogRequestContext].lower() != "false" |
||
341 | ) |
||
342 | 1 | call_context_state = ( |
|
343 | "enabled" |
||
344 | if self.settings[SettingsParameters.LogRequestContext] |
||
345 | else "disabled" |
||
346 | ) |
||
347 | 1 | logger.info(f"Call context logging is {call_context_state}") |
|
348 | |||
438 |