Skip to content

Commit

Permalink
Remove most Python 2 specific code
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Oct 7, 2022
1 parent cbd1dc7 commit 0a6558f
Show file tree
Hide file tree
Showing 135 changed files with 335 additions and 2,074 deletions.
108 changes: 19 additions & 89 deletions apprise/Apprise.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@
from . import plugins
from . import __version__

# Python v3+ support code made importable so it can remain backwards
# Python v3+ support code made importable, so it can remain backwards
# compatible with Python v2
from . import py3compat
ASYNCIO_SUPPORT = not six.PY2


class Apprise(object):
class Apprise:
"""
Our Notification Manager
Expand Down Expand Up @@ -370,41 +369,15 @@ def notify(self, body, title='', notify_type=common.NotifyType.INFO,
such as turning a \n into an actual new line, etc.
"""

if ASYNCIO_SUPPORT:
return py3compat.asyncio.tosync(
self.async_notify(
body, title,
notify_type=notify_type, body_format=body_format,
tag=tag, match_always=match_always, attach=attach,
interpret_escapes=interpret_escapes,
),
debug=self.debug
)

else:
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,
)
)

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:
# No notifications sent.
return None
return py3compat.asyncio.tosync(
self.async_notify(
body, title,
notify_type=notify_type, body_format=body_format,
tag=tag, match_always=match_always, attach=attach,
interpret_escapes=interpret_escapes,
),
debug=self.debug
)

def async_notify(self, *args, **kwargs):
"""
Expand All @@ -413,8 +386,7 @@ def async_notify(self, *args, **kwargs):
awaited on, even if it is missing the async keyword in its signature.
(This is omitted to preserve syntax compatibility with Python 2.)
The arguments are identical to those of Apprise.notify(). This method
is not available in Python 2.
The arguments are identical to those of Apprise.notify().
"""

