| Conditions | 9 |
| Total Lines | 115 |
| Code Lines | 85 |
| 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 | # Copyright (C) 2019 Greenbone Networks GmbH |
||
| 35 | def create_args_parser(description): |
||
| 36 | """ Create a command-line arguments parser for OSPD. """ |
||
| 37 | |||
| 38 | parser = argparse.ArgumentParser(description=description) |
||
| 39 | |||
| 40 | def network_port(string): |
||
| 41 | """ Check if provided string is a valid network port. """ |
||
| 42 | |||
| 43 | value = int(string) |
||
| 44 | if not 0 < value <= 65535: |
||
| 45 | raise argparse.ArgumentTypeError( |
||
| 46 | 'port must be in ]0,65535] interval' |
||
| 47 | ) |
||
| 48 | return value |
||
| 49 | |||
| 50 | def cacert_file(cacert): |
||
| 51 | """ Check if provided file is a valid CA Certificate """ |
||
| 52 | try: |
||
| 53 | context = ssl.create_default_context(cafile=cacert) |
||
| 54 | except AttributeError: |
||
| 55 | # Python version < 2.7.9 |
||
| 56 | return cacert |
||
| 57 | except IOError: |
||
| 58 | raise argparse.ArgumentTypeError('CA Certificate not found') |
||
| 59 | try: |
||
| 60 | not_after = context.get_ca_certs()[0]['notAfter'] |
||
| 61 | not_after = ssl.cert_time_to_seconds(not_after) |
||
| 62 | not_before = context.get_ca_certs()[0]['notBefore'] |
||
| 63 | not_before = ssl.cert_time_to_seconds(not_before) |
||
| 64 | except (KeyError, IndexError): |
||
| 65 | raise argparse.ArgumentTypeError('CA Certificate is erroneous') |
||
| 66 | if not_after < int(time.time()): |
||
| 67 | raise argparse.ArgumentTypeError('CA Certificate expired') |
||
| 68 | if not_before > int(time.time()): |
||
| 69 | raise argparse.ArgumentTypeError('CA Certificate not active yet') |
||
| 70 | return cacert |
||
| 71 | |||
| 72 | def log_level(string): |
||
| 73 | """ Check if provided string is a valid log level. """ |
||
| 74 | |||
| 75 | value = getattr(logging, string.upper(), None) |
||
| 76 | if not isinstance(value, int): |
||
| 77 | raise argparse.ArgumentTypeError( |
||
| 78 | 'log level must be one of {debug,info,warning,error,critical}' |
||
| 79 | ) |
||
| 80 | return value |
||
| 81 | |||
| 82 | def filename(string): |
||
| 83 | """ Check if provided string is a valid file path. """ |
||
| 84 | |||
| 85 | if not os.path.isfile(string): |
||
| 86 | raise argparse.ArgumentTypeError( |
||
| 87 | '%s is not a valid file path' % string |
||
| 88 | ) |
||
| 89 | return string |
||
| 90 | |||
| 91 | parser.add_argument( |
||
| 92 | '-p', |
||
| 93 | '--port', |
||
| 94 | default=DEFAULT_PORT, |
||
| 95 | type=network_port, |
||
| 96 | help='TCP Port to listen on. Default: %(default)s', |
||
| 97 | ) |
||
| 98 | parser.add_argument( |
||
| 99 | '-b', |
||
| 100 | '--bind-address', |
||
| 101 | default=DEFAULT_ADDRESS, |
||
| 102 | help='Address to listen on. Default: %(default)s', |
||
| 103 | ) |
||
| 104 | parser.add_argument( |
||
| 105 | '-u', '--unix-socket', help='Unix file socket to listen on.' |
||
| 106 | ) |
||
| 107 | parser.add_argument( |
||
| 108 | '-k', |
||
| 109 | '--key-file', |
||
| 110 | type=filename, |
||
| 111 | help='Server key file. Default: {0}'.format(DEFAULT_KEY_FILE), |
||
| 112 | ) |
||
| 113 | parser.add_argument( |
||
| 114 | '-c', |
||
| 115 | '--cert-file', |
||
| 116 | type=filename, |
||
| 117 | help='Server cert file. Default: {0}'.format(DEFAULT_CERT_FILE), |
||
| 118 | ) |
||
| 119 | parser.add_argument( |
||
| 120 | '--ca-file', |
||
| 121 | type=cacert_file, |
||
| 122 | help='CA cert file. Default: {0}'.format(DEFAULT_CA_FILE), |
||
| 123 | ) |
||
| 124 | parser.add_argument( |
||
| 125 | '-L', |
||
| 126 | '--log-level', |
||
| 127 | default='WARNING', |
||
| 128 | type=log_level, |
||
| 129 | help='Wished level of logging. Default: %(default)s', |
||
| 130 | ) |
||
| 131 | parser.add_argument( |
||
| 132 | '--foreground', |
||
| 133 | action='store_true', |
||
| 134 | help='Run in foreground and logs all messages to console.', |
||
| 135 | ) |
||
| 136 | parser.add_argument( |
||
| 137 | '-l', '--log-file', type=filename, help='Path to the logging file.' |
||
| 138 | ) |
||
| 139 | parser.add_argument( |
||
| 140 | '--version', action='store_true', help='Print version then exit.' |
||
| 141 | ) |
||
| 142 | parser.add_argument( |
||
| 143 | '--niceness', |
||
| 144 | default=DEFAULT_NICENESS, |
||
| 145 | type=int, |
||
| 146 | help='Start the scan with the given niceness. Default %(default)s', |
||
| 147 | ) |
||
| 148 | |||
| 149 | return parser |
||
| 150 | |||
| 194 |