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