try:
Expand Down Expand Up @@ -496,23 +468,11 @@ def _notifyall(self, handler, body, title='',
raise TypeError(msg)

try:
if six.PY2:
# Python 2.7 encoding support isn't the greatest, so we try
# to ensure that we're ALWAYS dealing with unicode characters
# prior to entrying the next part. This is especially required
# for Markdown support
if title and isinstance(title, str): # noqa: F821
title = title.decode(self.asset.encoding)

if body and isinstance(body, str): # noqa: F821
body = body.decode(self.asset.encoding)
if title and isinstance(title, bytes): # noqa: F821
title = title.decode(self.asset.encoding)

else: # Python 3+
if title and isinstance(title, bytes): # noqa: F821
title = title.decode(self.asset.encoding)

if body and isinstance(body, bytes): # noqa: F821
body = body.decode(self.asset.encoding)
if body and isinstance(body, bytes): # noqa: F821
body = body.decode(self.asset.encoding)

except UnicodeDecodeError:
msg = 'The content passed into Apprise was not of encoding ' \
Expand Down Expand Up @@ -581,7 +541,7 @@ def _notifyall(self, handler, body, title='',
.decode('unicode-escape')

except UnicodeDecodeError: # pragma: no cover
# This occurs using a very old verion of Python 2.7
# This occurs using a very old version of Python 2.7
# such as the one that ships with CentOS/RedHat 7.x
# (v2.7.5).
conversion_body_map[server.notify_format] = \
Expand All @@ -598,25 +558,6 @@ def _notifyall(self, handler, body, title='',
logger.error(msg)
raise TypeError(msg)

if six.PY2:
# Python 2.7 strings must be encoded as utf-8 for
# consistency across all platforms
if conversion_body_map[server.notify_format] and \
isinstance(
conversion_body_map[server.notify_format],
unicode): # noqa: F821
conversion_body_map[server.notify_format] = \
conversion_body_map[server.notify_format]\
.encode('utf-8')

if conversion_title_map[server.notify_format] and \
isinstance(
conversion_title_map[server.notify_format],
unicode): # noqa: F821
conversion_title_map[server.notify_format] = \
conversion_title_map[server.notify_format]\
.encode('utf-8')

yield handler(
server,
body=conversion_body_map[server.notify_format],
Expand Down Expand Up @@ -779,15 +720,8 @@ def __getitem__(self, index):

def __bool__(self):
"""
Allows the Apprise object to be wrapped in an Python 3.x based 'if
statement'. True is returned if at least one service has been loaded.
"""
return len(self) > 0

def __nonzero__(self):
"""
Allows the Apprise object to be wrapped in an Python 2.x based 'if
statement'. True is returned if at least one service has been loaded.
Allows the Apprise object to be wrapped in an 'if statement'.
True is returned if at least one service has been loaded.
"""
return len(self) > 0

Expand All @@ -807,7 +741,3 @@ def __len__(self):
"""
return sum([1 if not isinstance(s, (ConfigBase, AppriseConfig))
else len(s.servers()) for s in self.servers])


if six.PY2:
del Apprise.async_notify
1 change: 0 additions & 1 deletion apprise/Apprise.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,5 @@ class Apprise:
def pop(self, index: int) -> ConfigBase: ...
def __getitem__(self, index: int) -> ConfigBase: ...
def __bool__(self) -> bool: ...
def __nonzero__(self) -> bool: ...
def __iter__(self) -> Iterator[ConfigBase]: ...
def __len__(self) -> int: ...
2 changes: 1 addition & 1 deletion apprise/AppriseAsset.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from .utils import module_detection


class AppriseAsset(object):
class AppriseAsset:
"""
Provides a supplimentary class that can be used to provide extra
information and details that can be used by Apprise such as providing
Expand Down
13 changes: 3 additions & 10 deletions apprise/AppriseAttachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from .utils import GET_SCHEMA_RE


class AppriseAttachment(object):
class AppriseAttachment:
"""
Our Apprise Attachment File Manager
Expand Down Expand Up @@ -296,15 +296,8 @@ def __getitem__(self, index):

def __bool__(self):
"""
Allows the Apprise object to be wrapped in an Python 3.x based 'if
statement'. True is returned if at least one service has been loaded.
"""
return True if self.attachments else False

def __nonzero__(self):
"""
Allows the Apprise object to be wrapped in an Python 2.x based 'if
statement'. True is returned if at least one service has been loaded.
Allows the Apprise object to be wrapped in an 'if statement'.
True is returned if at least one service has been loaded.
"""
return True if self.attachments else False

Expand Down
1 change: 0 additions & 1 deletion apprise/AppriseAttachment.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,5 @@ class AppriseAttachment:
def pop(self, index: int = ...) -> AttachBase: ...
def __getitem__(self, index: int) -> AttachBase: ...
def __bool__(self) -> bool: ...
def __nonzero__(self) -> bool: ...
def __iter__(self) -> Iterator[AttachBase]: ...
def __len__(self) -> int: ...
13 changes: 3 additions & 10 deletions apprise/AppriseConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from .logger import logger


class AppriseConfig(object):
class AppriseConfig:
"""
Our Apprise Configuration File Manager
Expand Down Expand Up @@ -432,15 +432,8 @@ def __getitem__(self, index):

def __bool__(self):
"""
Allows the Apprise object to be wrapped in an Python 3.x based 'if
statement'. True is returned if at least one service has been loaded.
"""
return True if self.configs else False

def __nonzero__(self):
"""
Allows the Apprise object to be wrapped in an Python 2.x based 'if
statement'. True is returned if at least one service has been loaded.
Allows the Apprise object to be wrapped in an 'if statement'.
True is returned if at least one service has been loaded.
"""
return True if self.configs else False

Expand Down
1 change: 0 additions & 1 deletion apprise/AppriseConfig.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,5 @@ class AppriseConfig:
def pop(self, index: int = ...) -> ConfigBase: ...
def __getitem__(self, index: int) -> ConfigBase: ...
def __bool__(self) -> bool: ...
def __nonzero__(self) -> bool: ...
def __iter__(self) -> Iterator[ConfigBase]: ...
def __len__(self) -> int: ...
15 changes: 4 additions & 11 deletions apprise/AppriseLocale.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,11 @@
except ImportError:
# gettext isn't available; no problem, just fall back to using
# the library features without multi-language support.
try:
# Python v2.7
import __builtin__
__builtin__.__dict__['_'] = lambda x: x # pragma: no branch
import builtins
builtins.__dict__['_'] = lambda x: x # pragma: no branch

except ImportError:
# Python v3.4+
import builtins
builtins.__dict__['_'] = lambda x: x # pragma: no branch


class LazyTranslation(object):
class LazyTranslation:
"""
Doesn't translate anything until str() or unicode() references
are made.
Expand All @@ -89,7 +82,7 @@ def gettext_lazy(text):
return LazyTranslation(text=text)


class AppriseLocale(object):
class AppriseLocale:
"""
A wrapper class to gettext so that we can manipulate multiple lanaguages
on the fly if required.
Expand Down
36 changes: 6 additions & 30 deletions apprise/URLBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,8 @@
from datetime import datetime
from xml.sax.saxutils import escape as sax_escape

try:
# Python 2.7
from urllib import unquote as _unquote
from urllib import quote as _quote

except ImportError:
# Python 3.x
from urllib.parse import unquote as _unquote
from urllib.parse import quote as _quote
from urllib.parse import unquote as _unquote
from urllib.parse import quote as _quote

from .AppriseLocale import gettext_lazy as _
from .AppriseAsset import AppriseAsset
Expand All @@ -52,7 +45,7 @@
PATHSPLIT_LIST_DELIM = re.compile(r'[ \t\r\n,\\/]+')


class PrivacyMode(object):
class PrivacyMode:
# Defines different privacy modes strings can be printed as
# Astrisk sets 4 of them: e.g. ****
# This is used for passwords
Expand All @@ -77,7 +70,7 @@ class PrivacyMode(object):
}


class URLBase(object):
class URLBase:
"""
This is the base class for all URL Manipulation
"""
Expand Down Expand Up @@ -388,13 +381,7 @@ def unquote(content, encoding='utf-8', errors='replace'):
if not content:
return ''

try:
# Python v3.x
return _unquote(content, encoding=encoding, errors=errors)

except TypeError:
# Python v2.7
return _unquote(content)
return _unquote(content, encoding=encoding, errors=errors)

@staticmethod
def quote(content, safe='/', encoding=None, errors=None):
Expand All @@ -421,13 +408,7 @@ def quote(content, safe='/', encoding=None, errors=None):
if not content:
return ''

try:
# Python v3.x
return _quote(content, safe=safe, encoding=encoding, errors=errors)

except TypeError:
# Python v2.7
return _quote(content, safe=safe)
return _quote(content, safe=safe, encoding=encoding, errors=errors)

@staticmethod
def pprint(content, privacy=True, mode=PrivacyMode.Outer,
Expand Down Expand Up @@ -575,11 +556,6 @@ def parse_phone_no(content, unquote=True):
# Nothing further to do
return []

except AttributeError:
# This exception ONLY gets thrown under Python v2.7 if an
# object() is passed in place of the content
return []

content = parse_phone_no(content)

return content
Expand Down
11 changes: 2 additions & 9 deletions apprise/attachment/AttachBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,7 @@ def __len__(self):

def __bool__(self):
"""
Allows the Apprise object to be wrapped in an Python 3.x based 'if
statement'. True is returned if our content was downloaded correctly.
"""
return True if self.path else False

def __nonzero__(self):
"""
Allows the Apprise object to be wrapped in an Python 2.x based 'if
statement'. True is returned if our content was downloaded correctly.
Allows the Apprise object to be wrapped in an based 'if statement'.
True is returned if our content was downloaded correctly.
"""
return True if self.path else False
1 change: 0 additions & 1 deletion apprise/attachment/AttachBase.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ class AttachBase:
) -> Dict[str, Any]: ...
def __len__(self) -> int: ...
def __bool__(self) -> bool: ...
def __nonzero__(self) -> bool: ...
Loading

0 comments on commit 0a6558f

Please sign in to comment.