This method returns a random comment based on the specified theme.
(self, theme)
| 130 | } |
| 131 | |
| 132 | def get_commentaire(self, theme): |
| 133 | """ This method returns a random comment based on the specified theme.""" |
| 134 | current_time = time.time() # Get the current time in seconds |
| 135 | if theme != self.last_theme or current_time - self.last_comment_time >= self.comment_delay: # Check if the theme has changed or if the delay has expired |
| 136 | self.last_comment_time = current_time # Update the last comment time |
| 137 | self.last_theme = theme # Update the last theme |
| 138 | |
| 139 | # Handle theme case variations and missing themes |
| 140 | original_theme = theme |
| 141 | if theme not in self.themes: |
| 142 | # Try case-insensitive lookup |
| 143 | theme_lower = theme.lower() |
| 144 | matching_themes = [t for t in self.themes.keys() if t.lower() == theme_lower] |
| 145 | |
| 146 | if matching_themes: |
| 147 | theme = matching_themes[0] |
| 148 | logger.debug(f"Theme '{original_theme}' matched to '{theme}' (case variation)") |
| 149 | else: |
| 150 | logger.warning(f"Theme '{original_theme}' is not defined, using fallback to IDLE.") |
| 151 | theme = "IDLE" |
| 152 | |
| 153 | # If even IDLE is missing, create emergency fallback |
| 154 | if theme not in self.themes: |
| 155 | logger.error(f"Critical: IDLE theme missing! Using emergency fallback.") |
| 156 | return "System operational..." |
| 157 | |
| 158 | # Return random comment from the theme |
| 159 | try: |
| 160 | return random.choice(self.themes[theme]) |
| 161 | except (KeyError, IndexError) as e: |
| 162 | logger.error(f"Error getting comment for theme '{theme}': {e}") |
| 163 | return "System operational..." |
| 164 | else: |
| 165 | return None |
no test coverage detected