| Conditions | 30 | 
| Total Lines | 125 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 11 | ||
| Bugs | 0 | Features | 1 | 
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 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 | """ | ||
| 80 | @click.command(context_settings=dict(auto_envvar_prefix='APEX')) | ||
| 81 | @click.option('-c', | ||
| 82 | '--configfile', | ||
| 83 | type=click.Path(exists=True, file_okay=True, dir_okay=False, readable=True, resolve_path=True), | ||
| 84 | help='Configuration file for APEX.') | ||
| 85 | @click.option('-v', '--verbose', is_flag=True, help='Enables verbose mode.') | ||
| 86 | def main(verbose, configfile): | ||
| 87 | |||
| 88 |     port_map = {} | ||
| 89 | config = find_config(configfile, verbose) | ||
| 90 | if config is None: | ||
| 91 |         click.echo(click.style('Error: ', fg='red', bold=True, blink=True) + | ||
| 92 |                    click.style('No apex configuration found, can not continue.', bold=True)) | ||
| 93 | return | ||
| 94 | for section in config.sections(): | ||
| 95 |         if section.startswith("TNC "): | ||
| 96 |             tnc_name = section.split(" ")[1] | ||
| 97 | if config.has_option(section, 'com_port') and config.has_option(section, 'baud'): | ||
| 98 | com_port = config.get(section, 'com_port') | ||
| 99 | baud = config.get(section, 'baud') | ||
| 100 | kiss_tnc = apex.aprs.AprsKiss(com_port=com_port, baud=baud) | ||
| 101 | elif config.has_option(section, 'tcp_host') and config.has_option(section, 'tcp_port'): | ||
| 102 | tcp_host = config.get(section, 'tcp_host') | ||
| 103 | tcp_port = config.get(section, 'tcp_port') | ||
| 104 | kiss_tnc = apex.aprs.AprsKiss(host=tcp_host, tcp_port=tcp_port) | ||
| 105 | else: | ||
| 106 |                 click.echo(click.style('Error: ', fg='red', bold=True, blink=True) + | ||
| 107 |                            click.style("""Invalid configuration, must have both com_port and baud set or tcp_host and | ||
| 108 | tcp_port set in TNC sections of configuration file""", bold=True)) | ||
| 109 | return | ||
| 110 | |||
| 111 | if not config.has_option(section, 'kiss_init'): | ||
| 112 |                 click.echo(click.style('Error: ', fg='red', bold=True, blink=True) + | ||
| 113 |                            click.style("""Invalid configuration, must have kiss_init set in TNC sections of | ||
| 114 | configuration file""", bold=True)) | ||
| 115 | return | ||
| 116 | kiss_init_string = config.get(section, 'kiss_init') | ||
| 117 | if kiss_init_string == 'MODE_INIT_W8DED': | ||
| 118 | kiss_tnc.start(kissConstants.MODE_INIT_W8DED) | ||
| 119 | elif kiss_init_string == 'MODE_INIT_KENWOOD_D710': | ||
| 120 | kiss_tnc.start(kissConstants.MODE_INIT_KENWOOD_D710) | ||
| 121 | elif kiss_init_string == 'NONE': | ||
| 122 | kiss_tnc.start() | ||
| 123 | else: | ||
| 124 |                 click.echo(click.style('Error: ', fg='red', bold=True, blink=True) + | ||
| 125 |                            click.style('Invalid configuration, value assigned to kiss_init was not recognized: %s' | ||
| 126 | % kiss_init_string, bold=True)) | ||
| 127 | return | ||
| 128 | for port in range(1, 1 + int(config.get(section, 'port_count'))): | ||
| 129 | port_name = tnc_name + '-' + str(port) | ||
| 130 | port_section = 'PORT ' + port_name | ||
| 131 | port_identifier = config.get(port_section, 'identifier') | ||
| 132 | port_net = config.get(port_section, 'net') | ||
| 133 | tnc_port = int(config.get(port_section, 'tnc_port')) | ||
| 134 |                 port_map[port_name] = {'identifier': port_identifier, 'net': port_net, 'tnc': kiss_tnc, | ||
| 135 | 'tnc_port': tnc_port} | ||
| 136 |     if config.has_section('APRS-IS'): | ||
| 137 |         aprsis_callsign = config.get('APRS-IS', 'callsign') | ||
| 138 |         if config.has_option('APRS-IS', 'password'): | ||
| 139 |             aprsis_password = config.get('APRS-IS', 'password') | ||
| 140 | else: | ||
| 141 | aprsis_password = -1 | ||
| 142 |         aprsis_server = config.get('APRS-IS', 'server') | ||
| 143 |         aprsis_server_port = config.get('APRS-IS', 'server_port') | ||
| 144 | aprsis = apex.aprs.AprsInternetService(aprsis_callsign, aprsis_password) | ||
| 145 | aprsis.connect(aprsis_server, int(aprsis_server_port)) | ||
| 146 | |||
| 147 | def sigint_handler(signal, frame): | ||
| 148 | for port in port_map.values(): | ||
| 149 | port['tnc'].close() | ||
| 150 | sys.exit(0) | ||
| 151 | |||
| 152 | signal.signal(signal.SIGINT, sigint_handler) | ||
| 153 | |||
| 154 |     click.echo("Press ctrl + c at any time to exit") | ||
| 155 | |||
| 156 | packet_cache = cachetools.TTLCache(10000, 5) | ||
| 157 | # start the plugins | ||
| 158 | plugins = [] | ||
| 159 | try: | ||
| 160 | plugin_loaders = getPlugins() | ||
| 161 | if not len(plugin_loaders): | ||
| 162 |             click.echo(click.style('Warning: ', fg='yellow') + | ||
| 163 |                        click.style("No plugins were able to be discovered, will only display incoming messages.")) | ||
| 164 | for plugin_loader in plugin_loaders: | ||
| 165 | if verbose: | ||
| 166 |                 click.echo('Plugin found at the following location: %s' % repr(plugin_loader)) | ||
| 167 | loaded_plugin = loadPlugin(plugin_loader) | ||
| 168 | plugins.append(loaded_plugin) | ||
| 169 | threading.Thread(target=loaded_plugin.start, args=(config, port_map, packet_cache, aprsis)).start() | ||
| 170 | except IOError: | ||
| 171 |         click.echo(click.style('Warning: ', fg='yellow') + | ||
| 172 |                    click.style('plugin directory not found, will only display incoming messages.')) | ||
| 173 | |||
| 174 | if verbose: | ||
| 175 |         click.echo('Starting packet processing...') | ||
| 176 | while 1: | ||
| 177 | something_read = False | ||
| 178 | try: | ||
| 179 | for port_name in port_map.keys(): | ||
| 180 | port = port_map[port_name] | ||
| 181 | frame = port['tnc'].read() | ||
| 182 | if frame: | ||
| 183 | formatted_aprs = '>'.join([click.style(frame['source'], fg='green'), click.style(frame['destination'], fg='blue')]) | ||
| 184 | paths = [] | ||
| 185 | for path in frame['path']: | ||
| 186 | paths += click.style(path, fg='magenta') | ||
| 187 | paths = ','.join(paths) | ||
| 188 | if frame['path']: | ||
| 189 | formatted_aprs = ','.join([formatted_aprs, paths]) | ||
| 190 | formatted_aprs += ':' | ||
| 191 | formatted_aprs += frame['text'] | ||
| 192 | click.echo(click.style(port_name + ' << ', fg='magenta') + formatted_aprs) | ||
| 193 | |||
| 194 | for plugin in plugins: | ||
| 195 | something_read = True | ||
| 196 | plugin.handle_packet(frame, port, port_name) | ||
| 197 | except Exception as ex: | ||
| 198 | # We want to keep this thread alive so long as the application runs. | ||
| 199 | traceback.print_exc(file=sys.stdout) | ||
| 200 |             click.echo(click.style('Error: ', fg='red', bold=True, blink=True) + | ||
| 201 |                        click.style('Caught exception while reading packet: %s' % str(ex), bold=True)) | ||
| 202 | |||
| 203 | if something_read is False: | ||
| 204 | time.sleep(1) | ||
| 205 |