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