| Conditions | 10 |
| Total Lines | 135 |
| Code Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
| 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 | import concurrent.futures |
||
| 214 | 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 | self.settings = {} |
||
| 236 | self.subdirectory = "" |
||
| 237 | self.tabpy_state = None |
||
| 238 | self.python_service = None |
||
| 239 | self.credentials = {} |
||
| 240 | |||
| 241 | pkg_path = os.path.dirname(tabpy.__file__) |
||
| 242 | |||
| 243 | parser = configparser.ConfigParser(os.environ) |
||
| 244 | |||
| 245 | if os.path.isfile(config_file): |
||
| 246 | with open(config_file) as f: |
||
| 247 | parser.read_string(f.read()) |
||
| 248 | else: |
||
| 249 | logger.warning( |
||
| 250 | f"Unable to find config file at {config_file}, " |
||
| 251 | "using default settings." |
||
| 252 | ) |
||
| 253 | |||
| 254 | settings_parameters = [ |
||
| 255 | (SettingsParameters.Port, ConfigParameters.TABPY_PORT, 9004), |
||
| 256 | (SettingsParameters.ServerVersion, None, __version__), |
||
| 257 | (SettingsParameters.EvaluateTimeout, ConfigParameters.TABPY_EVALUATE_TIMEOUT, 30), |
||
| 258 | (SettingsParameters.UploadDir, ConfigParameters.TABPY_QUERY_OBJECT_PATH, |
||
| 259 | os.path.join(pkg_path, "tmp", "query_objects")), |
||
| 260 | (SettingsParameters.TransferProtocol, ConfigParameters.TABPY_TRANSFER_PROTOCOL, |
||
| 261 | "http"), |
||
| 262 | (SettingsParameters.CertificateFile, ConfigParameters.TABPY_CERTIFICATE_FILE, |
||
| 263 | None), |
||
| 264 | (SettingsParameters.KeyFile, ConfigParameters.TABPY_KEY_FILE, None), |
||
| 265 | (SettingsParameters.StateFilePath, ConfigParameters.TABPY_STATE_PATH, |
||
| 266 | os.path.join(pkg_path, "tabpy_server")), |
||
| 267 | (SettingsParameters.StaticPath, ConfigParameters.TABPY_STATIC_PATH, |
||
| 268 | os.path.join(pkg_path, "tabpy_server", "static")), |
||
| 269 | (ConfigParameters.TABPY_PWD_FILE, ConfigParameters.TABPY_PWD_FILE, None), |
||
| 270 | (SettingsParameters.LogRequestContext, ConfigParameters.TABPY_LOG_DETAILS, |
||
| 271 | "false"), |
||
| 272 | (SettingsParameters.MaxRequestSizeInMb, ConfigParameters.TABPY_MAX_REQUEST_SIZE_MB, |
||
| 273 | 100), |
||
| 274 | ] |
||
| 275 | |||
| 276 | for setting, parameter, default_val in settings_parameters: |
||
| 277 | self._set_parameter(parser, setting, parameter, default_val) |
||
| 278 | |||
| 279 | try: |
||
| 280 | self.settings[SettingsParameters.EvaluateTimeout] = float( |
||
| 281 | self.settings[SettingsParameters.EvaluateTimeout] |
||
| 282 | ) |
||
| 283 | except ValueError: |
||
| 284 | logger.warning( |
||
| 285 | "Evaluate timeout must be a float type. Defaulting " |
||
| 286 | "to evaluate timeout of 30 seconds." |
||
| 287 | ) |
||
| 288 | self.settings[SettingsParameters.EvaluateTimeout] = 30 |
||
| 289 | |||
| 290 | if not os.path.exists(self.settings[SettingsParameters.UploadDir]): |
||
| 291 | os.makedirs(self.settings[SettingsParameters.UploadDir]) |
||
| 292 | |||
| 293 | # set and validate transfer protocol |
||
| 294 | self.settings[SettingsParameters.TransferProtocol] = self.settings[ |
||
| 295 | SettingsParameters.TransferProtocol |
||
| 296 | ].lower() |
||
| 297 | |||
| 298 | self._validate_transfer_protocol_settings() |
||
| 299 | |||
| 300 | # if state.ini does not exist try and create it - remove |
||
| 301 | # last dependence on batch/shell script |
||
| 302 | self.settings[SettingsParameters.StateFilePath] = os.path.realpath( |
||
| 303 | os.path.normpath( |
||
| 304 | os.path.expanduser(self.settings[SettingsParameters.StateFilePath]) |
||
| 305 | ) |
||
| 306 | ) |
||
| 307 | state_config, self.tabpy_state = self._build_tabpy_state() |
||
| 308 | |||
| 309 | self.python_service = PythonServiceHandler(PythonService()) |
||
| 310 | self.settings["compress_response"] = True |
||
| 311 | self.settings[SettingsParameters.StaticPath] = os.path.abspath( |
||
| 312 | self.settings[SettingsParameters.StaticPath] |
||
| 313 | ) |
||
| 314 | logger.debug( |
||
| 315 | f"Static pages folder set to " |
||
| 316 | f'"{self.settings[SettingsParameters.StaticPath]}"' |
||
| 317 | ) |
||
| 318 | |||
| 319 | # Set subdirectory from config if applicable |
||
| 320 | if state_config.has_option("Service Info", "Subdirectory"): |
||
| 321 | self.subdirectory = "/" + state_config.get("Service Info", "Subdirectory") |
||
| 322 | |||
| 323 | # If passwords file specified load credentials |
||
| 324 | if ConfigParameters.TABPY_PWD_FILE in self.settings: |
||
| 325 | if not self._parse_pwd_file(): |
||
| 326 | msg = ( |
||
| 327 | "Failed to read passwords file " |
||
| 328 | f"{self.settings[ConfigParameters.TABPY_PWD_FILE]}" |
||
| 329 | ) |
||
| 330 | logger.critical(msg) |
||
| 331 | raise RuntimeError(msg) |
||
| 332 | else: |
||
| 333 | logger.info( |
||
| 334 | "Password file is not specified: " "Authentication is not enabled" |
||
| 335 | ) |
||
| 336 | |||
| 337 | features = self._get_features() |
||
| 338 | self.settings[SettingsParameters.ApiVersions] = {"v1": {"features": features}} |
||
| 339 | |||
| 340 | self.settings[SettingsParameters.LogRequestContext] = ( |
||
| 341 | self.settings[SettingsParameters.LogRequestContext].lower() != "false" |
||
| 342 | ) |
||
| 343 | call_context_state = ( |
||
| 344 | "enabled" |
||
| 345 | if self.settings[SettingsParameters.LogRequestContext] |
||
| 346 | else "disabled" |
||
| 347 | ) |
||
| 348 | logger.info(f"Call context logging is {call_context_state}") |
||
| 349 | |||
| 439 |