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