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