Skip to content

Commit

Permalink
refactor: eliminate the code paths for partial notification sending
Browse files Browse the repository at this point in the history
Given the current code, it is actually not possible to construct an Apprise
object such that one server can successfully escape the body and title, but
a subsequent server cannot.
  • Loading branch information
YoRyan committed Jun 23, 2021
1 parent 4aaaf61 commit 4373877
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 70 deletions.
87 changes: 29 additions & 58 deletions apprise/Apprise.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,32 +330,29 @@ def notify(self, body, title='', notify_type=NotifyType.INFO,
)

else:
results, e = Apprise._partiallist(
self._notifyall(
Apprise._notifyhandler,
body, title,
notify_type=notify_type, body_format=body_format,
tag=tag, attach=attach,
interpret_escapes=interpret_escapes,
try:
results = list(
self._notifyall(
Apprise._notifyhandler,
body, title,
notify_type=notify_type, body_format=body_format,
tag=tag, attach=attach,
interpret_escapes=interpret_escapes,
)
)
)

if len(results) > 0:
if e is None:
except TypeError:
# No notifications sent, and there was an internal error.
return False

else:
if len(results) > 0:
# All notifications sent, return False if any failed.
return all(results)

else:
# Some notifications sent, but there was an internal error.
return False

elif e is None:
# No notifications sent.
return None

else:
# No notifications sent, and there was an internal error.
return False
# No notifications sent.
return None

def async_notify(self, *args, **kwargs):
"""
Expand All @@ -368,50 +365,24 @@ def async_notify(self, *args, **kwargs):
is not available in Python 2.
"""

coroutines, e = Apprise._partiallist(
self._notifyall(
Apprise._notifyhandlerasync, *args, **kwargs))

if len(coroutines) > 0:
cor_result = py3compat.asyncio.notify(coroutines, debug=self.debug)

if e is None:
# All notifications sent, return False if any failed.
return cor_result

else:
# Some notifications sent, but there was an internal error.
return py3compat.asyncio.chain(
cor_result,
py3compat.asyncio.toasyncwrap(False)
)

elif e is None:
# No notifications sent.
return py3compat.asyncio.toasyncwrap(None)
try:
coroutines = list(
self._notifyall(
Apprise._notifyhandlerasync, *args, **kwargs))

else:
except TypeError:
# No notifications sent, and there was an internal error.
return py3compat.asyncio.toasyncwrap(False)

@staticmethod
def _partiallist(gen):
"""
Saves the items produced by a generator into a list. If the generator
produces a premature exception, the items produced up until that point
will be returned as well as the exception itself.
"""
else:
if len(coroutines) > 0:
# All notifications sent, return False if any failed.
return py3compat.asyncio.notify(
coroutines, debug=self.debug)

l = []
while True:
try:
x = next(gen)
except StopIteration:
return l, None
except Exception as e:
return l, e
else:
l.append(x)
# No notifications sent.
return py3compat.asyncio.toasyncwrap(None)

@staticmethod
def _notifyhandler(server, **kwargs):
Expand Down
12 changes: 0 additions & 12 deletions apprise/py3compat/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,6 @@ async def toasyncwrap(v): # noqa: E999
return v


async def chain(*cors): # noqa: E999
"""
Chain multiple coroutines into a single sequential coroutine that returns
the value returned by the last coroutine.
"""

v = None
for c in cors:
await c
return v


class AsyncNotifyBase(URLBase):
"""
asyncio wrapper for the NotifyBase object
Expand Down

0 comments on commit 4373877

Please sign in to comment.