Skip to content

Commit

Permalink
Switch to packaging and importlib_metadata (fixes Nuitka build)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrob committed Jul 29, 2024
1 parent 4e1dfd8 commit 50d07c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
41 changes: 22 additions & 19 deletions emailproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
__author__ = 'Simon Robinson'
__copyright__ = 'Copyright (c) 2024 Simon Robinson'
__license__ = 'Apache 2.0'
__version__ = '2024-07-08' # ISO 8601 (YYYY-MM-DD)
__version__ = '2024-07-29' # ISO 8601 (YYYY-MM-DD)
__package_version__ = '.'.join([str(int(i)) for i in __version__.split('-')]) # for pyproject.toml usage only

import abc
Expand Down Expand Up @@ -99,14 +99,22 @@ class Icon:
except ImportError as gui_requirement_import_error:
MISSING_GUI_REQUIREMENTS.append(gui_requirement_import_error)

with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
try:
# pylint: disable-next=ungrouped-imports
import importlib.metadata as importlib_metadata # get package version numbers - available in stdlib from python 3.8
except ImportError:
try:
# noinspection PyDeprecation,PyUnresolvedReferences
import pkg_resources # from setuptools - to change to importlib.metadata and packaging.version once min. is 3.8
# noinspection PyUnresolvedReferences
import importlib_metadata
except ImportError as gui_requirement_import_error:
MISSING_GUI_REQUIREMENTS.append(gui_requirement_import_error)

try:
# noinspection PyUnresolvedReferences
import packaging.version # parse package version numbers - used to work around various GUI-only package issues
except ImportError as gui_requirement_import_error:
MISSING_GUI_REQUIREMENTS.append(gui_requirement_import_error)

# for macOS-specific functionality
if sys.platform == 'darwin':
try:
Expand Down Expand Up @@ -2696,13 +2704,12 @@ def macos_nsworkspace_notification_listener_(self, notification):
# noinspection PyDeprecation
def create_icon(self):
# fix pystray <= 0.19.4 incompatibility with PIL 10.0.0+; resolved in 0.19.5 and later via pystray PR #147
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
pystray_version = pkg_resources.get_distribution('pystray').version
pillow_version = pkg_resources.get_distribution('pillow').version
if pkg_resources.parse_version(pystray_version) <= pkg_resources.parse_version('0.19.4') and \
pkg_resources.parse_version(pillow_version) >= pkg_resources.parse_version('10.0.0'):
Image.ANTIALIAS = Image.LANCZOS if hasattr(Image, 'LANCZOS') else Image.Resampling.LANCZOS
pystray_version = packaging.version.Version(importlib_metadata.version('pystray'))
pillow_version = packaging.version.Version(importlib_metadata.version('pillow'))
if pystray_version <= packaging.version.Version('0.19.4') and \
pillow_version >= packaging.version.Version('10.0.0'):
Image.ANTIALIAS = Image.LANCZOS if hasattr(Image, 'LANCZOS') else Image.Resampling.LANCZOS

icon_class = RetinaIcon if sys.platform == 'darwin' else pystray.Icon
return icon_class(APP_NAME, App.get_image(), APP_NAME, menu=pystray.Menu(
pystray.MenuItem('Servers and accounts', pystray.Menu(self.create_config_menu)),
Expand Down Expand Up @@ -2763,9 +2770,7 @@ def get_icon_size(text, font_size):
font = ImageFont.truetype(io.BytesIO(zlib.decompress(base64.b64decode(APP_ICON))), size=font_size)

# pillow's getsize method was deprecated in 9.2.0 (see docs for PIL.ImageFont.ImageFont.getsize)
# noinspection PyDeprecation
if pkg_resources.parse_version(
pkg_resources.get_distribution('pillow').version) < pkg_resources.parse_version('9.2.0'):
if packaging.version.Version(importlib_metadata.version('pillow')) < packaging.version.Version('9.2.0'):
font_width, font_height = font.getsize(text)
return font, font_width, font_height

Expand Down Expand Up @@ -2906,11 +2911,9 @@ def create_authorisation_window(self, request):
setattr(authorisation_window, 'get_title', lambda window: window.title) # add missing get_title method

# pywebview 3.6+ moved window events to a separate namespace in a non-backwards-compatible way
# noinspection PyDeprecation
pywebview_version = pkg_resources.parse_version(pkg_resources.get_distribution('pywebview').version)
pywebview_version = packaging.version.Version(importlib_metadata.version('pywebview'))
# the version zero check is due to a bug in the Ubuntu 24.04 python-pywebview package - see GitHub #242
# noinspection PyDeprecation
if pkg_resources.parse_version('0') < pywebview_version < pkg_resources.parse_version('3.6'):
if packaging.version.Version('0') < pywebview_version < packaging.version.Version('3.6'):
# noinspection PyUnresolvedReferences
authorisation_window.loaded += self.authorisation_window_loaded
else:
Expand Down
3 changes: 2 additions & 1 deletion requirements-gui.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# the standard way to install the proxy and dependencies is `python -m pip install emailproxy` (i.e., direct from PyPI)
# to install requirements directly, use: `python -m pip install -r requirements-core.txt -r requirements-gui.txt`

importlib_metadata; python_version < '3.8' # to get dependency versions (available in stdlib from 3.8 onwards)
packaging # for dependency version comparisons
pillow # to create the menu bar icon image from a TTF icon
setuptools # for pkg_resources (checking dependency versions)
timeago # for displaying the last authenticated activity hint

# force pystray version with dummy GUI fix for headless deployments (https://github.com/moses-palmer/pystray/issues/118)
Expand Down

0 comments on commit 50d07c9

Please sign in to comment.