| Conditions | 17 |
| Total Lines | 186 |
| Code Lines | 139 |
| Lines | 28 |
| Ratio | 15.05 % |
| 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:
Complex classes like gvmtools.cli.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 | # -*- coding: utf-8 -*- |
||
| 73 | def main(): |
||
| 74 | parser = argparse.ArgumentParser( |
||
| 75 | prog='gvm-cli', |
||
| 76 | description=HELP_TEXT, |
||
| 77 | formatter_class=argparse.RawTextHelpFormatter, |
||
| 78 | add_help=False, |
||
| 79 | epilog=""" |
||
| 80 | usage: gvm-cli [-h] [--version] [connection_type] ... |
||
| 81 | or: gvm-cli connection_type --help""") |
||
| 82 | |||
| 83 | subparsers = parser.add_subparsers(metavar='[connection_type]') |
||
| 84 | subparsers.required = True |
||
| 85 | subparsers.dest = 'connection_type' |
||
| 86 | |||
| 87 | parser.add_argument( |
||
| 88 | '-h', '--help', action='help', |
||
| 89 | help='Show this help message and exit.') |
||
| 90 | |||
| 91 | parent_parser = argparse.ArgumentParser(add_help=False) |
||
| 92 | parent_parser.add_argument( |
||
| 93 | '-c', '--config', nargs='?', const='~/.config/gvm-tools.conf', |
||
| 94 | help='Configuration file path. Default: ~/.config/gvm-tools.conf') |
||
| 95 | args, remaining_args = parent_parser.parse_known_args() |
||
| 96 | |||
| 97 | defaults = { |
||
| 98 | 'gmp_username': '', |
||
| 99 | 'gmp_password': '' |
||
| 100 | } |
||
| 101 | |||
| 102 | # Retrieve data from config file |
||
| 103 | if args.config: |
||
| 104 | try: |
||
| 105 | config = configparser.SafeConfigParser() |
||
| 106 | path = os.path.expanduser(args.config) |
||
| 107 | config.read(path) |
||
| 108 | defaults = dict(config.items('Auth')) |
||
| 109 | except Exception as e: # pylint: disable=broad-except |
||
| 110 | print(str(e)) |
||
| 111 | |||
| 112 | parent_parser.set_defaults(**defaults) |
||
| 113 | |||
| 114 | parent_parser.add_argument( |
||
| 115 | '--timeout', required=False, default=DEFAULT_TIMEOUT, type=int, |
||
| 116 | help='Wait <seconds> for response or if value is -1, then wait ' |
||
| 117 | 'indefinitely. Default: %(default)s.') |
||
| 118 | parent_parser.add_argument( |
||
| 119 | '--log', nargs='?', dest='loglevel', const='INFO', |
||
| 120 | choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
||
| 121 | help='Activates logging. Default level: %(default)s.') |
||
| 122 | parent_parser.add_argument('--gmp-username', help='GMP username.') |
||
| 123 | parent_parser.add_argument('--gmp-password', help='GMP password.') |
||
| 124 | parent_parser.add_argument('-X', '--xml', help='The XML request to send.') |
||
| 125 | parent_parser.add_argument('-r', '--raw', help='Return raw XML.', |
||
| 126 | action='store_true', default=False) |
||
| 127 | parent_parser.add_argument('infile', nargs='?', type=open, |
||
| 128 | default=sys.stdin) |
||
| 129 | |||
| 130 | parser_ssh = subparsers.add_parser( |
||
| 131 | 'ssh', help='Use SSH connection for gmp service.', |
||
| 132 | parents=[parent_parser]) |
||
| 133 | parser_ssh.add_argument('--hostname', required=True, |
||
| 134 | help='Hostname or IP-Address.') |
||
| 135 | parser_ssh.add_argument('--port', required=False, |
||
| 136 | default=22, help='Port. Default: %(default)s.') |
||
| 137 | parser_ssh.add_argument('--ssh-user', default='gmp', |
||
| 138 | help='SSH Username. Default: %(default)s.') |
||
| 139 | parser_ssh.add_argument('--ssh-password', default='gmp', |
||
| 140 | help='SSH Password. Default: %(default)s.') |
||
| 141 | |||
| 142 | parser_tls = subparsers.add_parser( |
||
| 143 | 'tls', help='Use TLS secured connection for gmp service.', |
||
| 144 | parents=[parent_parser]) |
||
| 145 | parser_tls.add_argument('--hostname', required=True, |
||
| 146 | help='Hostname or IP-Address.') |
||
| 147 | parser_tls.add_argument('--port', required=False, |
||
| 148 | default=DEFAULT_GVM_PORT, |
||
| 149 | help='Port. Default: %(default)s.') |
||
| 150 | parser_tls.add_argument('--certfile', required=False, default=None, |
||
| 151 | help='Path to the certificate file.') |
||
| 152 | parser_tls.add_argument('--keyfile', required=False, default=None, |
||
| 153 | help='Path to key certificate file.') |
||
| 154 | parser_tls.add_argument('--cafile', required=False, default=None, |
||
| 155 | help='Path to CA certificate file.') |
||
| 156 | |||
| 157 | parser_socket = subparsers.add_parser( |
||
| 158 | 'socket', help='Use UNIX-Socket connection for gmp service.', |
||
| 159 | parents=[parent_parser]) |
||
| 160 | parser_socket.add_argument( |
||
| 161 | '--sockpath', nargs='?', default=None, |
||
| 162 | help='Depreacted. Use --socketpath instead') |
||
| 163 | parser_socket.add_argument( |
||
| 164 | '--socketpath', nargs='?', default=DEFAULT_UNIX_SOCKET_PATH, |
||
| 165 | help='UNIX-Socket path. Default: %(default)s.') |
||
| 166 | |||
| 167 | parser.add_argument( |
||
| 168 | '-V', '--version', action='version', |
||
| 169 | version='%(prog)s {version}'.format(version=__version__), |
||
| 170 | help='Show program\'s version number and exit') |
||
| 171 | |||
| 172 | args = parser.parse_args(remaining_args) |
||
| 173 | |||
| 174 | # Sets the logging |
||
| 175 | if args.loglevel is not None: |
||
| 176 | level = logging.getLevelName(args.loglevel) |
||
| 177 | logging.basicConfig(filename='gvm-cli.log', level=level) |
||
| 178 | |||
| 179 | # If timeout value is -1, then the socket has no timeout for this session |
||
| 180 | if args.timeout == -1: |
||
| 181 | args.timeout = None |
||
| 182 | |||
| 183 | xml = '' |
||
| 184 | |||
| 185 | if args.xml is not None: |
||
| 186 | xml = args.xml |
||
| 187 | else: |
||
| 188 | # If this returns False, then some data are in sys.stdin |
||
| 189 | if not args.infile.isatty(): |
||
| 190 | try: |
||
| 191 | xml = args.infile.read() |
||
| 192 | except (EOFError, BlockingIOError) as e: |
||
| 193 | print(e) |
||
| 194 | |||
| 195 | # If no command was given, program asks for one |
||
| 196 | if len(xml) == 0: |
||
| 197 | xml = input() |
||
| 198 | |||
| 199 | # Remove all newlines if the commands come from file |
||
| 200 | xml = xml.replace('\n', '').replace('\r', '') |
||
| 201 | |||
| 202 | # Ask for password if none are given |
||
| 203 | if args.gmp_username and not args.gmp_password: |
||
| 204 | args.gmp_password = getpass.getpass('Enter password for ' + |
||
| 205 | args.gmp_username + ': ') |
||
| 206 | |||
| 207 | # Open the right connection. SSH at last for default |
||
| 208 | View Code Duplication | if 'socket' in args.connection_type: |
|
| 209 | socketpath = args.sockpath |
||
| 210 | if socketpath is None: |
||
| 211 | socketpath = args.socketpath |
||
| 212 | else: |
||
| 213 | print('The --sockpath parameter has been deprecated. Please use ' |
||
| 214 | '--socketpath instead', file=sys.stderr) |
||
| 215 | |||
| 216 | connection = UnixSocketConnection( |
||
| 217 | timeout=args.timeout, |
||
| 218 | path=socketpath |
||
| 219 | ) |
||
| 220 | elif 'tls' in args.connection_type: |
||
| 221 | connection = TLSConnection( |
||
| 222 | timeout=args.timeout, |
||
| 223 | hostname=args.hostname, |
||
| 224 | port=args.port, |
||
| 225 | certfile=args.certfile, |
||
| 226 | keyfile=args.keyfile, |
||
| 227 | cafile=args.cafile, |
||
| 228 | ) |
||
| 229 | else: |
||
| 230 | connection = SSHConnection( |
||
| 231 | timeout=args.timeout, |
||
| 232 | hostname=args.hostname, |
||
| 233 | port=args.port, |
||
| 234 | username=args.ssh_user, |
||
| 235 | password=args.ssh_password |
||
| 236 | ) |
||
| 237 | |||
| 238 | if args.raw: |
||
| 239 | transform = None |
||
| 240 | else: |
||
| 241 | transform = CheckCommandTransform() |
||
| 242 | |||
| 243 | gvm = Gmp(connection, transform=transform) |
||
| 244 | |||
| 245 | if args.gmp_username: |
||
| 246 | gvm.authenticate(args.gmp_username, args.gmp_password) |
||
| 247 | |||
| 248 | try: |
||
| 249 | result = gvm.send_command(xml) |
||
| 250 | |||
| 251 | print(result) |
||
| 252 | except Exception as e: # pylint: disable=broad-except |
||
| 253 | print(e) |
||
| 254 | sys.exit(1) |
||
| 255 | |||
| 256 | gvm.disconnect() |
||
| 257 | |||
| 258 | sys.exit(0) |
||
| 259 | |||
| 263 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.