Conditions | 9 |
Total Lines | 97 |
Code Lines | 33 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | # -*- coding: utf-8 -*- |
||
160 | def start_scan(self, scan_id=None, parallel=1, target=None, |
||
|
|||
161 | ports=None, targets=None, scanner_params=None, |
||
162 | vt_selection=None): |
||
163 | """Start a new scan. |
||
164 | |||
165 | Arguments: |
||
166 | scan_id (str, optional): UUID identifier for a running scan. |
||
167 | parallel (int, optional): Number of parallel scanned targets. |
||
168 | Default 1. |
||
169 | target (dict, optional): Deprecated. Please use targets instead. |
||
170 | targets (list, optional): List of dictionaries. See example. |
||
171 | ports (str, optional): Deprecated. Ports to use for target |
||
172 | parameter. |
||
173 | scanner_params: (dict, optional): Dictionary of scanner parameters. |
||
174 | vt_selection: (dict, optional): Vulnerability tests to select. See |
||
175 | example. |
||
176 | |||
177 | Returns: |
||
178 | str: Response from server. |
||
179 | |||
180 | |||
181 | Examples: |
||
182 | |||
183 | Scanner Parameters:: |
||
184 | |||
185 | scanner_parameters = { |
||
186 | 'scan_param1': 'scan_param1_value', |
||
187 | 'scan_param2': 'scan_param2_value', |
||
188 | } |
||
189 | |||
190 | Targets:: |
||
191 | |||
192 | targets = [{ |
||
193 | 'hosts': 'localhost', |
||
194 | 'ports': '80,43' |
||
195 | }, { |
||
196 | 'hosts': '192.168.0.0/24', |
||
197 | 'ports': '22', |
||
198 | }, { |
||
199 | 'credentials': { |
||
200 | 'smb': { |
||
201 | 'password': 'pass', |
||
202 | 'port': 'port', |
||
203 | 'type': 'type', |
||
204 | 'username': 'username', |
||
205 | } |
||
206 | } |
||
207 | }] |
||
208 | |||
209 | VT Selection:: |
||
210 | |||
211 | vt_selection = { |
||
212 | 'vt1': {}, |
||
213 | 'vt2': {'value_id': 'value'}, |
||
214 | 'vt_groups': ['family=debian', 'family=general'] |
||
215 | } |
||
216 | """ |
||
217 | cmd = XmlCommand('start_scan') |
||
218 | |||
219 | if scan_id: |
||
220 | cmd.set_attribute('scan_id', scan_id) |
||
221 | |||
222 | cmd.set_attribute('parallel', str(parallel)) |
||
223 | |||
224 | # Add <scanner_params> even if it is empty, since it is mandatory |
||
225 | _xmlscanparams = cmd.add_element('scanner_params') |
||
226 | if scanner_params: |
||
227 | _xmlscanparams.set_attributes(scanner_params) |
||
228 | |||
229 | if targets: |
||
230 | _xmltargets = cmd.add_element('targets') |
||
231 | for target in targets: |
||
232 | _xmltarget = _xmltargets.add_element('target') |
||
233 | hosts = target.get('hosts') |
||
234 | ports = target.get('ports') |
||
235 | credentials = target.get('credentials') |
||
236 | _xmltarget.add_element('hosts', hosts) |
||
237 | _xmltarget.add_element('ports', ports) |
||
238 | if credentials: |
||
239 | _xmlcredentials = _xmltarget.add_element('credentials') |
||
240 | _xmlcredentials = (create_credentials_element( |
||
241 | _xmlcredentials, credentials)) |
||
242 | # Check target as attribute for legacy mode compatibility. Deprecated. |
||
243 | elif target: |
||
244 | cmd.set_attribute('target', target) |
||
245 | if ports: |
||
246 | cmd.set_attribute('ports', ports) |
||
247 | else: |
||
248 | raise RequiredArgument('start_scan requires a target. Please pass ' |
||
249 | 'targets parameter.') |
||
250 | |||
251 | if vt_selection: |
||
252 | _xmlvtselection = cmd.add_element('vt_selection') |
||
253 | _xmlvtselection = create_vt_selection_element( |
||
254 | _xmlvtselection, vt_selection) |
||
255 | |||
256 | return self._send_xml_command(cmd) |
||
257 | |||
274 |