Conditions | 12 |
Total Lines | 58 |
Lines | 0 |
Ratio | 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 doorpi.action.SingleActions.fire_action_mail() 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 | #!/usr/bin/env python |
||
21 | def fire_action_mail(smtp_to, smtp_subject, smtp_text, smtp_snapshot): |
||
22 | try: |
||
23 | smtp_host = doorpi.DoorPi().config.get('SMTP', 'server', 'smtp.gmail.com') |
||
24 | smtp_port = doorpi.DoorPi().config.get_int('SMTP', 'port', 465) |
||
25 | smtp_user = doorpi.DoorPi().config.get('SMTP', 'username') |
||
26 | smtp_password = doorpi.DoorPi().config.get('SMTP', 'password') |
||
27 | smtp_from = doorpi.DoorPi().config.get('SMTP', 'from') |
||
28 | |||
29 | smtp_use_tls = doorpi.DoorPi().config.get_boolean('SMTP', 'use_tls', False) |
||
30 | smtp_use_ssl = doorpi.DoorPi().config.get_boolean('SMTP', 'use_ssl', True) |
||
31 | smtp_need_login = doorpi.DoorPi().config.get_boolean('SMTP', 'need_login', True) |
||
32 | |||
33 | smtp_tolist = smtp_to.split() |
||
34 | |||
35 | email_signature = doorpi.DoorPi().config.get_string_parsed('SMTP', 'signature', '!EPILOG!') |
||
36 | |||
37 | if smtp_use_ssl: |
||
38 | server = smtplib.SMTP_SSL(smtp_host, smtp_port) |
||
39 | else: |
||
40 | server = smtplib.SMTP(smtp_host, smtp_port) |
||
41 | |||
42 | server.ehlo() |
||
43 | if smtp_use_tls and not smtp_use_ssl: |
||
44 | server.starttls() |
||
45 | if smtp_need_login: |
||
46 | server.login(smtp_user, smtp_password) |
||
47 | |||
48 | msg = MIMEMultipart() |
||
49 | msg['From'] = smtp_from |
||
50 | msg['To'] = COMMASPACE.join(smtp_tolist) |
||
51 | msg['Subject'] = doorpi.DoorPi().parse_string(smtp_subject) |
||
52 | msg.attach(MIMEText(doorpi.DoorPi().parse_string(smtp_text), 'html')) |
||
53 | if email_signature and len(email_signature) > 0: |
||
54 | msg.attach(MIMEText('\nsent by:\n'+doorpi.DoorPi().epilog, 'plain')) |
||
55 | |||
56 | if smtp_snapshot: |
||
57 | smtp_snapshot = doorpi.DoorPi().parse_string(smtp_snapshot) |
||
58 | if not os.path.exists(smtp_snapshot): |
||
59 | smtp_snapshot = get_last_snapshot() |
||
60 | |||
61 | try: |
||
62 | with open(smtp_snapshot, "rb") as snapshot_file: |
||
63 | part = MIMEBase('application',"octet-stream") |
||
64 | part.set_payload(snapshot_file.read()) |
||
65 | Encoders.encode_base64(part) |
||
66 | part.add_header( |
||
67 | 'Content-Disposition', |
||
68 | 'attachment; filename="%s"' % os.path.basename(smtp_snapshot)) |
||
69 | msg.attach(part) |
||
70 | except Exception as exp: |
||
71 | logger.exception("send not attachment for this mail: %s" % exp) |
||
72 | |||
73 | server.sendmail(smtp_from, smtp_tolist, msg.as_string()) |
||
74 | server.quit() |
||
75 | except: |
||
76 | logger.exception("couldn't send email") |
||
77 | return False |
||
78 | return True |
||
79 | |||
102 |