| Conditions | 10 |
| Total Lines | 105 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 run_coala() 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 os |
||
| 21 | def run_coala(log_printer=None, |
||
| 22 | print_results=do_nothing, |
||
| 23 | acquire_settings=fail_acquire_settings, |
||
| 24 | print_section_beginning=do_nothing, |
||
| 25 | nothing_done=do_nothing, |
||
| 26 | autoapply=True, |
||
| 27 | arg_parser=None, |
||
| 28 | arg_list=None): |
||
| 29 | """ |
||
| 30 | This is a main method that should be usable for almost all purposes and |
||
| 31 | reduces executing coala to one function call. |
||
| 32 | |||
| 33 | :param log_printer: A LogPrinter object to use for logging. |
||
| 34 | :param print_results: A callback that takes a LogPrinter, a |
||
| 35 | section, a list of results to be printed, |
||
| 36 | the file dict and the mutable file diff |
||
| 37 | dict. |
||
| 38 | :param acquire_settings: The method to use for requesting settings. |
||
| 39 | It will get a parameter which is a |
||
| 40 | dictionary with the settings name as key |
||
| 41 | and a list containing a description in [0] |
||
| 42 | and the names of the bears who need this |
||
| 43 | setting in all following indexes. |
||
| 44 | :param print_section_beginning: A callback that will be called with a |
||
| 45 | section name string whenever analysis of a |
||
| 46 | new section is started. |
||
| 47 | :param nothing_done: A callback that will be called with only a |
||
| 48 | log printer that shall indicate that |
||
| 49 | nothing was done. |
||
| 50 | :param autoapply: Set to False to autoapply nothing by |
||
| 51 | default; this is overridable via any |
||
| 52 | configuration file/CLI. |
||
| 53 | :param arg_list: The CLI argument list. |
||
| 54 | :return: A dictionary containing a list of results |
||
| 55 | for all analyzed sections as key. |
||
| 56 | """ |
||
| 57 | log_printer = ( |
||
| 58 | LogPrinter(ConsolePrinter(), LOG_LEVEL.DEBUG) if log_printer is None |
||
| 59 | else log_printer) |
||
| 60 | |||
| 61 | exitcode = 0 |
||
| 62 | results = {} |
||
| 63 | file_dicts = {} |
||
| 64 | try: |
||
| 65 | yielded_results = yielded_unfixed_results = False |
||
| 66 | did_nothing = True |
||
| 67 | sections, local_bears, global_bears, targets = gather_configuration( |
||
| 68 | acquire_settings, |
||
| 69 | log_printer, |
||
| 70 | autoapply=autoapply, |
||
| 71 | arg_parser=arg_parser, |
||
| 72 | arg_list=arg_list) |
||
| 73 | |||
| 74 | log_printer.debug("Platform {} -- Python {}, pip {}, coalib {}" |
||
| 75 | .format(platform.system(), platform.python_version(), |
||
| 76 | pip.__version__, VERSION)) |
||
| 77 | |||
| 78 | config_file = os.path.abspath(str(sections["default"].get("config"))) |
||
| 79 | |||
| 80 | settings_hash = get_settings_hash(sections, targets) |
||
| 81 | flush_cache = bool(sections["default"].get("flush_cache", False) or |
||
| 82 | settings_changed(log_printer, settings_hash)) |
||
| 83 | |||
| 84 | disable_caching = bool(sections["default"].get( |
||
| 85 | "disable_caching", False)) |
||
| 86 | cache = None |
||
| 87 | if not sections["default"].get("disable_caching", False): |
||
| 88 | cache = FileCache(log_printer, os.getcwd(), flush_cache) |
||
| 89 | |||
| 90 | for section_name, section in sections.items(): |
||
| 91 | if not section.is_enabled(targets): |
||
| 92 | continue |
||
| 93 | |||
| 94 | print_section_beginning(section) |
||
| 95 | section_result = execute_section( |
||
| 96 | section=section, |
||
| 97 | global_bear_list=global_bears[section_name], |
||
| 98 | local_bear_list=local_bears[section_name], |
||
| 99 | print_results=print_results, |
||
| 100 | cache=cache, |
||
| 101 | log_printer=log_printer) |
||
| 102 | yielded, yielded_unfixed, results[section_name] = ( |
||
| 103 | simplify_section_result(section_result)) |
||
| 104 | |||
| 105 | yielded_results = yielded_results or yielded |
||
| 106 | yielded_unfixed_results = ( |
||
| 107 | yielded_unfixed_results or yielded_unfixed) |
||
| 108 | did_nothing = False |
||
| 109 | |||
| 110 | file_dicts[section_name] = section_result[3] |
||
| 111 | |||
| 112 | update_settings_db(log_printer, settings_hash) |
||
| 113 | if cache: |
||
| 114 | cache.write() |
||
| 115 | |||
| 116 | if did_nothing: |
||
| 117 | nothing_done(log_printer) |
||
| 118 | elif yielded_unfixed_results: |
||
| 119 | exitcode = 1 |
||
| 120 | elif yielded_results: |
||
| 121 | exitcode = 5 |
||
| 122 | except BaseException as exception: # pylint: disable=broad-except |
||
| 123 | exitcode = exitcode or get_exitcode(exception, log_printer) |
||
| 124 | |||
| 125 | return results, exitcode, file_dicts |
||
| 126 |