Conditions | 12 |
Total Lines | 65 |
Code Lines | 40 |
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 -*- |
||
46 | def main(): |
||
47 | parser = create_parser(description=HELP_TEXT, logfilename='gvm-cli.log') |
||
48 | |||
49 | parser.add_protocol_argument() |
||
50 | |||
51 | parser.add_argument('-X', '--xml', help='XML request to send') |
||
52 | parser.add_argument( |
||
53 | '-r', '--raw', help='Return raw XML', action='store_true', default=False |
||
54 | ) |
||
55 | parser.add_argument('infile', nargs='?', type=open, default=sys.stdin) |
||
56 | |||
57 | args = parser.parse_args() |
||
58 | |||
59 | # If timeout value is -1, then the socket has no timeout for this session |
||
60 | if args.timeout == -1: |
||
61 | args.timeout = None |
||
62 | |||
63 | xml = '' |
||
64 | |||
65 | if args.xml is not None: |
||
66 | xml = args.xml |
||
67 | else: |
||
68 | # If this returns False, then some data are in sys.stdin |
||
69 | if not args.infile.isatty(): |
||
70 | try: |
||
71 | xml = args.infile.read() |
||
72 | except (EOFError, BlockingIOError) as e: |
||
73 | print(e) |
||
74 | |||
75 | # If no command was given, program asks for one |
||
76 | if len(xml) == 0: |
||
77 | xml = input() |
||
78 | |||
79 | connection = create_connection(**vars(args)) |
||
80 | |||
81 | if args.raw: |
||
82 | transform = None |
||
83 | else: |
||
84 | transform = CheckCommandTransform() |
||
85 | |||
86 | if args.protocol == PROTOCOL_OSP: |
||
87 | protocol = Osp(connection, transform=transform) |
||
88 | else: |
||
89 | protocol = Gmp(connection, transform=transform) |
||
90 | |||
91 | # Ask for password if none are given |
||
92 | if args.gmp_username and not args.gmp_password: |
||
93 | args.gmp_password = getpass.getpass( |
||
94 | 'Enter password for ' + args.gmp_username + ': ' |
||
95 | ) |
||
96 | |||
97 | if args.gmp_username: |
||
98 | protocol.authenticate(args.gmp_username, args.gmp_password) |
||
99 | |||
100 | try: |
||
101 | result = protocol.send_command(xml) |
||
102 | |||
103 | print(result) |
||
104 | except Exception as e: # pylint: disable=broad-except |
||
105 | print(e) |
||
106 | sys.exit(1) |
||
107 | |||
108 | protocol.disconnect() |
||
109 | |||
110 | sys.exit(0) |
||
111 | |||
115 |