Skip to content

Commit bc8a408

Browse files
committed
more test coverage
1 parent cc933ef commit bc8a408

44 files changed

Lines changed: 181 additions & 155 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apprise/plugins/aprs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def __init__(self, targets=None, locale=None, delay=None, **kwargs):
286286
except (TypeError, ValueError):
287287
msg = f"Unsupported APRS-IS delay ({delay}) specified. "
288288
self.logger.warning(msg)
289-
raise TypeError(msg)
289+
raise TypeError(msg) from None
290290

291291
# Bump up our request_rate
292292
self.request_rate_per_sec += self.delay

apprise/plugins/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ def _apply_overflow(
712712

713713
# Detect padding and prepare template
714714
digits = len(str(count))
715-
template = " [{:0%d}/{:0%d}]" % (digits, digits)
715+
template = f" [{{:0{digits}d}}/{{:0{digits}d}}]"
716716

717717
# Update our counter
718718
overflow_display_count_width = 4 + (digits * 2)

apprise/plugins/burstsms.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def __init__(
226226
" invalid."
227227
)
228228
self.logger.warning(msg)
229-
raise TypeError(msg)
229+
raise TypeError(msg) from None
230230

231231
# Prepare Batch Mode Flag
232232
self.batch = (
@@ -343,15 +343,17 @@ def send(self, body, title="", notify_type=NotifyType.INFO, **kwargs):
343343

344344
else:
345345
self.logger.info(
346-
"Sent Burst SMS notification to %d target(s)."
347-
% len(self.targets[index : index + batch_size])
346+
"Sent Burst SMS notification to "
347+
f"{len(self.targets[index : index + batch_size])} "
348+
"target(s)."
348349
)
349350

350351
except requests.RequestException as e:
351352
self.logger.warning(
352-
"A Connection error occurred sending Burst SMS "
353-
"notification to %d target(s)."
354-
% len(self.targets[index : index + batch_size])
353+
f"A Connection error occurred sending Burst SMS "
354+
"notification to "
355+
f"{len(self.targets[index : index + batch_size])} "
356+
"target(s)."
355357
)
356358
self.logger.debug(f"Socket Exception: {e!s}")
357359

apprise/plugins/custom_form.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,9 @@ def send(
318318
if attachment.name
319319
else f"file{no:03}.dat"
320320
),
321-
open(attachment.path, "rb"),
321+
# file handle is safely closed in `finally`; inline
322+
# open is intentional
323+
open(attachment.path, "rb"), # noqa: SIM115
322324
attachment.mimetype,
323325
),
324326
))
@@ -365,7 +367,7 @@ def send(
365367

366368
url = f"{schema}://{self.host}"
367369
if isinstance(self.port, int):
368-
url += ":%d" % self.port
370+
url += f":{self.port}"
369371

370372
url += self.fullpath
371373

apprise/plugins/custom_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def send(
281281

282282
url = f"{schema}://{self.host}"
283283
if isinstance(self.port, int):
284-
url += ":%d" % self.port
284+
url += f":{self.port}"
285285

286286
url += self.fullpath
287287

apprise/plugins/custom_xml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def send(
372372

373373
url = f"{schema}://{self.host}"
374374
if isinstance(self.port, int):
375-
url += ":%d" % self.port
375+
url += f":{self.port}"
376376

377377
url += self.fullpath
378378
payload = re_table.sub(lambda x: re_map[x.group()], self.payload)

apprise/plugins/dbus.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ class NotifyDBus(NotifyBase):
208208
"default": DBusUrgency.NORMAL,
209209
},
210210
"priority": {
211-
# Apprise uses 'priority' everywhere; it's just a nice consistent
212-
# feel to be able to use it here as well. Just map the
213-
# value back to 'priority'
211+
# Apprise uses 'priority' everywhere; it's just a nice
212+
# consistent feel to be able to use it here as well. Just map
213+
# the value back to 'priority'
214214
"alias_of": "urgency",
215215
},
216216
"x": {
@@ -284,7 +284,7 @@ def __init__(
284284
" invalid."
285285
)
286286
self.logger.warning(msg)
287-
raise TypeError(msg)
287+
raise TypeError(msg) from None
288288
else:
289289
self.x_axis = None
290290
self.y_axis = None

apprise/plugins/discord.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ class NotifyDiscord(NotifyBase):
162162
"url": {
163163
"alias_of": "href",
164164
},
165-
# Send a message to the specified thread within a webhook's channel.
166-
# The thread will automatically be unarchived.
165+
# Send a message to the specified thread within a webhook's
166+
# channel. The thread will automatically be unarchived.
167167
"thread": {
168168
"name": _("Thread ID"),
169169
"type": "string",
@@ -352,7 +352,7 @@ def send(
352352
]
353353

354354
# Remove entry from head of fields
355-
fields = fields[self.discord_max_fields :]
355+
fields = fields[self.discord_max_fields:]
356356

357357
else:
358358
# not markdown
@@ -495,7 +495,13 @@ def _send(self, payload, attach=None, params=None, rate_limit=1, **kwargs):
495495

496496
# Open our attachment path if required:
497497
if attach:
498-
files = {"file": (attach.name, open(attach.path, "rb"))}
498+
files = {
499+
"file": (
500+
attach.name,
501+
# file handle is safely closed in `finally`; inline
502+
# open is intentional
503+
open(attach.path, "rb"), # noqa: SIM115
504+
)}
499505

500506
else:
501507
headers["Content-Type"] = "application/json; charset=utf-8"

apprise/plugins/email/__init__.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,14 @@
4141
charset.add_charset("utf-8", charset.QP, charset.QP, "utf-8")
4242

4343
__all__ = [
44-
# Reference
45-
"NotifyEmail",
46-
# Pretty Good Privacy
44+
"EMAIL_TEMPLATES",
45+
"PGP_SUPPORT",
46+
"SECURE_MODES",
47+
"AppriseEmailException",
4748
"ApprisePGPController",
4849
"ApprisePGPException",
49-
# Other
50-
"AppriseEmailException",
5150
"EmailMessage",
51+
"NotifyEmail",
5252
"SecureMailMode",
53-
"SECURE_MODES",
5453
"WebBaseLogin",
55-
# Additional entries that may be useful to some developers
56-
"EMAIL_TEMPLATES",
57-
"PGP_SUPPORT",
5854
]

apprise/plugins/email/base.py

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from email.utils import formataddr, make_msgid
3535
import re
3636
import smtplib
37+
from typing import Optional
3738

3839
from ...common import NotifyFormat, NotifyType
3940
from ...conversion import convert_between
@@ -229,7 +230,7 @@ def __init__(
229230
super().__init__(**kwargs)
230231

231232
# Acquire Email 'To'
232-
self.targets = list()
233+
self.targets = []
233234

234235
# Acquire Carbon Copies
235236
self.cc = set()
@@ -457,14 +458,13 @@ def apply_email_defaults(self, secure_mode=None, port=None, **kwargs):
457458

458459
for i in range(len(templates.EMAIL_TEMPLATES)): # pragma: no branch
459460
self.logger.trace(
460-
"Scanning %s against %s"
461-
% (from_addr, templates.EMAIL_TEMPLATES[i][0])
462-
)
461+
f"Scanning {from_addr} "
462+
f"against {templates.EMAIL_TEMPLATES[i][0]}")
463463
match = templates.EMAIL_TEMPLATES[i][1].match(from_addr)
464464
if match:
465465
self.logger.info(
466-
"Applying %s Defaults" % templates.EMAIL_TEMPLATES[i][0],
467-
)
466+
f"Applying {templates.EMAIL_TEMPLATES[i][0]} Defaults")
467+
468468
# the secure flag can not be altered if defined in the template
469469
self.secure = templates.EMAIL_TEMPLATES[i][2].get(
470470
"secure", self.secure
@@ -692,7 +692,7 @@ def url(self, privacy=False, *args, **kwargs):
692692
# Handle our Carbon Copy Addresses
693693
params["cc"] = ",".join([
694694
formataddr(
695-
(self.names[e] if e in self.names else False, e),
695+
(self.names.get(e, False), e),
696696
# Swap comma for it's escaped url code (if detected) since
697697
# we're using that as a delimiter
698698
charset="utf-8",
@@ -704,7 +704,7 @@ def url(self, privacy=False, *args, **kwargs):
704704
# Handle our Blind Carbon Copy Addresses
705705
params["bcc"] = ",".join([
706706
formataddr(
707-
(self.names[e] if e in self.names else False, e),
707+
(self.names.get(e, False), e),
708708
# Swap comma for it's escaped url code (if detected) since
709709
# we're using that as a delimiter
710710
charset="utf-8",
@@ -716,8 +716,8 @@ def url(self, privacy=False, *args, **kwargs):
716716
# Handle our Reply-To Addresses
717717
params["reply"] = ",".join([
718718
formataddr(
719-
(self.names[e] if e in self.names else False, e),
720-
# Swap comma for it's escaped url code (if detected) since
719+
(self.names.get(e, False), e),
720+
# Swap comma for its escaped url code (if detected) since
721721
# we're using that as a delimiter
722722
charset="utf-8",
723723
).replace(",", "%2C")
@@ -927,16 +927,16 @@ def prepare_emails(
927927
body,
928928
from_addr,
929929
to,
930-
cc=set(),
931-
bcc=set(),
932-
reply_to=set(),
930+
cc: Optional[set] = None,
931+
bcc: Optional[set] = None,
932+
reply_to: Optional[set] = None,
933933
# Providing an SMTP Host helps improve Email Message-ID
934934
# and avoids getting flagged as spam
935935
smtp_host=None,
936936
# Can be either 'html' or 'text'
937937
notify_format=NotifyFormat.HTML,
938938
attach=None,
939-
headers=dict(),
939+
headers: Optional[dict] = None,
940940
# Names can be a dictionary
941941
names=None,
942942
# Pretty Good Privacy Support; Pass in an
@@ -970,17 +970,28 @@ def prepare_emails(
970970
971971
Pass in an ApprisePGPController() if you wish to use this
972972
"""
973-
974973
if not to:
975974
# There is no one to email; we're done
976975
msg = "There are no Email recipients to notify"
977976
logger.warning(msg)
978-
raise AppriseEmailException(msg)
977+
raise AppriseEmailException(msg) from None
979978

980979
elif pgp and not _pgp.PGP_SUPPORT:
981980
msg = "PGP Support unavailable; install PGPy library"
982981
logger.warning(msg)
983-
raise AppriseEmailException(msg)
982+
raise AppriseEmailException(msg) from None
983+
984+
if headers is None:
985+
headers = {}
986+
987+
if cc is None:
988+
cc = set()
989+
990+
if bcc is None:
991+
bcc = set()
992+
993+
if reply_to is None:
994+
reply_to = set()
984995

985996
if not names:
986997
# Prepare a empty dictionary to prevent errors/warnings
@@ -999,13 +1010,13 @@ def prepare_emails(
9991010
to_name, to_addr = emails.pop(0)
10001011

10011012
# Strip target out of cc list if in To or Bcc
1002-
_cc = cc - bcc - set([to_addr])
1013+
_cc = cc - bcc - {to_addr}
10031014

10041015
# Strip target out of bcc list if in To
1005-
_bcc = bcc - set([to_addr])
1016+
_bcc = bcc - {to_addr}
10061017

10071018
# Strip target out of reply_to list if in To
1008-
_reply_to = reply_to - set([to_addr])
1019+
_reply_to = reply_to - {to_addr}
10091020

10101021
# Format our cc addresses to support the Name field
10111022
_cc = [
@@ -1124,8 +1135,8 @@ def prepare_emails(
11241135
# Store Autocrypt header (DeltaChat Support)
11251136
base.add_header(
11261137
"Autocrypt",
1127-
"addr=%s; prefer-encrypt=mutual"
1128-
% formataddr((False, to_addr), charset="utf-8"),
1138+
f"addr={formataddr((False, to_addr), charset='utf-8')}; "
1139+
"prefer-encrypt=mutual"
11291140
)
11301141

11311142
# Set Encryption Info Part
@@ -1159,6 +1170,6 @@ def prepare_emails(
11591170

11601171
yield EmailMessage(
11611172
recipient=to_addr,
1162-
to_addrs=[to_addr] + list(_cc) + list(_bcc),
1173+
to_addrs=[to_addr, *list(_cc), *list(_bcc)],
11631174
body=base.as_string(),
11641175
)

0 commit comments

Comments
 (0)