2121
2222def setup_logging (log_config : LogConfig ) -> None :
2323 # Configure root logger if logging level is specified in config
24- if log_config [' level' ]:
25- level = getattr (logging , log_config [' level' ])
26- formatter = logging .Formatter (log_config [' format' ])
24+ if log_config [" level" ]:
25+ level = getattr (logging , log_config [" level" ])
26+ formatter = logging .Formatter (log_config [" format" ])
2727 sh : logging .StreamHandler [Any ] = logging .StreamHandler ()
2828 sh .setFormatter (formatter )
2929
3030 logger = logging .getLogger ()
3131 logger .setLevel (level )
3232 logger .addHandler (sh )
3333
34- trace_level = getattr (logging , log_config [' trace_level' ])
35- trace_logger = logging .getLogger (log_config [' trace_logger_prefix' ])
34+ trace_level = getattr (logging , log_config [" trace_level" ])
35+ trace_logger = logging .getLogger (log_config [" trace_logger_prefix" ])
3636 trace_logger .setLevel (trace_level )
3737
38- if log_config [' setup_structlog' ]:
38+ if log_config [" setup_structlog" ]:
3939 setup_structlog (sys .stdout .isatty ())
4040
4141
@@ -44,7 +44,7 @@ def setup_structlog(tty: bool = False) -> None:
4444 structlog .stdlib .filter_by_level ,
4545 structlog .stdlib .add_log_level ,
4646 structlog .stdlib .add_logger_name ,
47- structlog .processors .TimeStamper (fmt = ' iso' , utc = True ),
47+ structlog .processors .TimeStamper (fmt = " iso" , utc = True ),
4848 structlog .processors .StackInfoRenderer (),
4949 structlog .processors .format_exc_info ,
5050 ]
@@ -66,8 +66,8 @@ def load_backend(config: Config) -> Any:
6666 """
6767 Return the backend module from the given SocketShark configuration.
6868 """
69- backend_name = config .get (' BACKEND' , ' websockets' )
70- backend_module = f' socketshark.backend.{ backend_name } '
69+ backend_name = config .get (" BACKEND" , " websockets" )
70+ backend_module = f" socketshark.backend.{ backend_name } "
7171 return importlib .import_module (backend_module )
7272
7373
@@ -87,35 +87,35 @@ def __init__(self, config: Config) -> None:
8787 self .redis_connections : list [RedisConnection ] = []
8888
8989 def _init_logging (self ) -> None :
90- logger_name = self .config [' LOG' ][ ' logger_name' ]
91- trace_logger_prefix = self .config [' LOG' ][ ' trace_logger_prefix' ]
92- trace_logger_name = f' { trace_logger_prefix } .{ logger_name } '
90+ logger_name = self .config [" LOG" ][ " logger_name" ]
91+ trace_logger_prefix = self .config [" LOG" ][ " trace_logger_prefix" ]
92+ trace_logger_name = f" { trace_logger_prefix } .{ logger_name } "
9393 pid = os .getpid ()
9494 self .log : structlog .stdlib .BoundLogger = structlog .get_logger (
9595 logger_name
9696 ).bind (pid = pid )
9797 self .trace_log : structlog .stdlib .BoundLogger = structlog .get_logger (
9898 trace_logger_name
9999 ).bind (pid = pid )
100- self .trace_log .debug (' trace' )
100+ self .trace_log .debug (" trace" )
101101
102102 def signal_ready (self ) -> None :
103103 """
104104 Notify that the backend is ready.
105105 """
106106 self .log .info (
107- ' 🦈 ready' ,
108- host = self .config [' WS_HOST' ],
109- port = self .config [' WS_PORT' ],
110- secure = bool (self .config .get (' WS_SSL' )),
107+ " 🦈 ready" ,
108+ host = self .config [" WS_HOST" ],
109+ port = self .config [" WS_PORT" ],
110+ secure = bool (self .config .get (" WS_SSL" )),
111111 )
112112 self .metrics .set_ready (True )
113113
114114 def signal_shutdown (self ) -> None :
115115 """
116116 Notify that the backend shut down.
117117 """
118- self .log .info (' done' )
118+ self .log .info (" done" )
119119 self .metrics .set_ready (False )
120120
121121 async def _redis_connection_handler (self ) -> None :
@@ -132,7 +132,7 @@ async def _redis_connection_handler(self) -> None:
132132 ]
133133 await asyncio .wait (tasks , return_when = asyncio .FIRST_COMPLETED )
134134
135- self .log .error (' redis unexpectedly closed' )
135+ self .log .error (" redis unexpectedly closed" )
136136 self .metrics .set_ready (False )
137137
138138 # Since we rely on PUBSUB channels, we disconnect all clients when
@@ -145,15 +145,15 @@ async def prepare(self) -> None:
145145
146146 Initialize Redis connection and the receiver class.
147147 """
148- redis_settings = [self .config [' REDIS' ]]
149- if self .config .get (' REDIS_ALT' ):
150- redis_settings .append (self .config [' REDIS_ALT' ])
148+ redis_settings = [self .config [" REDIS" ]]
149+ if self .config .get (" REDIS_ALT" ):
150+ redis_settings .append (self .config [" REDIS_ALT" ])
151151 try :
152152 self .redis_connections = await asyncio .gather (
153153 * [RedisConnection .create (s ) for s in redis_settings ]
154154 )
155155 except (OSError , aioredis .RedisError ):
156- self .log .exception (' could not connect to redis' )
156+ self .log .exception (" could not connect to redis" )
157157 raise
158158
159159 self ._redis_connection_handler_task = asyncio .ensure_future (
@@ -174,7 +174,7 @@ async def shutdown(self) -> None:
174174 if self ._shutdown :
175175 return
176176
177- self .log .info (' shutting down' )
177+ self .log .info (" shutting down" )
178178
179179 self ._shutdown = True
180180
@@ -192,7 +192,7 @@ async def shutdown(self) -> None:
192192 # Wait for all sessions to close
193193 while self .sessions :
194194 self .log .info (
195- ' waiting for sessions to close' , n_sessions = len (self .sessions )
195+ " waiting for sessions to close" , n_sessions = len (self .sessions )
196196 )
197197 await asyncio .sleep (1 )
198198
@@ -238,7 +238,7 @@ def _install_signal_handlers(self) -> None:
238238 """
239239
240240 def request_stop () -> None :
241- self .log .info (' stop requested' )
241+ self .log .info (" stop requested" )
242242 asyncio .ensure_future (self .shutdown ())
243243
244244 loop = asyncio .get_event_loop ()
@@ -254,11 +254,11 @@ def _uninstall_signal_handlers(self) -> None:
254254 loop .remove_signal_handler (signal .SIGTERM )
255255
256256 def get_ssl_context (self ) -> ssl .SSLContext | None :
257- ssl_settings = self .config .get (' WS_SSL' )
257+ ssl_settings = self .config .get (" WS_SSL" )
258258 if ssl_settings :
259259 ssl_context = ssl .create_default_context (ssl .Purpose .CLIENT_AUTH )
260260 ssl_context .load_cert_chain (
261- certfile = ssl_settings [' cert' ], keyfile = ssl_settings [' key' ]
261+ certfile = ssl_settings [" cert" ], keyfile = ssl_settings [" key" ]
262262 )
263263 return ssl_context
264264 return None
@@ -286,16 +286,16 @@ def load_config(config_name: str) -> Config:
286286
287287
288288@click .command ()
289- @click .option ('-c' , ' --config' , required = True , help = ' dotted path to config' )
289+ @click .option ("-c" , " --config" , required = True , help = " dotted path to config" )
290290@click .pass_context
291291def run (context : click .Context , config : str ) -> None :
292292 config_obj = load_config (config )
293293
294- setup_logging (config_obj [' LOG' ])
294+ setup_logging (config_obj [" LOG" ])
295295
296296 shark = SocketShark (config_obj )
297297 try :
298298 shark .start ()
299299 except Exception :
300- shark .log .exception (' unhandled exception' )
300+ shark .log .exception (" unhandled exception" )
301301 raise
0 commit comments