Conditions | 11 |
Total Lines | 136 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Tests | 47 |
CRAP Score | 11.1077 |
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 |
|
230 | def _parse_config(self, config_file): |
||
231 | """Provide consistent mechanism for pulling in configuration. |
||
232 | |||
233 | Attempt to retain backward compatibility for |
||
234 | existing implementations by grabbing port |
||
235 | setting from CLI first. |
||
236 | |||
237 | Take settings in the following order: |
||
238 | |||
239 | 1 | 1. CLI arguments if present |
|
240 | 1 | 2. config file |
|
241 | 1 | 3. OS environment variables (for ease of |
|
242 | 1 | setting defaults if not present) |
|
243 | 1 | 4. current defaults if a setting is not present in any location |
|
244 | |||
245 | 1 | Additionally provide similar configuration capabilities in between |
|
246 | config file and environment variables. |
||
247 | 1 | For consistency use the same variable name in the config file as |
|
248 | 1 | in the os environment. |
|
249 | For naming standards use all capitals and start with 'TABPY_' |
||
250 | 1 | """ |
|
251 | 1 | self.settings = {} |
|
252 | 1 | self.subdirectory = "" |
|
253 | 1 | self.tabpy_state = None |
|
254 | 1 | self.python_service = None |
|
255 | 1 | self.credentials = {} |
|
256 | |||
257 | pkg_path = os.path.dirname(tabpy.__file__) |
||
258 | |||
259 | 1 | parser = configparser.ConfigParser(os.environ) |
|
260 | 1 | logger.info(f"Parsing config file {config_file}") |
|
261 | |||
262 | file_exists = False |
||
263 | if os.path.isfile(config_file): |
||
264 | try: |
||
265 | 1 | with open(config_file, 'r') as f: |
|
266 | parser.read_string(f.read()) |
||
267 | file_exists = True |
||
268 | except Exception: |
||
269 | pass |
||
270 | |||
271 | if not file_exists: |
||
272 | logger.warning( |
||
273 | f"Unable to open config file {config_file}, " |
||
274 | "using default settings." |
||
275 | ) |
||
276 | |||
277 | settings_parameters = [ |
||
278 | (SettingsParameters.Port, ConfigParameters.TABPY_PORT, 9004, None), |
||
279 | (SettingsParameters.ServerVersion, None, __version__, None), |
||
280 | (SettingsParameters.EvaluateEnabled, ConfigParameters.TABPY_EVALUATE_ENABLE, |
||
281 | True, parser.getboolean), |
||
282 | (SettingsParameters.EvaluateTimeout, ConfigParameters.TABPY_EVALUATE_TIMEOUT, |
||
283 | 30, parser.getfloat), |
||
284 | (SettingsParameters.UploadDir, ConfigParameters.TABPY_QUERY_OBJECT_PATH, |
||
285 | os.path.join(pkg_path, "tmp", "query_objects"), None), |
||
286 | (SettingsParameters.TransferProtocol, ConfigParameters.TABPY_TRANSFER_PROTOCOL, |
||
287 | "http", None), |
||
288 | (SettingsParameters.CertificateFile, ConfigParameters.TABPY_CERTIFICATE_FILE, |
||
289 | None, None), |
||
290 | (SettingsParameters.KeyFile, ConfigParameters.TABPY_KEY_FILE, None, None), |
||
291 | (SettingsParameters.StateFilePath, ConfigParameters.TABPY_STATE_PATH, |
||
292 | 1 | os.path.join(pkg_path, "tabpy_server"), None), |
|
293 | 1 | (SettingsParameters.StaticPath, ConfigParameters.TABPY_STATIC_PATH, |
|
294 | os.path.join(pkg_path, "tabpy_server", "static"), None), |
||
295 | 1 | (ConfigParameters.TABPY_PWD_FILE, ConfigParameters.TABPY_PWD_FILE, None, None), |
|
296 | 1 | (SettingsParameters.LogRequestContext, ConfigParameters.TABPY_LOG_DETAILS, |
|
297 | "false", None), |
||
298 | (SettingsParameters.MaxRequestSizeInMb, ConfigParameters.TABPY_MAX_REQUEST_SIZE_MB, |
||
299 | 1 | 100, None), |
|
300 | (SettingsParameters.GzipEnabled, ConfigParameters.TABPY_GZIP_ENABLED, |
||
301 | True, parser.getboolean), |
||
302 | ] |
||
303 | 1 | ||
304 | for setting, parameter, default_val, parse_function in settings_parameters: |
||
305 | self._set_parameter(parser, setting, parameter, default_val, parse_function) |
||
306 | |||
307 | 1 | if not os.path.exists(self.settings[SettingsParameters.UploadDir]): |
|
308 | os.makedirs(self.settings[SettingsParameters.UploadDir]) |
||
309 | |||
310 | # set and validate transfer protocol |
||
311 | self.settings[SettingsParameters.TransferProtocol] = self.settings[ |
||
312 | 1 | SettingsParameters.TransferProtocol |
|
313 | ].lower() |
||
314 | 1 | ||
315 | 1 | self._validate_transfer_protocol_settings() |
|
316 | 1 | ||
317 | # if state.ini does not exist try and create it - remove |
||
318 | # last dependence on batch/shell script |
||
319 | 1 | self.settings[SettingsParameters.StateFilePath] = os.path.realpath( |
|
320 | os.path.normpath( |
||
321 | os.path.expanduser(self.settings[SettingsParameters.StateFilePath]) |
||
322 | ) |
||
323 | ) |
||
324 | state_config, self.tabpy_state = self._build_tabpy_state() |
||
325 | 1 | ||
326 | 1 | self.python_service = PythonServiceHandler(PythonService()) |
|
327 | self.settings["compress_response"] = True |
||
328 | self.settings[SettingsParameters.StaticPath] = os.path.abspath( |
||
329 | 1 | self.settings[SettingsParameters.StaticPath] |
|
330 | 1 | ) |
|
331 | 1 | logger.debug( |
|
332 | f"Static pages folder set to " |
||
333 | f'"{self.settings[SettingsParameters.StaticPath]}"' |
||
334 | ) |
||
335 | 1 | ||
336 | 1 | # Set subdirectory from config if applicable |
|
337 | if state_config.has_option("Service Info", "Subdirectory"): |
||
338 | 1 | self.subdirectory = "/" + state_config.get("Service Info", "Subdirectory") |
|
339 | |||
340 | # If passwords file specified load credentials |
||
341 | if ConfigParameters.TABPY_PWD_FILE in self.settings: |
||
342 | 1 | if not self._parse_pwd_file(): |
|
343 | 1 | msg = ( |
|
344 | "Failed to read passwords file " |
||
345 | 1 | f"{self.settings[ConfigParameters.TABPY_PWD_FILE]}" |
|
346 | ) |
||
347 | logger.critical(msg) |
||
348 | 1 | raise RuntimeError(msg) |
|
349 | else: |
||
350 | logger.info( |
||
351 | "Password file is not specified: " "Authentication is not enabled" |
||
352 | ) |
||
353 | 1 | ||
354 | features = self._get_features() |
||
355 | 1 | self.settings[SettingsParameters.ApiVersions] = {"v1": {"features": features}} |
|
356 | 1 | ||
357 | self.settings[SettingsParameters.LogRequestContext] = ( |
||
358 | self.settings[SettingsParameters.LogRequestContext].lower() != "false" |
||
359 | ) |
||
360 | call_context_state = ( |
||
361 | 1 | "enabled" |
|
362 | if self.settings[SettingsParameters.LogRequestContext] |
||
363 | 1 | else "disabled" |
|
364 | 1 | ) |
|
365 | logger.info(f"Call context logging is {call_context_state}") |
||
366 | 1 | ||
458 |