| Conditions | 16 |
| Total Lines | 195 |
| Code Lines | 151 |
| Lines | 23 |
| Ratio | 11.79 % |
| 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.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 -*- |
||
| 129 | def main(): |
||
| 130 | parser = argparse.ArgumentParser( |
||
| 131 | prog='gvm-pyshell', |
||
| 132 | description=HELP_TEXT, |
||
| 133 | formatter_class=argparse.RawTextHelpFormatter, |
||
| 134 | add_help=False, |
||
| 135 | epilog=""" |
||
| 136 | usage: gvm-pyshell [-h] [--version] [connection_type] ... |
||
| 137 | or: gvm-pyshell connection_type --help""") |
||
| 138 | subparsers = parser.add_subparsers(metavar='[connection_type]') |
||
| 139 | subparsers.required = True |
||
| 140 | subparsers.dest = 'connection_type' |
||
| 141 | |||
| 142 | parser.add_argument( |
||
| 143 | '-h', '--help', action='help', |
||
| 144 | help='Show this help message and exit.') |
||
| 145 | |||
| 146 | parent_parser = argparse.ArgumentParser(add_help=False) |
||
| 147 | |||
| 148 | parent_parser.add_argument( |
||
| 149 | '-c', '--config', nargs='?', const='~/.config/gvm-tools.conf', |
||
| 150 | help='Configuration file path. Default: ~/.config/gvm-tools.conf') |
||
| 151 | args_before, remaining_args = parent_parser.parse_known_args() |
||
| 152 | |||
| 153 | defaults = { |
||
| 154 | 'gmp_username': '', |
||
| 155 | 'gmp_password': '' |
||
| 156 | } |
||
| 157 | |||
| 158 | # Retrieve data from config file |
||
| 159 | if args_before.config: |
||
| 160 | try: |
||
| 161 | config = configparser.SafeConfigParser() |
||
| 162 | path = os.path.expanduser(args_before.config) |
||
| 163 | config.read(path) |
||
| 164 | defaults = dict(config.items('Auth')) |
||
| 165 | except Exception as e: # pylint: disable=broad-except |
||
| 166 | print(str(e), file=sys.stderr) |
||
| 167 | |||
| 168 | parent_parser.set_defaults(**defaults) |
||
| 169 | |||
| 170 | parent_parser.add_argument( |
||
| 171 | '--timeout', required=False, default=DEFAULT_TIMEOUT, type=int, |
||
| 172 | help='Wait <seconds> for response or if value -1, then wait ' |
||
| 173 | 'continuously. Default: %(default)s') |
||
| 174 | parent_parser.add_argument( |
||
| 175 | '--log', nargs='?', dest='loglevel', const='INFO', |
||
| 176 | choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
||
| 177 | help='Activates logging. Default level: INFO.') |
||
| 178 | parent_parser.add_argument( |
||
| 179 | '-i', '--interactive', action='store_true', default=False, |
||
| 180 | help='Start an interactive Python shell.') |
||
| 181 | parent_parser.add_argument( |
||
| 182 | '--protocol', required=False, default=DEFAULT_PROTOCOL, |
||
| 183 | choices=[PROTOCOL_GMP, PROTOCOL_OSP], |
||
| 184 | help='Protocol to use. Default: %(default)s.') |
||
| 185 | parent_parser.add_argument('--gmp-username', help='GMP username.') |
||
| 186 | parent_parser.add_argument('--gmp-password', help='GMP password.') |
||
| 187 | parent_parser.add_argument( |
||
| 188 | 'scriptname', nargs='?', metavar="SCRIPT", |
||
| 189 | help='Preload gmp script. Example: myscript.gmp.') |
||
| 190 | parent_parser.add_argument( |
||
| 191 | 'scriptargs', nargs='*', metavar="ARG", |
||
| 192 | help='Arguments for the script.') |
||
| 193 | |||
| 194 | parser_ssh = subparsers.add_parser( |
||
| 195 | 'ssh', help='Use SSH connection for gmp service.', |
||
| 196 | parents=[parent_parser]) |
||
| 197 | parser_ssh.add_argument('--hostname', required=True, |
||
| 198 | help='Hostname or IP-Address.') |
||
| 199 | parser_ssh.add_argument('--port', required=False, |
||
| 200 | default=22, help='Port. Default: %(default)s.') |
||
| 201 | parser_ssh.add_argument('--ssh-user', default='gmp', |
||
| 202 | help='SSH Username. Default: %(default)s.') |
||
| 203 | |||
| 204 | parser_tls = subparsers.add_parser( |
||
| 205 | 'tls', help='Use TLS secured connection for gmp service.', |
||
| 206 | parents=[parent_parser]) |
||
| 207 | parser_tls.add_argument('--hostname', required=True, |
||
| 208 | help='Hostname or IP-Address.') |
||
| 209 | parser_tls.add_argument('--port', required=False, |
||
| 210 | default=DEFAULT_GVM_PORT, |
||
| 211 | help='Port. Default: %(default)s.') |
||
| 212 | parser_tls.add_argument('--certfile', required=False, default=None, |
||
| 213 | help='Path to the client certificate file.') |
||
| 214 | parser_tls.add_argument('--keyfile', required=False, default=None, |
||
| 215 | help='Path to key certificate file.') |
||
| 216 | parser_tls.add_argument('--cafile', required=False, default=None, |
||
| 217 | help='Path to CA certificate file.') |
||
| 218 | parser_tls.add_argument('--no-credentials', required=False, default=False, |
||
| 219 | help='Use only certificates.') |
||
| 220 | |||
| 221 | parser_socket = subparsers.add_parser( |
||
| 222 | 'socket', help='Use UNIX-Socket connection for gmp service.', |
||
| 223 | parents=[parent_parser]) |
||
| 224 | parser_socket.add_argument( |
||
| 225 | '--sockpath', nargs='?', |
||
| 226 | help='Depreacted. Use --socketpath instead') |
||
| 227 | parser_socket.add_argument( |
||
| 228 | '--socketpath', nargs='?', default=DEFAULT_UNIX_SOCKET_PATH, |
||
| 229 | help='UNIX-Socket path. Default: %(default)s.') |
||
| 230 | |||
| 231 | parser.add_argument( |
||
| 232 | '-V', '--version', action='version', |
||
| 233 | version='%(prog)s {version}'.format(version=__version__), |
||
| 234 | help='Show program\'s version number and exit') |
||
| 235 | |||
| 236 | args = parser.parse_args(remaining_args) |
||
| 237 | |||
| 238 | # Sets the logging |
||
| 239 | if args.loglevel is not None: |
||
| 240 | level = logging.getLevelName(args.loglevel) |
||
| 241 | logging.basicConfig(filename='gvm-pyshell.log', level=level) |
||
| 242 | |||
| 243 | # If timeout value is -1, then the socket has no timeout for this session |
||
| 244 | if args.timeout == -1: |
||
| 245 | args.timeout = None |
||
| 246 | |||
| 247 | # Open the right connection. SSH at last for default |
||
| 248 | View Code Duplication | if 'socket' in args.connection_type: |
|
| 249 | socketpath = args.sockpath |
||
| 250 | if socketpath is None: |
||
| 251 | socketpath = args.socketpath |
||
| 252 | else: |
||
| 253 | print('The --sockpath parameter has been deprecated. Please use ' |
||
| 254 | '--socketpath instead', file=sys.stderr) |
||
| 255 | |||
| 256 | connection = UnixSocketConnection(path=socketpath, |
||
| 257 | timeout=args.timeout) |
||
| 258 | elif 'tls' in args.connection_type: |
||
| 259 | connection = TLSConnection( |
||
| 260 | timeout=args.timeout, |
||
| 261 | hostname=args.hostname, |
||
| 262 | port=args.port, |
||
| 263 | certfile=args.certfile, |
||
| 264 | keyfile=args.keyfile, |
||
| 265 | cafile=args.cafile, |
||
| 266 | ) |
||
| 267 | else: |
||
| 268 | connection = SSHConnection(hostname=args.hostname, port=args.port, |
||
| 269 | timeout=args.timeout, username=args.ssh_user, |
||
| 270 | password='') |
||
| 271 | |||
| 272 | transform = EtreeCheckCommandTransform() |
||
| 273 | |||
| 274 | global_vars = { |
||
| 275 | 'help': Help(), |
||
| 276 | } |
||
| 277 | |||
| 278 | username = None |
||
| 279 | password = None |
||
| 280 | |||
| 281 | if args.protocol == PROTOCOL_OSP: |
||
| 282 | protocol = Osp(connection, transform=transform) |
||
| 283 | global_vars['osp'] = protocol |
||
| 284 | global_vars['__name__'] = '__osp__' |
||
| 285 | else: |
||
| 286 | protocol = Gmp(connection, transform=transform) |
||
| 287 | global_vars['gmp'] = protocol |
||
| 288 | global_vars['__name__'] = '__gmp__' |
||
| 289 | |||
| 290 | if args.gmp_username: |
||
| 291 | (username, password) = authenticate( |
||
| 292 | protocol, username=args.gmp_username, |
||
| 293 | password=args.gmp_password) |
||
| 294 | |||
| 295 | shell_args = Arguments( |
||
| 296 | username=username, password=password) |
||
| 297 | |||
| 298 | global_vars['args'] = shell_args |
||
| 299 | |||
| 300 | with_script = args.scriptname and len(args.scriptname) > 0 |
||
| 301 | |||
| 302 | if with_script: |
||
| 303 | argv = [os.path.abspath(args.scriptname), *args.scriptargs] |
||
| 304 | shell_args.argv = argv |
||
| 305 | # for backwards compatibility we add script here |
||
| 306 | shell_args.script = argv |
||
| 307 | |||
| 308 | no_script_no_interactive = not args.interactive and not with_script |
||
| 309 | script_and_interactive = args.interactive and with_script |
||
| 310 | only_interactive = not with_script and args.interactive |
||
| 311 | only_script = not args.interactive and with_script |
||
| 312 | |||
| 313 | if only_interactive or no_script_no_interactive: |
||
| 314 | enter_interactive_mode(global_vars) |
||
| 315 | |||
| 316 | if script_and_interactive or only_script: |
||
| 317 | script_name = args.scriptname |
||
| 318 | load(script_name, global_vars) |
||
| 319 | |||
| 320 | if not only_script: |
||
| 321 | enter_interactive_mode(global_vars) |
||
| 322 | |||
| 323 | protocol.disconnect() |
||
| 324 | |||
| 352 |
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.