44__author__ = 'Simon Robinson'
55__copyright__ = 'Copyright (c) 2022 Simon Robinson'
66__license__ = 'Apache 2.0'
7- __version__ = '2022-09-27 ' # ISO 8601 (YYYY-MM-DD)
7+ __version__ = '2022-10-10 ' # ISO 8601 (YYYY-MM-DD)
88
99import argparse
1010import base64
1111import binascii
1212import configparser
13+ import contextlib
1314import datetime
1415import enum
1516import errno
@@ -181,7 +182,7 @@ def initialise(log_file=None):
181182 Log ._LOGGER = logging .getLogger (APP_NAME )
182183 if log_file or sys .platform == 'win32' :
183184 handler = logging .FileHandler (
184- log_file if log_file else '%s/%s.log' % (os .path .dirname (os .path .realpath (__file__ )), APP_SHORT_NAME ))
185+ log_file or '%s/%s.log' % (os .path .dirname (os .path .realpath (__file__ )), APP_SHORT_NAME ))
185186 handler .setFormatter (logging .Formatter ('%(asctime)s: %(message)s' ))
186187 elif sys .platform == 'darwin' :
187188 if Log ._MACOS_USE_SYSLOG : # syslog prior to 10.12
@@ -513,10 +514,8 @@ def __call__(self, environ, start_response):
513514 Log .info ('Please visit the following URL to authenticate account %s: %s' %
514515 (token_request ['username' ], token_request ['permission_url' ]))
515516 redirection_server .handle_request ()
516- try :
517+ with contextlib . suppress ( socket . error ) :
517518 redirection_server .server_close ()
518- except socket .error :
519- pass
520519
521520 if 'response_url' in token_request :
522521 Log .debug ('Local server auth mode (%s:%d): closing local server and returning response' % (
@@ -893,16 +892,12 @@ def handle_close(self):
893892 def close (self ):
894893 if self .server_connection :
895894 self .server_connection .client_connection = None
896- try :
895+ with contextlib . suppress ( AttributeError ) :
897896 self .server_connection .close ()
898- except AttributeError :
899- pass
900897 self .server_connection = None
901898 self .proxy_parent .remove_client (self )
902- try :
899+ with contextlib . suppress ( OSError ) :
903900 super ().close ()
904- except OSError :
905- pass
906901
907902
908903class IMAPOAuth2ClientConnection (OAuth2ClientConnection ):
@@ -1261,15 +1256,11 @@ def handle_close(self):
12611256 def close (self ):
12621257 if self .client_connection :
12631258 self .client_connection .server_connection = None
1264- try :
1259+ with contextlib . suppress ( AttributeError ) :
12651260 self .client_connection .close ()
1266- except AttributeError :
1267- pass
12681261 self .client_connection = None
1269- try :
1262+ with contextlib . suppress ( OSError ) :
12701263 super ().close ()
1271- except OSError :
1272- pass
12731264
12741265
12751266class IMAPOAuth2ServerConnection (OAuth2ServerConnection ):
@@ -1525,7 +1516,7 @@ def handle_accepted(self, connection, address):
15251516 new_server_connection .client_connection = new_client_connection
15261517 self .client_connections .append (new_client_connection )
15271518
1528- threading .Thread (target = self .run_server , args = (new_client_connection , socket_map , address ),
1519+ threading .Thread (target = OAuth2Proxy .run_server , args = (new_client_connection , socket_map , address ),
15291520 name = 'EmailOAuth2Proxy-connection-%d' % address [1 ], daemon = True ).start ()
15301521
15311522 except ssl .SSLError :
@@ -1599,8 +1590,7 @@ def bye_message(self, error_text=None):
15991590 return '+OK Server signing off' if error_text is None else ('-ERR %s' % error_text )
16001591 elif self .proxy_type == 'SMTP' :
16011592 return '221 %s' % ('2.0.0 Service closing transmission channel' if error_text is None else error_text )
1602- else :
1603- return ''
1593+ return ''
16041594
16051595 def close_clients (self ):
16061596 for connection in self .client_connections [:]: # iterate over a copy; remove (in close()) from original
@@ -1736,6 +1726,7 @@ def __init__(self):
17361726 'Log.initialise() for details)' )
17371727 parser .add_argument ('--debug' , action = 'store_true' , help = 'enable debug mode, printing client<->proxy<->server '
17381728 'interaction to the system log' )
1729+ parser .add_argument ('--version' , action = 'version' , version = '%s %s' % (APP_NAME , __version__ ))
17391730
17401731 self .args = parser .parse_args ()
17411732
@@ -2165,8 +2156,7 @@ def get_script_start_command(self):
21652156 if self .args .local_server_auth :
21662157 script_command .append ('--local-server-auth' )
21672158 if self .args .config_file :
2168- script_command .append ('--config-file' )
2169- script_command .append (CONFIG_FILE_PATH )
2159+ script_command .extend (['--config-file' , CONFIG_FILE_PATH ])
21702160
21712161 return script_command
21722162
@@ -2178,7 +2168,7 @@ def linux_restart(self, icon):
21782168 shell = True ))
21792169
21802170 @staticmethod
2181- def macos_launchctl (command = 'list' ):
2171+ def macos_launchctl (command ):
21822172 # this used to use the python launchctl package, but it has a bug (github.com/andrewp-as-is/values.py/pull/2)
21832173 # in a sub-package, so we reproduce just the core features - supported commands are 'list', 'load' and 'unload'
21842174 proxy_command = APP_PACKAGE if command == 'list' else PLIST_FILE_PATH
@@ -2252,11 +2242,8 @@ def stop_servers(self):
22522242 except queue .Empty :
22532243 break
22542244 for proxy in self .proxies :
2255- # noinspection PyBroadException
2256- try :
2245+ with contextlib .suppress (Exception ):
22572246 proxy .stop ()
2258- except Exception :
2259- pass
22602247 self .proxies = []
22612248 self .authorisation_requests = [] # these requests are no-longer valid
22622249
@@ -2324,7 +2311,7 @@ def load_and_start_servers(self, icon=None, reload=True):
23242311 if icon :
23252312 icon .update_menu () # force refresh the menu to show running proxy servers
23262313
2327- threading .Thread (target = self .run_proxy , name = 'EmailOAuth2Proxy-main' , daemon = True ).start ()
2314+ threading .Thread (target = App .run_proxy , name = 'EmailOAuth2Proxy-main' , daemon = True ).start ()
23282315 return True
23292316
23302317 def post_create (self , icon ):
@@ -2399,11 +2386,8 @@ def exit(self, icon, restart_callback=None):
23992386 window .destroy ()
24002387
24012388 for proxy in self .proxies : # no need to copy - proxies are never removed, we just restart them on error
2402- # noinspection PyBroadException
2403- try :
2389+ with contextlib .suppress (Exception ):
24042390 proxy .stop ()
2405- except Exception :
2406- pass
24072391
24082392 if icon :
24092393 icon .stop ()
0 commit comments