@@ -106,7 +106,9 @@ def publish_event(self, event: NotificationEvent) -> None:
106106 self .stats .last_activity = datetime .now ()
107107 self ._event_history .append (event )
108108
109- logger .debug (f"Publishing event: { event .event_type } from { event .source_component } " )
109+ logger .debug (
110+ f"Publishing event: { event .event_type } from { event .source_component } "
111+ )
110112
111113 # Find and notify subscribers
112114 matching_subscribers = self ._find_matching_subscribers (event )
@@ -139,13 +141,13 @@ def subscribe(self, subscriber: EventSubscriber) -> str:
139141
140142 with self ._lock :
141143 self ._subscriptions [subscription_id ] = SubscriptionRecord (
142- subscription_id = subscription_id ,
143- subscriber = subscriber ,
144- filters = filters
144+ subscription_id = subscription_id , subscriber = subscriber , filters = filters
145145 )
146146 self .stats .active_subscriptions = len (self ._subscriptions )
147147
148- logger .info (f"Registered subscriber { subscriber .get_subscriber_id ()} with ID { subscription_id } " )
148+ logger .info (
149+ f"Registered subscriber { subscriber .get_subscriber_id ()} with ID { subscription_id } "
150+ )
149151 return subscription_id
150152
151153 def unsubscribe (self , subscription_id : str ) -> bool :
@@ -160,13 +162,19 @@ def unsubscribe(self, subscription_id: str) -> bool:
160162 """
161163 with self ._lock :
162164 if subscription_id in self ._subscriptions :
163- subscriber_id = self ._subscriptions [subscription_id ].subscriber .get_subscriber_id ()
165+ subscriber_id = self ._subscriptions [
166+ subscription_id
167+ ].subscriber .get_subscriber_id ()
164168 del self ._subscriptions [subscription_id ]
165169 self .stats .active_subscriptions = len (self ._subscriptions )
166- logger .info (f"Unsubscribed subscriber { subscriber_id } ({ subscription_id } )" )
170+ logger .info (
171+ f"Unsubscribed subscriber { subscriber_id } ({ subscription_id } )"
172+ )
167173 return True
168174
169- logger .warning (f"Attempted to unsubscribe unknown subscription ID: { subscription_id } " )
175+ logger .warning (
176+ f"Attempted to unsubscribe unknown subscription ID: { subscription_id } "
177+ )
170178 return False
171179
172180 def get_active_subscriptions (self ) -> list [str ]:
@@ -192,7 +200,7 @@ def report_status(
192200 "component_id" : component_id ,
193201 "metadata" : metadata or {},
194202 "timestamp" : datetime .now (),
195- "record_id" : str (uuid .uuid4 ())
203+ "record_id" : str (uuid .uuid4 ()),
196204 }
197205
198206 with self ._lock :
@@ -211,7 +219,7 @@ def report_status(
211219 timestamp = status_record ["timestamp" ],
212220 source_component = component_id ,
213221 metadata = status_record ["metadata" ],
214- channels = [NotificationChannel .LOG ]
222+ channels = [NotificationChannel .LOG ],
215223 )
216224
217225 self .publish_event (event )
@@ -236,13 +244,13 @@ def report_progress(
236244 "progress" : min (max (progress , 0.0 ), 1.0 ), # Clamp to [0.0, 1.0]
237245 "operation" : operation ,
238246 "details" : details or "" ,
239- "percentage" : f"{ progress * 100 :.1f} %"
247+ "percentage" : f"{ progress * 100 :.1f} %" ,
240248 }
241249
242250 self .report_status (
243251 f"Progress: { progress_metadata ['percentage' ]} - { operation } " ,
244252 component_id ,
245- progress_metadata
253+ progress_metadata ,
246254 )
247255
248256 def report_completion (
@@ -282,7 +290,7 @@ def report_completion(
282290 timestamp = datetime .now (),
283291 source_component = component_id ,
284292 metadata = completion_metadata ,
285- channels = [NotificationChannel .LOG , NotificationChannel .CONSOLE ]
293+ channels = [NotificationChannel .LOG , NotificationChannel .CONSOLE ],
286294 )
287295
288296 self .publish_event (event )
@@ -318,7 +326,7 @@ def report_error(
318326 "context" : context or {},
319327 "timestamp" : datetime .now (),
320328 "acknowledged" : False ,
321- "acknowledged_by" : None
329+ "acknowledged_by" : None ,
322330 }
323331
324332 with self ._lock :
@@ -337,7 +345,7 @@ def report_error(
337345 timestamp = error_record ["timestamp" ],
338346 source_component = component_id ,
339347 metadata = error_record ,
340- channels = [NotificationChannel .LOG , NotificationChannel .CONSOLE ]
348+ channels = [NotificationChannel .LOG , NotificationChannel .CONSOLE ],
341349 )
342350
343351 self .publish_event (event )
@@ -363,7 +371,7 @@ def report_warning(
363371 "message" : message ,
364372 "component_id" : component_id ,
365373 "context" : context or {},
366- "timestamp" : datetime .now ()
374+ "timestamp" : datetime .now (),
367375 }
368376
369377 logger .warning (f"Warning from { component_id } : { message } " )
@@ -377,7 +385,7 @@ def report_warning(
377385 timestamp = warning_record ["timestamp" ],
378386 source_component = component_id ,
379387 metadata = warning_record ,
380- channels = [NotificationChannel .LOG ]
388+ channels = [NotificationChannel .LOG ],
381389 )
382390
383391 self .publish_event (event )
@@ -496,7 +504,9 @@ def send_targeted_message(
496504 delivery_id = str (uuid .uuid4 ())
497505 delivery_ids .append (delivery_id )
498506
499- logger .info (f"Targeted message to { recipient } via { channel .value } : { message } " )
507+ logger .info (
508+ f"Targeted message to { recipient } via { channel .value } : { message } "
509+ )
500510 # Actual delivery implementation would go here
501511
502512 return delivery_ids
@@ -533,7 +543,9 @@ def handle_client_notification(self, notification_data: dict[str, Any]) -> bool:
533543 with self ._lock :
534544 self .stats .total_cancelled_notifications += 1
535545
536- logger .info (f"Handled cancelled notification for request: { notification .params .requestId } " )
546+ logger .info (
547+ f"Handled cancelled notification for request: { notification .params .requestId } "
548+ )
537549
538550 # Create cancellation event
539551 event = NotificationEvent (
@@ -545,9 +557,9 @@ def handle_client_notification(self, notification_data: dict[str, Any]) -> bool:
545557 source_component = "client" ,
546558 metadata = {
547559 "request_id" : notification .params .requestId ,
548- "reason" : notification .params .reason
560+ "reason" : notification .params .reason ,
549561 },
550- channels = [NotificationChannel .LOG ]
562+ channels = [NotificationChannel .LOG ],
551563 )
552564
553565 self .publish_event (event )
@@ -568,7 +580,7 @@ def get_component_state(self) -> dict[str, Any]:
568580 "subscriber_id" : record .subscriber .get_subscriber_id (),
569581 "filters" : record .filters ,
570582 "created_at" : record .created_at .isoformat (),
571- "last_activity" : record .last_activity .isoformat ()
583+ "last_activity" : record .last_activity .isoformat (),
572584 }
573585 for sub_id , record in self ._subscriptions .items ()
574586 }
@@ -586,15 +598,16 @@ def get_component_state(self) -> dict[str, Any]:
586598 "total_cancelled_notifications" : self .stats .total_cancelled_notifications ,
587599 "active_subscriptions" : self .stats .active_subscriptions ,
588600 "intercepted_notifications" : interceptor_stats ["total_intercepted" ],
589- "last_activity" : self .stats .last_activity .isoformat ()
601+ "last_activity" : self .stats .last_activity .isoformat (),
590602 },
591603 "subscriptions" : subscriptions_info ,
592604 "channel_configs" : {
593- channel .value : config for channel , config in self ._channel_configs .items ()
605+ channel .value : config
606+ for channel , config in self ._channel_configs .items ()
594607 },
595608 "event_history_size" : len (self ._event_history ),
596609 "error_history_size" : len (self ._error_history ),
597- "status_history_size" : len (self ._status_history )
610+ "status_history_size" : len (self ._status_history ),
598611 }
599612
600613 def validate_component (self ) -> dict [str , Any ]:
@@ -604,12 +617,15 @@ def validate_component(self) -> dict[str, Any]:
604617 # Check if interceptor is working
605618 interceptor_stats = self .interceptor .get_stats ()
606619 if interceptor_stats ["total_intercepted" ] == 0 :
607- issues .append ("No notifications have been intercepted - may indicate setup issue" )
620+ issues .append (
621+ "No notifications have been intercepted - may indicate setup issue"
622+ )
608623
609624 # Check subscription health
610625 with self ._lock :
611626 stale_subscriptions = [
612- sub_id for sub_id , record in self ._subscriptions .items ()
627+ sub_id
628+ for sub_id , record in self ._subscriptions .items ()
613629 if (datetime .now () - record .last_activity ).seconds > 3600 # 1 hour
614630 ]
615631
@@ -622,40 +638,44 @@ def validate_component(self) -> dict[str, Any]:
622638 "recommendations" : [
623639 "Monitor subscription activity regularly" ,
624640 "Configure channel settings based on deployment environment" ,
625- "Set up proper logging levels for different notification types"
626- ]
641+ "Set up proper logging levels for different notification types" ,
642+ ],
627643 }
628644
629645 def get_debug_info (self , detailed : bool = False ) -> dict [str , Any ]:
630646 """Get debug information about the component."""
631647 debug_info = {
632648 "component_type" : "NotificationOperations" ,
633649 "state" : self .get_component_state (),
634- "validation" : self .validate_component ()
650+ "validation" : self .validate_component (),
635651 }
636652
637653 if detailed :
638- debug_info .update ({
639- "recent_events" : [
640- {
641- "event_id" : event .event_id ,
642- "event_type" : event .event_type ,
643- "level" : event .level .value ,
644- "message" : event .message ,
645- "timestamp" : event .timestamp .isoformat (),
646- "source" : event .source_component
647- }
648- for event in list (self ._event_history )[- 10 :] # Last 10 events
649- ],
650- "recent_errors" : self .get_error_history (limit = 5 ),
651- "interceptor_stats" : self .interceptor .get_stats ()
652- })
654+ debug_info .update (
655+ {
656+ "recent_events" : [
657+ {
658+ "event_id" : event .event_id ,
659+ "event_type" : event .event_type ,
660+ "level" : event .level .value ,
661+ "message" : event .message ,
662+ "timestamp" : event .timestamp .isoformat (),
663+ "source" : event .source_component ,
664+ }
665+ for event in list (self ._event_history )[- 10 :] # Last 10 events
666+ ],
667+ "recent_errors" : self .get_error_history (limit = 5 ),
668+ "interceptor_stats" : self .interceptor .get_stats (),
669+ }
670+ )
653671
654672 return debug_info
655673
656674 # Helper Methods
657675
658- def _find_matching_subscribers (self , event : NotificationEvent ) -> list [tuple [str , EventSubscriber ]]:
676+ def _find_matching_subscribers (
677+ self , event : NotificationEvent
678+ ) -> list [tuple [str , EventSubscriber ]]:
659679 """Find subscribers that match the given event."""
660680 matching = []
661681
@@ -666,7 +686,9 @@ def _find_matching_subscribers(self, event: NotificationEvent) -> list[tuple[str
666686
667687 return matching
668688
669- def _event_matches_filters (self , event : NotificationEvent , filters : dict [str , Any ]) -> bool :
689+ def _event_matches_filters (
690+ self , event : NotificationEvent , filters : dict [str , Any ]
691+ ) -> bool :
670692 """Check if an event matches subscription filters."""
671693 # Simple filter matching - can be enhanced
672694 if "event_type" in filters :
@@ -713,21 +735,24 @@ def _log_message(self, message: str, level: NotificationLevel) -> None:
713735
714736 def _console_message (self , message : str , level : NotificationLevel ) -> None :
715737 """Output message to console (if appropriate)."""
716- if level in [NotificationLevel .WARNING , NotificationLevel .ERROR , NotificationLevel .CRITICAL ]:
738+ if level in [
739+ NotificationLevel .WARNING ,
740+ NotificationLevel .ERROR ,
741+ NotificationLevel .CRITICAL ,
742+ ]:
717743 print (f"[{ level .value .upper ()} ] { message } " )
718744
719- def _report_subscriber_error (self , subscription_id : str , error : Exception , event : NotificationEvent ) -> None :
745+ def _report_subscriber_error (
746+ self , subscription_id : str , error : Exception , event : NotificationEvent
747+ ) -> None :
720748 """Report an error that occurred while notifying a subscriber."""
721749 error_context = {
722750 "subscription_id" : subscription_id ,
723751 "event_id" : event .event_id ,
724752 "event_type" : event .event_type ,
725- "subscriber_error" : str (error )
753+ "subscriber_error" : str (error ),
726754 }
727755
728756 self .report_error (
729- error ,
730- "notification_operations" ,
731- "notify_subscriber" ,
732- error_context
757+ error , "notification_operations" , "notify_subscriber" , error_context
733758 )
0 commit comments