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