Conditions | 6 |
Total Lines | 101 |
Code Lines | 79 |
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:
1 | # -*- coding: utf-8 -*- |
||
58 | def execute_send_delta_emails(sc, **kwargs): |
||
59 | gmp = kwargs.get('gmp') |
||
60 | task_tag = kwargs.get('task_tag') |
||
61 | interval = kwargs.get('interval') |
||
62 | email_subject = kwargs.get('email_subject') |
||
63 | to_addresses = kwargs.get('to_addresses') |
||
64 | from_address = kwargs.get('from_address') |
||
65 | mta_address = kwargs.get('mta_address') |
||
66 | mta_user = kwargs.get('mta_user') |
||
67 | mta_port = kwargs.get('mta_port') |
||
68 | mta_password = kwargs.get('mta_password') |
||
69 | report_tag_name = kwargs.get('report_tag_name') |
||
70 | |||
71 | print('Retrieving task list ...') |
||
72 | |||
73 | task_filter = 'tag=%s' % task_tag |
||
74 | tasks = gmp.get_tasks(filter=task_filter).xpath('task') |
||
75 | print('Found %d task(s) with tag "%s".' % (len(tasks), task_tag)) |
||
76 | |||
77 | for task in tasks: |
||
78 | task_id = task.xpath('@id')[0] |
||
79 | print( |
||
80 | 'Processing task "%s" (%s)...' |
||
81 | % (task.xpath('name/text()')[0], task_id) |
||
82 | ) |
||
83 | |||
84 | reports = gmp.get_reports( |
||
85 | filter='task_id={0} and status=Done ' |
||
86 | 'sort-reverse=date'.format(task_id) |
||
87 | ).xpath('report') |
||
88 | print(' Found %d report(s).' % len(reports)) |
||
89 | if len(reports) < 2: |
||
90 | print(' Delta-reporting requires at least 2 finished reports.') |
||
91 | continue |
||
92 | |||
93 | if reports[0].xpath( |
||
94 | 'report/user_tags/tag/' 'name[text()="delta_alert_sent"]' |
||
95 | ): |
||
96 | print(' Delta report for latest finished report already sent') |
||
97 | continue |
||
98 | |||
99 | print( |
||
100 | ' Latest finished report not send yet. Preparing delta ' |
||
101 | 'report...' |
||
102 | ) |
||
103 | |||
104 | delta_report = gmp.get_report( |
||
105 | report_id=reports[0].xpath('@id')[0], |
||
106 | delta_report_id=reports[1].xpath('@id')[0], |
||
107 | filter='delta_states=n', |
||
108 | format_id='c1645568-627a-11e3-a660-406186ea4fc5', |
||
109 | ) |
||
110 | |||
111 | csv_in_b64 = delta_report.xpath('report/text()')[0] |
||
112 | csv = base64.b64decode(csv_in_b64) |
||
113 | |||
114 | print(" Composing Email...") |
||
115 | alert_email = MIMEMultipart() |
||
116 | alert_email['Subject'] = email_subject |
||
117 | alert_email['To'] = ', '.join(to_addresses) |
||
118 | alert_email['From'] = from_address |
||
119 | alert_email['Date'] = formatdate(localtime=True) |
||
120 | |||
121 | report_attachment = MIMEBase('application', "octet-stream") |
||
122 | report_attachment.add_header( |
||
123 | 'Content-Disposition', 'attachment', filename='delta.csv' |
||
124 | ) |
||
125 | report_attachment.set_payload(csv) |
||
126 | alert_email.attach(report_attachment) |
||
127 | |||
128 | print(" Sending Email...") |
||
129 | try: |
||
130 | with smtplib.SMTP(mta_address, mta_port) as smtp: |
||
131 | smtp.ehlo() |
||
132 | smtp.starttls() |
||
133 | smtp.ehlo() |
||
134 | smtp.login(mta_user, mta_password) # if required |
||
135 | smtp.sendmail( |
||
136 | from_address, to_addresses, alert_email.as_string() |
||
137 | ) |
||
138 | smtp.close() |
||
139 | print(" Email has been sent!") |
||
140 | |||
141 | gmp.create_tag( |
||
142 | name=report_tag_name, |
||
143 | resource_id=reports[0].xpath('@id')[0], |
||
144 | resource_type='report', |
||
145 | value=datetime.datetime.now(), |
||
146 | ) |
||
147 | except Exception: # pylint: disable=broad-except |
||
148 | print(" Unable to send the email. Error: ", sys.exc_info()[0]) |
||
149 | # raise # in case an error should stop the script |
||
150 | continue # ignore the problem for the time being |
||
151 | |||
152 | print("\nCheck will be repeated in {} minutes...\n".format(interval)) |
||
153 | sc.enter( |
||
154 | interval * 60, |
||
155 | 1, |
||
156 | execute_send_delta_emails, |
||
157 | argument=(sc,), |
||
158 | kwargs=kwargs, |
||
159 | ) |
||
220 |