| Conditions | 11 |
| Total Lines | 132 |
| Code Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 40 |
| CRAP Score | 11.0411 |
| 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 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 |
|
| 216 | 1 | def _parse_config(self, config_file): |
|
| 217 | """Provide consistent mechanism for pulling in configuration. |
||
| 218 | |||
| 219 | Attempt to retain backward compatibility for |
||
| 220 | existing implementations by grabbing port |
||
| 221 | setting from CLI first. |
||
| 222 | |||
| 223 | Take settings in the following order: |
||
| 224 | |||
| 225 | 1. CLI arguments if present |
||
| 226 | 2. config file |
||
| 227 | 3. OS environment variables (for ease of |
||
| 228 | setting defaults if not present) |
||
| 229 | 4. current defaults if a setting is not present in any location |
||
| 230 | |||
| 231 | Additionally provide similar configuration capabilities in between |
||
| 232 | config file and environment variables. |
||
| 233 | For consistency use the same variable name in the config file as |
||
| 234 | in the os environment. |
||
| 235 | For naming standards use all capitals and start with 'TABPY_' |
||
| 236 | """ |
||
| 237 | 1 | self.settings = {} |
|
| 238 | 1 | self.subdirectory = "" |
|
| 239 | 1 | self.tabpy_state = None |
|
| 240 | 1 | self.python_service = None |
|
| 241 | 1 | self.credentials = {} |
|
| 242 | |||
| 243 | 1 | pkg_path = os.path.dirname(tabpy.__file__) |
|
| 244 | |||
| 245 | 1 | parser = configparser.ConfigParser(os.environ) |
|
| 246 | logger.info(f"Parsing config file {config_file}") |
||
| 247 | 1 | ||
| 248 | 1 | file_exists = False |
|
| 249 | 1 | if os.path.isfile(config_file): |
|
| 250 | try: |
||
| 251 | 1 | with open(config_file, 'r') as f: |
|
| 252 | parser.read_string(f.read()) |
||
| 253 | file_exists = True |
||
| 254 | except Exception: |
||
| 255 | pass |
||
| 256 | 1 | ||
| 257 | if not file_exists: |
||
| 258 | logger.warning( |
||
| 259 | f"Unable to open config file {config_file}, " |
||
| 260 | "using default settings." |
||
| 261 | ) |
||
| 262 | |||
| 263 | settings_parameters = [ |
||
| 264 | (SettingsParameters.Port, ConfigParameters.TABPY_PORT, 9004, None), |
||
| 265 | (SettingsParameters.ServerVersion, None, __version__, None), |
||
| 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 | 1 | (ConfigParameters.TABPY_PWD_FILE, ConfigParameters.TABPY_PWD_FILE, None, None), |
|
| 280 | 1 | (SettingsParameters.LogRequestContext, ConfigParameters.TABPY_LOG_DETAILS, |
|
| 281 | "false", None), |
||
| 282 | 1 | (SettingsParameters.MaxRequestSizeInMb, ConfigParameters.TABPY_MAX_REQUEST_SIZE_MB, |
|
| 283 | 1 | 100, None), |
|
| 284 | ] |
||
| 285 | |||
| 286 | 1 | for setting, parameter, default_val, parse_function in settings_parameters: |
|
| 287 | self._set_parameter(parser, setting, parameter, default_val, parse_function) |
||
| 288 | |||
| 289 | 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 | self.settings[SettingsParameters.TransferProtocol] = self.settings[ |
||
| 294 | 1 | SettingsParameters.TransferProtocol |
|
| 295 | ].lower() |
||
| 296 | |||
| 297 | self._validate_transfer_protocol_settings() |
||
| 298 | |||
| 299 | 1 | # 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 | 1 | os.path.normpath( |
|
| 303 | 1 | os.path.expanduser(self.settings[SettingsParameters.StateFilePath]) |
|
| 304 | ) |
||
| 305 | ) |
||
| 306 | 1 | state_config, self.tabpy_state = self._build_tabpy_state() |
|
| 307 | |||
| 308 | self.python_service = PythonServiceHandler(PythonService()) |
||
| 309 | self.settings["compress_response"] = True |
||
| 310 | self.settings[SettingsParameters.StaticPath] = os.path.abspath( |
||
| 311 | self.settings[SettingsParameters.StaticPath] |
||
| 312 | 1 | ) |
|
| 313 | 1 | logger.debug( |
|
| 314 | f"Static pages folder set to " |
||
| 315 | f'"{self.settings[SettingsParameters.StaticPath]}"' |
||
| 316 | 1 | ) |
|
| 317 | 1 | ||
| 318 | 1 | # Set subdirectory from config if applicable |
|
| 319 | if state_config.has_option("Service Info", "Subdirectory"): |
||
| 320 | self.subdirectory = "/" + state_config.get("Service Info", "Subdirectory") |
||
| 321 | |||
| 322 | 1 | # If passwords file specified load credentials |
|
| 323 | 1 | if ConfigParameters.TABPY_PWD_FILE in self.settings: |
|
| 324 | 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 | 1 | ||
| 336 | features = self._get_features() |
||
| 337 | self.settings[SettingsParameters.ApiVersions] = {"v1": {"features": features}} |
||
| 338 | |||
| 339 | self.settings[SettingsParameters.LogRequestContext] = ( |
||
| 340 | 1 | self.settings[SettingsParameters.LogRequestContext].lower() != "false" |
|
| 341 | ) |
||
| 342 | 1 | call_context_state = ( |
|
| 343 | 1 | "enabled" |
|
| 344 | if self.settings[SettingsParameters.LogRequestContext] |
||
| 345 | else "disabled" |
||
| 346 | ) |
||
| 347 | logger.info(f"Call context logging is {call_context_state}") |
||
| 348 | 1 | ||
| 438 |