@@ -511,16 +511,33 @@ def _update_label(self):
511511 # Choose which label set to update (primary or alternate)
512512 active_widgets = self ._widgets_alt if self ._show_alt_label else self ._widgets
513513 active_label_content = self ._label_alt_content if self ._show_alt_label else self ._label_content
514- label_parts = re .split ("(<span.*?>.*?</span>)" , active_label_content )
515- label_parts = [part for part in label_parts if part ]
516514 widget_index = 0
515+
517516 now = datetime .now (ZoneInfo (self ._active_tz )) if self ._active_tz else datetime .now ().astimezone ()
517+
518+ # Finding the datetime format string in the label using keyerror exception.
519+ try :
520+ active_label_content .format (icon = "" , alarm = "" , timedata = now )
521+ except KeyError as ke :
522+ # Remove the quotes around the exception message to get the datetime format.
523+ datetime_format = str (ke )[1 :- 1 ]
524+
525+ datetime_format_idx = active_label_content .find (datetime_format )
526+ closing_braces_idx = active_label_content .find ("}" , datetime_format_idx + len (datetime_format ))
527+
528+ datetime_format = active_label_content [datetime_format_idx :closing_braces_idx ]
529+ active_label_content = active_label_content .replace (
530+ "{" + datetime_format + "}" , now .strftime (datetime_format )
531+ )
532+
518533 current_hour = f"{ now .hour :02d} "
519534 current_minute = f"{ now .minute :02d} "
520535 hour_changed = self ._current_hour != current_hour
521536 minute_changed = self ._current_minute != current_minute
537+
522538 if hour_changed :
523539 self ._current_hour = current_hour
540+
524541 if minute_changed :
525542 self ._current_minute = current_minute
526543
@@ -539,72 +556,53 @@ def _update_label(self):
539556 self ._timer_label .hide ()
540557 self ._timer_visible = False
541558
559+ clock_icon = self ._get_icon_for_hour (now .hour )
560+ hour_class = f"clock_{ current_hour } "
561+ alarm_icon = alarm_class = ""
562+
563+ if self ._shared_state ._snoozed_alarms :
564+ alarm_class = "icon alarm snooze"
565+ alarm_icon = self ._alarm_icons ["snooze" ]
566+
567+ elif self ._has_enabled_alarms ():
568+ alarm_icon = self ._alarm_icons ["enabled" ]
569+ alarm_class = "icon alarm"
570+
571+ # The icon place holders are replaced with a span tag of the icon string, with all of its appropriate classes
572+ # assigned to it.
573+ label_content_values = {
574+ "icon" : f'<span class="{ hour_class } ">{ clock_icon } </span>' ,
575+ "alarm" : f'<span class="{ alarm_class } ">{ alarm_icon } </span>' ,
576+ "timedata" : now ,
577+ }
578+
579+ active_label_content = active_label_content .format_map (label_content_values )
580+
581+ # If any of the icon placeholders are already in a span tag, then flatten the 2 layers of nested span tags, and
582+ # merge their classes into one span tag, before sending them for parsing.
583+ active_label_content = re .sub (
584+ r'<span(?: class=([\'"])(?P<class_name>[\w -]*)\1)?><span class=([\'"])(?P<class_name2>[\w -]*)\3>(?P<label_name>.*?)</span></span>' ,
585+ r'<span class="\g<class_name> \g<class_name2>">\g<label_name></span>' ,
586+ active_label_content ,
587+ )
588+
589+ label_parts = re .split ("(<span.*?>.*?</span>)" , active_label_content )
590+
542591 for part in label_parts :
543592 part = part .strip ()
544593 if part and widget_index < len (active_widgets ) and isinstance (active_widgets [widget_index ], QLabel ):
545594 if "<span" in part and "</span>" in part :
595+ match = re .search (r'<span class=([\'"])(?P<class_name>[\w -]*)\1>[^>]*</span>' , part )
596+ classes = match .group ("class_name" ) or ""
597+
546598 icon_placeholder = re .sub (r"<span.*?>|</span>" , "" , part ).strip ()
547- if icon_placeholder == "{icon}" :
548- if hour_changed :
549- icon = self ._get_icon_for_hour (now .hour )
550- active_widgets [widget_index ].setText (icon )
551- hour_class = f"clock_{ current_hour } "
552- active_widgets [widget_index ].setProperty ("class" , f"icon { hour_class } " )
553- refresh_widget_style (active_widgets [widget_index ])
554- elif icon_placeholder == "{alarm}" :
555- if self ._shared_state ._snoozed_alarms :
556- active_widgets [widget_index ].setText (self ._alarm_icons ["snooze" ])
557- active_widgets [widget_index ].setProperty ("class" , "icon alarm snooze" )
558- active_widgets [widget_index ].setVisible (True )
559- refresh_widget_style (active_widgets [widget_index ])
560- elif self ._has_enabled_alarms ():
561- active_widgets [widget_index ].setText (self ._alarm_icons ["enabled" ])
562- active_widgets [widget_index ].setProperty ("class" , "icon alarm" )
563- active_widgets [widget_index ].setVisible (True )
564- refresh_widget_style (active_widgets [widget_index ])
565- else :
566- active_widgets [widget_index ].setText ("" )
567- active_widgets [widget_index ].setVisible (False )
568-
569- else :
570- active_widgets [widget_index ].setText (icon_placeholder )
571- else :
572- has_alarm = "{alarm}" in part and (self ._shared_state ._snoozed_alarms or self ._has_enabled_alarms ())
573-
574- if "{icon}" in part :
575- icon = self ._get_icon_for_hour (now .hour )
576- part = part .replace ("{icon}" , icon )
577-
578- if "{alarm}" in part :
579- if self ._shared_state ._snoozed_alarms :
580- part = part .replace ("{alarm}" , self ._alarm_icons ["snooze" ])
581- elif self ._has_enabled_alarms ():
582- part = part .replace ("{alarm}" , self ._alarm_icons ["enabled" ])
583- else :
584- part = part .replace ("{alarm}" , "" )
585- try :
586- datetime_format_search = re .search (r"\{(.*)}" , part )
587- datetime_format_str = datetime_format_search .group ()
588- datetime_format = datetime_format_search .group (1 )
589- format_label_content = part .replace (datetime_format_str , now .strftime (datetime_format ))
590- except Exception :
591- format_label_content = part
592-
593- active_widgets [widget_index ].setText (format_label_content )
594-
595- alarm_state_changed = has_alarm != self ._previous_alarm_state
596- if has_alarm :
597- if self ._shared_state ._snoozed_alarms :
598- active_widgets [widget_index ].setProperty ("class" , "label alarm snooze" )
599- else :
600- active_widgets [widget_index ].setProperty ("class" , "label alarm" )
601- refresh_widget_style (active_widgets [widget_index ])
602- else :
603- hour_class = f"clock_{ current_hour } "
604- active_widgets [widget_index ].setProperty ("class" , f"label { hour_class } " )
605- if hour_changed or alarm_state_changed :
606- refresh_widget_style (active_widgets [widget_index ])
599+ active_widgets [widget_index ].setText (icon_placeholder )
600+ active_widgets [widget_index ].setProperty ("class" , classes .strip ())
601+ refresh_widget_style (active_widgets [widget_index ])
607602
603+ else :
604+ active_widgets [widget_index ].setText (part )
605+ has_alarm = self ._shared_state ._snoozed_alarms or self ._has_enabled_alarms ()
608606 self ._previous_alarm_state = has_alarm
609607 widget_index += 1
610608
0 commit comments