| Conditions | 8 |
| Total Lines | 57 |
| Code Lines | 45 |
| 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:
| 1 | import logging |
||
| 133 | @gen.coroutine |
||
| 134 | def on_state_change(settings, tabpy_state, python_service, |
||
| 135 | logger=logging.getLogger(__name__)): |
||
| 136 | try: |
||
| 137 | logger.log(logging.INFO, "Loading state from state file") |
||
| 138 | config = util._get_state_from_file( |
||
| 139 | settings[SettingsParameters.StateFilePath], |
||
| 140 | logger=logger) |
||
| 141 | new_ps_state = TabPyState(config=config, settings=settings) |
||
| 142 | |||
| 143 | (has_changes, changes) = _get_latest_service_state(settings, |
||
| 144 | tabpy_state, |
||
| 145 | new_ps_state, |
||
| 146 | python_service) |
||
| 147 | if not has_changes: |
||
| 148 | logger.info("Nothing changed, return.") |
||
| 149 | return |
||
| 150 | |||
| 151 | new_endpoints = new_ps_state.get_endpoints() |
||
| 152 | for object_name in changes['endpoints']: |
||
| 153 | (object_type, object_version, object_path) = changes['endpoints'][ |
||
| 154 | object_name] |
||
| 155 | |||
| 156 | if not object_path and not object_version: # removal |
||
| 157 | logger.info(f'Removing object: URI={object_name}') |
||
| 158 | |||
| 159 | python_service.manage_request(DeleteObjects([object_name])) |
||
| 160 | |||
| 161 | cleanup_endpoint_files(object_name, |
||
| 162 | settings[SettingsParameters.UploadDir], |
||
| 163 | logger=logger) |
||
| 164 | |||
| 165 | else: |
||
| 166 | endpoint_info = new_endpoints[object_name] |
||
| 167 | is_update = object_version > 1 |
||
| 168 | if object_type == 'alias': |
||
| 169 | msg = LoadObject(object_name, endpoint_info['target'], |
||
| 170 | object_version, is_update, 'alias') |
||
| 171 | else: |
||
| 172 | local_path = object_path |
||
| 173 | msg = LoadObject(object_name, local_path, object_version, |
||
| 174 | is_update, object_type) |
||
| 175 | |||
| 176 | python_service.manage_request(msg) |
||
| 177 | wait_for_endpoint_loaded(python_service, object_name) |
||
| 178 | |||
| 179 | # cleanup old version of endpoint files |
||
| 180 | if object_version > 2: |
||
| 181 | cleanup_endpoint_files( |
||
| 182 | object_name, settings[SettingsParameters.UploadDir], |
||
| 183 | logger=logger, |
||
| 184 | retain_versions=[object_version, object_version - 1]) |
||
| 185 | |||
| 186 | except Exception as e: |
||
| 187 | err_msg = format_exception(e, 'on_state_change') |
||
| 188 | logger.log(logging.ERROR, |
||
| 189 | f'Error submitting update model request: error={err_msg}') |
||
| 190 |