Skip to content

Commit ad2c722

Browse files
committed
Partially restore previous behaviour for pystray import
This change is to work around an issue where a broken pystray installation can cause the proxy to fail to start, even in `--no-gui` mode. Closes #204. This change also fixes an incorrect import (the existing behaviour only worked because pystray includes the correct import)
1 parent 1bf68f2 commit ad2c722

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

emailproxy.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
__author__ = 'Simon Robinson'
77
__copyright__ = 'Copyright (c) 2023 Simon Robinson'
88
__license__ = 'Apache 2.0'
9-
__version__ = '2023-11-02' # ISO 8601 (YYYY-MM-DD)
9+
__version__ = '2023-11-06' # ISO 8601 (YYYY-MM-DD)
1010
__package_version__ = '.'.join([str(int(i)) for i in __version__.split('-')]) # for pyproject.toml usage only
1111

1212
import abc
@@ -60,18 +60,25 @@
6060
# where not having to install GUI-only requirements can be helpful - see the proxy's readme (the `--no-gui` option)
6161
MISSING_GUI_REQUIREMENTS = []
6262

63-
try:
64-
import pystray # the menu bar/taskbar GUI
65-
except ImportError as gui_requirement_import_error:
66-
MISSING_GUI_REQUIREMENTS.append(gui_requirement_import_error)
67-
63+
no_gui_parser = argparse.ArgumentParser(add_help=False)
64+
no_gui_parser.add_argument('--no-gui', action='store_false', dest='gui')
65+
no_gui_args = no_gui_parser.parse_known_args()[0]
66+
if no_gui_args.gui:
67+
try:
68+
import pystray # the menu bar/taskbar GUI
69+
except Exception as gui_requirement_import_error: # see #204 - incomplete pystray installation can throw exceptions
70+
MISSING_GUI_REQUIREMENTS.append(gui_requirement_import_error)
71+
no_gui_args.gui = False # we need the dummy implementation
6872

73+
if not no_gui_args.gui:
6974
class DummyPystray: # dummy implementation allows initialisation to complete
7075
class Icon:
7176
pass
7277

7378

7479
pystray = DummyPystray # this is just to avoid unignorable IntelliJ warnings about naming and spacing
80+
del no_gui_parser
81+
del no_gui_args
7582

7683
try:
7784
# noinspection PyUnresolvedReferences
@@ -104,7 +111,7 @@ class Icon:
104111
try:
105112
# PyUnresolvedReferences; see: youtrack.jetbrains.com/issue/PY-11963 (same for others with this suppression)
106113
# noinspection PyPackageRequirements,PyUnresolvedReferences
107-
import PyObjCTools # SIGTERM handling (only needed when in GUI mode; `signal` is sufficient otherwise)
114+
import PyObjCTools.MachSignals # SIGTERM handling (only needed in GUI mode; `signal` is sufficient otherwise)
108115
except ImportError as gui_requirement_import_error:
109116
MISSING_GUI_REQUIREMENTS.append(gui_requirement_import_error)
110117

0 commit comments

Comments
 (0)