| Conditions | 16 |
| Total Lines | 162 |
| Code Lines | 123 |
| 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.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 -*- |
||
| 111 | def main(): |
||
| 112 | parser = argparse.ArgumentParser( |
||
| 113 | prog='gvm-pyshell', |
||
| 114 | description=help_text, |
||
| 115 | formatter_class=argparse.RawTextHelpFormatter, |
||
| 116 | add_help=False, |
||
| 117 | epilog=""" |
||
| 118 | usage: gvm-pyshell [-h] [--version] [connection_type] ... |
||
| 119 | or: gvm-pyshell connection_type --help""") |
||
| 120 | subparsers = parser.add_subparsers(metavar='[connection_type]') |
||
| 121 | subparsers.required = True |
||
| 122 | subparsers.dest = 'connection_type' |
||
| 123 | |||
| 124 | parser.add_argument( |
||
| 125 | '-h', '--help', action='help', |
||
| 126 | help='Show this help message and exit.') |
||
| 127 | |||
| 128 | parent_parser = argparse.ArgumentParser(add_help=False) |
||
| 129 | |||
| 130 | parent_parser.add_argument( |
||
| 131 | '-c', '--config', nargs='?', const='~/.config/gvm-tools.conf', |
||
| 132 | help='Configuration file path. Default: ~/.config/gvm-tools.conf') |
||
| 133 | args_before, remaining_args = parent_parser.parse_known_args() |
||
| 134 | |||
| 135 | defaults = { |
||
| 136 | 'gmp_username': '', |
||
| 137 | 'gmp_password': '' |
||
| 138 | } |
||
| 139 | |||
| 140 | # Retrieve data from config file |
||
| 141 | if args_before.config: |
||
| 142 | try: |
||
| 143 | config = configparser.SafeConfigParser() |
||
| 144 | path = os.path.expanduser(args_before.config) |
||
| 145 | config.read(path) |
||
| 146 | defaults = dict(config.items('Auth')) |
||
| 147 | except Exception as e: |
||
| 148 | print(str(e)) |
||
| 149 | |||
| 150 | parent_parser.set_defaults(**defaults) |
||
| 151 | |||
| 152 | parent_parser.add_argument( |
||
| 153 | '--timeout', required=False, default=DEFAULT_TIMEOUT, type=int, |
||
| 154 | help='Wait <seconds> for response or if value -1, then wait ' |
||
| 155 | 'continuously. Default: %(default)s') |
||
| 156 | parent_parser.add_argument( |
||
| 157 | '--log', nargs='?', dest='loglevel', const='INFO', |
||
| 158 | choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
||
| 159 | help='Activates logging. Default level: INFO.') |
||
| 160 | parent_parser.add_argument( |
||
| 161 | '-i', '--interactive', action='store_true', default=False, |
||
| 162 | help='Start an interactive Python shell.') |
||
| 163 | parent_parser.add_argument('--gmp-username', help='GMP username.') |
||
| 164 | parent_parser.add_argument('--gmp-password', help='GMP password.') |
||
| 165 | parent_parser.add_argument( |
||
| 166 | 'script', nargs='*', |
||
| 167 | help='Preload gmp script. Example: myscript.gmp.') |
||
| 168 | |||
| 169 | parser_ssh = subparsers.add_parser( |
||
| 170 | 'ssh', help='Use SSH connection for gmp service.', |
||
| 171 | parents=[parent_parser]) |
||
| 172 | parser_ssh.add_argument('--hostname', required=True, |
||
| 173 | help='Hostname or IP-Address.') |
||
| 174 | parser_ssh.add_argument('--port', required=False, |
||
| 175 | default=22, help='Port. Default: %(default)s.') |
||
| 176 | parser_ssh.add_argument('--ssh-user', default='gmp', |
||
| 177 | help='SSH Username. Default: %(default)s.') |
||
| 178 | |||
| 179 | parser_tls = subparsers.add_parser( |
||
| 180 | 'tls', help='Use TLS secured connection for gmp service.', |
||
| 181 | parents=[parent_parser]) |
||
| 182 | parser_tls.add_argument('--hostname', required=True, |
||
| 183 | help='Hostname or IP-Address.') |
||
| 184 | parser_tls.add_argument('--port', required=False, |
||
| 185 | default=DEFAULT_GVM_PORT, |
||
| 186 | help='Port. Default: %(default)s.') |
||
| 187 | |||
| 188 | parser_socket = subparsers.add_parser( |
||
| 189 | 'socket', help='Use UNIX-Socket connection for gmp service.', |
||
| 190 | parents=[parent_parser]) |
||
| 191 | parser_socket.add_argument( |
||
| 192 | '--sockpath', nargs='?', default=DEFAULT_UNIX_SOCKET_PATH, |
||
| 193 | help='Depreacted. Use --socketpath instead') |
||
| 194 | parser_socket.add_argument( |
||
| 195 | '--socketpath', nargs='?', default=DEFAULT_UNIX_SOCKET_PATH, |
||
| 196 | help='UNIX-Socket path. Default: %(default)s.') |
||
| 197 | |||
| 198 | parser.add_argument( |
||
| 199 | '-V', '--version', action='version', |
||
| 200 | version='%(prog)s {version}'.format(version=__version__), |
||
| 201 | help='Show program\'s version number and exit') |
||
| 202 | |||
| 203 | global args |
||
| 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 | if 'socket' in args.connection_type: |
||
| 217 | socketpath = args.socketpath |
||
| 218 | if socketpath is None: |
||
| 219 | socketpath = args.sockpath |
||
| 220 | |||
| 221 | connection = UnixSocketConnection(path=socketpath, |
||
| 222 | timeout=args.timeout) |
||
| 223 | elif 'tls' in args.connection_type: |
||
| 224 | connection = TLSConnection(hostname=args.hostname, port=args.port, |
||
| 225 | timeout=args.timeout) |
||
| 226 | else: |
||
| 227 | connection = SSHConnection(hostname=args.hostname, port=args.port, |
||
| 228 | timeout=args.timeout, username=args.ssh_user, |
||
| 229 | password='') |
||
| 230 | |||
| 231 | # Ask for login credentials if none are given |
||
| 232 | if not args.gmp_username: |
||
| 233 | while len(args.gmp_username) == 0: |
||
| 234 | args.gmp_username = input('Enter username: ') |
||
| 235 | |||
| 236 | if not args.gmp_password: |
||
| 237 | args.gmp_password = getpass.getpass( |
||
| 238 | 'Enter password for {0}: '.format(args.gmp_username)) |
||
| 239 | |||
| 240 | global gmp |
||
| 241 | |||
| 242 | # return an Etree at successful responses and raise execption on |
||
| 243 | # unsuccessful ones |
||
| 244 | gmp = Gmp(connection, transform=EtreeCheckCommandTransform()) |
||
| 245 | |||
| 246 | try: |
||
| 247 | gmp.authenticate(args.gmp_username, args.gmp_password) |
||
| 248 | except Exception as e: # pylint: disable=broad-except |
||
| 249 | print('Please check your credentials!') |
||
| 250 | print(e) |
||
| 251 | sys.exit(1) |
||
| 252 | |||
| 253 | with_script = args.script and len(args.script) > 0 |
||
| 254 | no_script_no_interactive = not args.interactive and not with_script |
||
| 255 | script_and_interactive = args.interactive and with_script |
||
| 256 | only_interactive = not with_script and args.interactive |
||
| 257 | only_script = not args.interactive and with_script |
||
| 258 | |||
| 259 | if no_script_no_interactive: |
||
| 260 | enter_interactive_mode() |
||
| 261 | |||
| 262 | if only_interactive: |
||
| 263 | enter_interactive_mode() |
||
| 264 | |||
| 265 | if script_and_interactive: |
||
| 266 | load(args.script[0]) |
||
| 267 | enter_interactive_mode() |
||
| 268 | |||
| 269 | if only_script: |
||
| 270 | load(args.script[0]) |
||
| 271 | |||
| 272 | gmp.disconnect() |
||
| 273 | |||
| 320 |
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.