Conditions | 5 |
Total Lines | 91 |
Code Lines | 51 |
Lines | 73 |
Ratio | 80.22 % |
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:
1 | # -*- coding: utf-8 -*- |
||
142 | def get_alerts(gmp, sender_email, recipient_email, debug=False): |
||
143 | # configurable alert name |
||
144 | alert_name = recipient_email |
||
145 | |||
146 | # create alert if necessary |
||
147 | alert_object = gmp.get_alerts(filter='name=%s' % alert_name) |
||
148 | alert_id = None |
||
149 | alert = alert_object.xpath('alert') |
||
150 | View Code Duplication | if len(alert) == 0: |
|
151 | gmp.create_alert( |
||
152 | alert_name, |
||
153 | event=gmp.types.AlertEvent.TASK_RUN_STATUS_CHANGED, |
||
154 | event_data={'status': 'Done'}, |
||
155 | condition=gmp.types.AlertCondition.ALWAYS, |
||
156 | method=gmp.types.AlertMethod.EMAIL, |
||
157 | method_data={ |
||
158 | """Task '$n': $e |
||
159 | |||
160 | After the event $e, |
||
161 | the following condition was met: $c |
||
162 | |||
163 | This email escalation is configured to attach report format '$r'. |
||
164 | Full details and other report formats are available on the scan engine. |
||
165 | |||
166 | $t |
||
167 | |||
168 | Note: |
||
169 | This email was sent to you as a configured security scan escalation. |
||
170 | Please contact your local system administrator if you think you |
||
171 | should not have received it. |
||
172 | """: "message", |
||
173 | "2": "notice", |
||
174 | sender_email: "from_address", |
||
175 | "[OpenVAS-Manager] Task": "subject", |
||
176 | "c402cc3e-b531-11e1-9163-406186ea4fc5": "notice_attach_format", |
||
177 | recipient_email: "to_address", |
||
178 | }, |
||
179 | ) |
||
180 | alert_object = gmp.get_alerts(filter='name=%s' % recipient_email) |
||
181 | alert = alert_object.xpath('alert') |
||
182 | alert_id = alert[0].get('id', 'no id found') |
||
183 | else: |
||
184 | alert_id = alert[0].get('id', 'no id found') |
||
185 | if debug: |
||
186 | print("alert_id: %s" % str(alert_id)) |
||
187 | |||
188 | # second configurable alert name |
||
189 | alert_name2 = "%s-2" % recipient_email |
||
190 | |||
191 | # create second alert if necessary |
||
192 | alert_object2 = gmp.get_alerts(filter='name=%s' % alert_name2) |
||
193 | alert_id2 = None |
||
194 | alert2 = alert_object2.xpath('alert') |
||
195 | View Code Duplication | if len(alert2) == 0: |
|
196 | gmp.create_alert( |
||
197 | alert_name2, |
||
198 | event=gmp.types.AlertEvent.TASK_RUN_STATUS_CHANGED, |
||
199 | event_data={'status': 'Done'}, |
||
200 | condition=gmp.types.AlertCondition.ALWAYS, |
||
201 | method=gmp.types.AlertMethod.EMAIL, |
||
202 | method_data={ |
||
203 | """Task '$n': $e |
||
204 | |||
205 | After the event $e, |
||
206 | the following condition was met: $c |
||
207 | |||
208 | This email escalation is configured to attach report format '$r'. |
||
209 | Full details and other report formats are available on the scan engine. |
||
210 | |||
211 | $t |
||
212 | |||
213 | Note: |
||
214 | This email was sent to you as a configured security scan escalation. |
||
215 | Please contact your local system administrator if you think you |
||
216 | should not have received it. |
||
217 | """: "message", |
||
218 | "2": "notice", |
||
219 | sender_email: "from_address", |
||
220 | "[OpenVAS-Manager] Task": "subject", |
||
221 | recipient_email: "to_address", |
||
222 | }, |
||
223 | ) |
||
224 | alert_object2 = gmp.get_alerts(filter='name=%s' % recipient_email) |
||
225 | alert2 = alert_object2.xpath('alert') |
||
226 | alert_id2 = alert2[0].get('id', 'no id found') |
||
227 | else: |
||
228 | alert_id2 = alert2[0].get('id', 'no id found') |
||
229 | if debug: |
||
230 | print("alert_id2: %s" % str(alert_id2)) |
||
231 | |||
232 | return (alert_id, alert_id2) |
||
233 | |||
288 |