| Conditions | 7 |
| Total Lines | 74 |
| Code Lines | 42 |
| 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 | # |
||
| 28 | def load(self, config): |
||
| 29 | """Load the ports list from the configuration file.""" |
||
| 30 | ports_list = [] |
||
| 31 | |||
| 32 | if config is None: |
||
| 33 | logger.debug("No configuration file available. Cannot load ports list.") |
||
| 34 | elif not config.has_section(self._section): |
||
| 35 | logger.debug(f"No [{self._section}] section in the configuration file. Cannot load ports list.") |
||
| 36 | else: |
||
| 37 | logger.debug(f"Start reading the [{self._section}] section in the configuration file") |
||
| 38 | |||
| 39 | refresh = int(config.get_value(self._section, 'refresh', default=self._default_refresh)) |
||
| 40 | timeout = int(config.get_value(self._section, 'timeout', default=self._default_timeout)) |
||
| 41 | |||
| 42 | # Add default gateway on top of the ports_list lists |
||
| 43 | default_gateway = config.get_value(self._section, 'port_default_gateway', default='False') |
||
| 44 | if default_gateway.lower().startswith('true'): |
||
| 45 | new_port = {} |
||
| 46 | # ICMP |
||
| 47 | new_port['host'] = get_default_gateway() |
||
| 48 | new_port['port'] = 0 |
||
| 49 | new_port['description'] = 'DefaultGateway' |
||
| 50 | new_port['refresh'] = refresh |
||
| 51 | new_port['timeout'] = timeout |
||
| 52 | new_port['status'] = None |
||
| 53 | new_port['rtt_warning'] = None |
||
| 54 | new_port['indice'] = 'port_0' |
||
| 55 | logger.debug("Add default gateway {} to the static list".format(new_port['host'])) |
||
| 56 | ports_list.append(new_port) |
||
| 57 | |||
| 58 | # Read the scan list |
||
| 59 | for i in range(1, 256): |
||
| 60 | new_port = {} |
||
| 61 | postfix = f'port_{str(i)}_' |
||
| 62 | |||
| 63 | # Read mandatory configuration key: host |
||
| 64 | new_port['host'] = config.get_value(self._section, '{}{}'.format(postfix, 'host')) |
||
| 65 | |||
| 66 | if new_port['host'] is None: |
||
| 67 | continue |
||
| 68 | |||
| 69 | # Read optionals configuration keys |
||
| 70 | # Port is set to 0 by default. 0 mean ICMP check instead of TCP check |
||
| 71 | new_port['port'] = config.get_value(self._section, '{}{}'.format(postfix, 'port'), 0) |
||
| 72 | new_port['description'] = config.get_value( |
||
| 73 | self._section, f'{postfix}description', default="{}:{}".format(new_port['host'], new_port['port']) |
||
| 74 | ) |
||
| 75 | |||
| 76 | # Default status |
||
| 77 | new_port['status'] = None |
||
| 78 | |||
| 79 | # Refresh rate in second |
||
| 80 | new_port['refresh'] = refresh |
||
| 81 | |||
| 82 | # Timeout in second |
||
| 83 | new_port['timeout'] = int(config.get_value(self._section, f'{postfix}timeout', default=timeout)) |
||
| 84 | |||
| 85 | # RTT warning |
||
| 86 | new_port['rtt_warning'] = config.get_value(self._section, f'{postfix}rtt_warning', default=None) |
||
| 87 | if new_port['rtt_warning'] is not None: |
||
| 88 | # Convert to second |
||
| 89 | new_port['rtt_warning'] = int(new_port['rtt_warning']) / 1000.0 |
||
| 90 | |||
| 91 | # Indice |
||
| 92 | new_port['indice'] = 'port_' + str(i) |
||
| 93 | |||
| 94 | # Add the server to the list |
||
| 95 | logger.debug("Add port {}:{} to the static list".format(new_port['host'], new_port['port'])) |
||
| 96 | ports_list.append(new_port) |
||
| 97 | |||
| 98 | # Ports list loaded |
||
| 99 | logger.debug(f"Ports list loaded: {ports_list}") |
||
| 100 | |||
| 101 | return ports_list |
||
| 102 | |||
| 110 |