| Conditions | 14 |
| Total Lines | 72 |
| Code Lines | 40 |
| 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 scripts.config_generator.savu_config.main() 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 | # Copyright 2014 Diamond Light Source Ltd. |
||
| 216 | def main(test=False): |
||
| 217 | """ |
||
| 218 | :param test: If test is True the last argument from sys.argv is removed, |
||
| 219 | as it contains the directory of the test scripts, which fails the |
||
| 220 | parsing of the arguments as it has an unexpected argument. |
||
| 221 | |||
| 222 | If test is False then nothing is touched. |
||
| 223 | """ |
||
| 224 | |||
| 225 | print("Running the configurator") |
||
| 226 | # required for running the tests locally or on travis |
||
| 227 | # drops the last argument from pytest which is the test file/module |
||
| 228 | if test: |
||
| 229 | try: |
||
| 230 | # find where the /scripts argument is |
||
| 231 | index_of_scripts_argument = ["scripts" in arg for arg in sys.argv].index(True) |
||
| 232 | # remove it, including every arguments after it (e.g --cov) |
||
| 233 | sys.argv = sys.argv[:index_of_scripts_argument] |
||
| 234 | except ValueError: |
||
| 235 | # scripts was not part of the arguments passed in by the test |
||
| 236 | pass |
||
| 237 | |||
| 238 | args = parsers._config_arg_parser() |
||
| 239 | if args.error: |
||
| 240 | utils.error_level = 1 |
||
| 241 | |||
| 242 | print("Starting Savu Config tool (please wait for prompt)") |
||
| 243 | |||
| 244 | _reduce_logging_level() |
||
| 245 | |||
| 246 | content = Content(level="all" if args.disp_all else 'user') |
||
| 247 | |||
| 248 | with warnings.catch_warnings(): |
||
| 249 | warnings.simplefilter("ignore") |
||
| 250 | # imports all the (working) plugin modules |
||
| 251 | content.failed = utils.populate_plugins(error_mode=args.error, |
||
| 252 | examples=args.examples) |
||
| 253 | |||
| 254 | comp = Completer(commands=commands, plugin_list=pu.plugins) |
||
| 255 | utils._set_readline(comp.complete) |
||
| 256 | |||
| 257 | |||
| 258 | # if file flag is passed then open it here |
||
| 259 | if args.file: |
||
| 260 | commands['open'](content, args.file) |
||
| 261 | |||
| 262 | print("\n*** Press Enter for a list of available commands. ***\n") |
||
| 263 | |||
| 264 | utils.load_history_file(utils.histfile) |
||
| 265 | |||
| 266 | while True: |
||
| 267 | try: |
||
| 268 | in_list = input(">>> ").strip().split(' ', 1) |
||
| 269 | except KeyboardInterrupt: |
||
| 270 | print() |
||
| 271 | continue |
||
| 272 | except EOFError: |
||
| 273 | # makes possible exiting on CTRL + D (EOF, like Python interpreter) |
||
| 274 | break |
||
| 275 | |||
| 276 | command, arg = in_list if len(in_list) == 2 else in_list+[''] |
||
| 277 | command = command if command else 'help' |
||
| 278 | if command not in commands: |
||
| 279 | print("I'm sorry, that's not a command I recognise. Press Enter " |
||
| 280 | "for a list of available commands.") |
||
| 281 | else: |
||
| 282 | content = commands[command](content, arg) |
||
| 283 | |||
| 284 | if content.is_finished(): |
||
| 285 | break |
||
| 286 | |||
| 287 | print("Thanks for using the application") |
||
| 288 | |||
| 295 |