| Conditions | 12 |
| Total Lines | 72 |
| Code Lines | 46 |
| 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 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 -*- |
||
| 57 | def main(): |
||
| 58 | do_not_run_as_root() |
||
| 59 | |||
| 60 | parser = create_parser(description=HELP_TEXT, logfilename='gvm-cli.log') |
||
| 61 | |||
| 62 | parser.add_protocol_argument() |
||
| 63 | |||
| 64 | parser.add_argument('-X', '--xml', help='XML request to send') |
||
| 65 | parser.add_argument( |
||
| 66 | '-r', '--raw', help='Return raw XML', action='store_true', default=False |
||
| 67 | ) |
||
| 68 | parser.add_argument( |
||
| 69 | 'infile', nargs='?', help='File to read XML commands from.' |
||
| 70 | ) |
||
| 71 | |||
| 72 | args = parser.parse_args() |
||
| 73 | |||
| 74 | # If timeout value is -1, then the socket has no timeout for this session |
||
| 75 | if args.timeout == -1: |
||
| 76 | args.timeout = None |
||
| 77 | |||
| 78 | if args.xml is not None: |
||
| 79 | xml = args.xml |
||
| 80 | else: |
||
| 81 | try: |
||
| 82 | xml = _load_infile(args.infile) |
||
| 83 | except IOError as e: |
||
| 84 | print(e, file=sys.stderr) |
||
| 85 | sys.exit(1) |
||
| 86 | |||
| 87 | # If no command was given, program asks for one |
||
| 88 | if len(xml) == 0: |
||
| 89 | xml = input() |
||
| 90 | |||
| 91 | try: |
||
| 92 | validate_xml_string(xml) |
||
| 93 | except GvmError as e: |
||
| 94 | print(e, file=sys.stderr) |
||
| 95 | sys.exit(1) |
||
| 96 | |||
| 97 | connection = create_connection(**vars(args)) |
||
| 98 | |||
| 99 | if args.raw: |
||
| 100 | transform = None |
||
| 101 | else: |
||
| 102 | transform = CheckCommandTransform() |
||
| 103 | |||
| 104 | if args.protocol == PROTOCOL_OSP: |
||
| 105 | protocol = Osp(connection, transform=transform) |
||
| 106 | else: |
||
| 107 | protocol = Gmp(connection, transform=transform) |
||
| 108 | |||
| 109 | # Ask for password if none are given |
||
| 110 | if args.gmp_username and not args.gmp_password: |
||
| 111 | args.gmp_password = getpass.getpass( |
||
| 112 | 'Enter password for ' + args.gmp_username + ': ' |
||
| 113 | ) |
||
| 114 | |||
| 115 | if args.gmp_username: |
||
| 116 | protocol.authenticate(args.gmp_username, args.gmp_password) |
||
| 117 | |||
| 118 | try: |
||
| 119 | result = protocol.send_command(xml) |
||
| 120 | |||
| 121 | print(result) |
||
| 122 | except Exception as e: # pylint: disable=broad-except |
||
| 123 | print(e, file=sys.stderr) |
||
| 124 | sys.exit(1) |
||
| 125 | |||
| 126 | protocol.disconnect() |
||
| 127 | |||
| 128 | sys.exit(0) |
||
| 129 | |||
| 133 |