| Conditions | 13 |
| Total Lines | 157 |
| Code Lines | 123 |
| Lines | 23 |
| Ratio | 14.65 % |
| 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.pyshell.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 -*- |
||
| 104 | def main(): |
||
| 105 | parser = argparse.ArgumentParser( |
||
| 106 | prog='gvm-pyshell', |
||
| 107 | description=HELP_TEXT, |
||
| 108 | formatter_class=argparse.RawTextHelpFormatter, |
||
| 109 | add_help=False, |
||
| 110 | epilog=""" |
||
| 111 | usage: gvm-pyshell [-h] [--version] [connection_type] ... |
||
| 112 | or: gvm-pyshell connection_type --help""") |
||
| 113 | subparsers = parser.add_subparsers(metavar='[connection_type]') |
||
| 114 | subparsers.required = True |
||
| 115 | subparsers.dest = 'connection_type' |
||
| 116 | |||
| 117 | parser.add_argument( |
||
| 118 | '-h', '--help', action='help', |
||
| 119 | help='Show this help message and exit.') |
||
| 120 | |||
| 121 | parent_parser = argparse.ArgumentParser(add_help=False) |
||
| 122 | |||
| 123 | parent_parser.add_argument( |
||
| 124 | '-c', '--config', nargs='?', const='~/.config/gvm-tools.conf', |
||
| 125 | help='Configuration file path. Default: ~/.config/gvm-tools.conf') |
||
| 126 | args_before, remaining_args = parent_parser.parse_known_args() |
||
| 127 | |||
| 128 | defaults = { |
||
| 129 | 'gmp_username': '', |
||
| 130 | 'gmp_password': '' |
||
| 131 | } |
||
| 132 | |||
| 133 | # Retrieve data from config file |
||
| 134 | if args_before.config: |
||
| 135 | try: |
||
| 136 | config = configparser.SafeConfigParser() |
||
| 137 | path = os.path.expanduser(args_before.config) |
||
| 138 | config.read(path) |
||
| 139 | defaults = dict(config.items('Auth')) |
||
| 140 | except Exception as e: # pylint: disable=broad-except |
||
| 141 | print(str(e), file=sys.stderr) |
||
| 142 | |||
| 143 | parent_parser.set_defaults(**defaults) |
||
| 144 | |||
| 145 | parent_parser.add_argument( |
||
| 146 | '--timeout', required=False, default=DEFAULT_TIMEOUT, type=int, |
||
| 147 | help='Wait <seconds> for response or if value -1, then wait ' |
||
| 148 | 'continuously. Default: %(default)s') |
||
| 149 | parent_parser.add_argument( |
||
| 150 | '--log', nargs='?', dest='loglevel', const='INFO', |
||
| 151 | choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
||
| 152 | help='Activates logging. Default level: INFO.') |
||
| 153 | parent_parser.add_argument( |
||
| 154 | '-i', '--interactive', action='store_true', default=False, |
||
| 155 | help='Start an interactive Python shell.') |
||
| 156 | parent_parser.add_argument('--gmp-username', help='GMP username.') |
||
| 157 | parent_parser.add_argument('--gmp-password', help='GMP password.') |
||
| 158 | parent_parser.add_argument( |
||
| 159 | 'script', nargs='*', |
||
| 160 | help='Preload gmp script. Example: myscript.gmp.') |
||
| 161 | |||
| 162 | parser_ssh = subparsers.add_parser( |
||
| 163 | 'ssh', help='Use SSH connection for gmp service.', |
||
| 164 | parents=[parent_parser]) |
||
| 165 | parser_ssh.add_argument('--hostname', required=True, |
||
| 166 | help='Hostname or IP-Address.') |
||
| 167 | parser_ssh.add_argument('--port', required=False, |
||
| 168 | default=22, help='Port. Default: %(default)s.') |
||
| 169 | parser_ssh.add_argument('--ssh-user', default='gmp', |
||
| 170 | help='SSH Username. Default: %(default)s.') |
||
| 171 | |||
| 172 | parser_tls = subparsers.add_parser( |
||
| 173 | 'tls', help='Use TLS secured connection for gmp service.', |
||
| 174 | parents=[parent_parser]) |
||
| 175 | parser_tls.add_argument('--hostname', required=True, |
||
| 176 | help='Hostname or IP-Address.') |
||
| 177 | parser_tls.add_argument('--port', required=False, |
||
| 178 | default=DEFAULT_GVM_PORT, |
||
| 179 | help='Port. Default: %(default)s.') |
||
| 180 | parser_tls.add_argument('--certfile', required=False, default=None, |
||
| 181 | help='Path to the client certificate file.') |
||
| 182 | parser_tls.add_argument('--keyfile', required=False, default=None, |
||
| 183 | help='Path to key certificate file.') |
||
| 184 | parser_tls.add_argument('--cafile', required=False, default=None, |
||
| 185 | help='Path to CA certificate file.') |
||
| 186 | parser_tls.add_argument('--no-credentials', required=False, default=False, |
||
| 187 | help='Use only certificates.') |
||
| 188 | |||
| 189 | parser_socket = subparsers.add_parser( |
||
| 190 | 'socket', help='Use UNIX-Socket connection for gmp service.', |
||
| 191 | parents=[parent_parser]) |
||
| 192 | parser_socket.add_argument( |
||
| 193 | '--sockpath', nargs='?', |
||
| 194 | help='Depreacted. Use --socketpath instead') |
||
| 195 | parser_socket.add_argument( |
||
| 196 | '--socketpath', nargs='?', default=DEFAULT_UNIX_SOCKET_PATH, |
||
| 197 | help='UNIX-Socket path. Default: %(default)s.') |
||
| 198 | |||
| 199 | parser.add_argument( |
||
| 200 | '-V', '--version', action='version', |
||
| 201 | version='%(prog)s {version}'.format(version=__version__), |
||
| 202 | help='Show program\'s version number and exit') |
||
| 203 | |||
| 204 | args = parser.parse_args(remaining_args) |
||
| 205 | |||
| 206 | # Sets the logging |
||
| 207 | if args.loglevel is not None: |
||
| 208 | level = logging.getLevelName(args.loglevel) |
||
| 209 | logging.basicConfig(filename='gvm-pyshell.log', level=level) |
||
| 210 | |||
| 211 | # If timeout value is -1, then the socket has no timeout for this session |
||
| 212 | if args.timeout == -1: |
||
| 213 | args.timeout = None |
||
| 214 | |||
| 215 | # Open the right connection. SSH at last for default |
||
| 216 | View Code Duplication | if 'socket' in args.connection_type: |
|
| 217 | socketpath = args.sockpath |
||
| 218 | if socketpath is None: |
||
| 219 | socketpath = args.socketpath |
||
| 220 | else: |
||
| 221 | print('The --sockpath parameter has been deprecated. Please use ' |
||
| 222 | '--socketpath instead', file=sys.stderr) |
||
| 223 | |||
| 224 | connection = UnixSocketConnection(path=socketpath, |
||
| 225 | timeout=args.timeout) |
||
| 226 | elif 'tls' in args.connection_type: |
||
| 227 | connection = TLSConnection( |
||
| 228 | timeout=args.timeout, |
||
| 229 | hostname=args.hostname, |
||
| 230 | port=args.port, |
||
| 231 | certfile=args.certfile, |
||
| 232 | keyfile=args.keyfile, |
||
| 233 | cafile=args.cafile, |
||
| 234 | ) |
||
| 235 | else: |
||
| 236 | connection = SSHConnection(hostname=args.hostname, port=args.port, |
||
| 237 | timeout=args.timeout, username=args.ssh_user, |
||
| 238 | password='') |
||
| 239 | |||
| 240 | gmp = Gmp(connection, transform=EtreeCheckCommandTransform()) |
||
| 241 | |||
| 242 | with_script = args.script and len(args.script) > 0 |
||
| 243 | no_script_no_interactive = not args.interactive and not with_script |
||
| 244 | script_and_interactive = args.interactive and with_script |
||
| 245 | only_interactive = not with_script and args.interactive |
||
| 246 | only_script = not args.interactive and with_script |
||
| 247 | |||
| 248 | global_vars = get_globals_dict(gmp, args) |
||
| 249 | |||
| 250 | if only_interactive or no_script_no_interactive: |
||
| 251 | enter_interactive_mode(global_vars) |
||
| 252 | |||
| 253 | if script_and_interactive or only_script: |
||
| 254 | script_name = args.script[0] |
||
| 255 | load(script_name, global_vars) |
||
| 256 | |||
| 257 | if not only_script: |
||
| 258 | enter_interactive_mode(global_vars) |
||
| 259 | |||
| 260 | gmp.disconnect() |
||
| 261 | |||
| 296 |
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.