refactor: Consolidate escape sequence decoding across modules

- Removed the custom `_decode_escape_sequences` method from multiple classes and replaced it with a centralized `decode_escape_sequences` function in the utils module.
- Updated all relevant modules (CommandManager, MessageScheduler, GreeterCommand) to utilize the new function for decoding escape sequences in configuration strings, improving code maintainability and consistency.
- Enhanced the config example to clarify the usage of escape sequences in messages.
This commit is contained in:
agessaman committed 2026-02-02 16:18:14 -08:00
1 parent 248a8da568
commit e6bec91c2b
5 files changed
+41 -65

No files matched your search

+5 -31
View File
@@ -16,7 +16,7 @@ from meshcore import EventType
from .models import MeshMessage
from .plugin_loader import PluginLoader
from .commands.base_command import BaseCommand
from .utils import check_internet_connectivity_async, format_keyword_response_with_placeholders
from .utils import check_internet_connectivity_async, decode_escape_sequences, format_keyword_response_with_placeholders
@dataclass
@@ -342,32 +342,6 @@ class CommandManager:
self.bot.bot_tx_rate_limiter.record_tx()
return True
def _decode_escape_sequences(self, text: str) -> str:
"""Decode escape sequences in config strings.
Processes common escape sequences like \\n (newline), \\t (tab), \\\\ (backslash).
This allows users to add newlines in keyword responses using \n (single backslash).
Behavior:
- \n in config file → newline character
- \\n in config file → literal backslash + n
Args:
text: The text string to process.
Returns:
str: The text with escape sequences decoded.
"""
# Replace escape sequences
# Order matters: \\ must be processed first to avoid double-processing
# This preserves literal backslashes (\\n becomes \n, not a newline)
text = text.replace('\\\\', '\x00') # Temporary placeholder for backslash
text = text.replace('\\n', '\n') # Newline
text = text.replace('\\t', '\t') # Tab
text = text.replace('\\r', '\r') # Carriage return
text = text.replace('\x00', '\\') # Restore backslash
return text
def load_keywords(self) -> Dict[str, str]:
"""Load keywords from config.
@@ -380,8 +354,8 @@ class CommandManager:
# Strip quotes from the response if present
if response.startswith('"') and response.endswith('"'):
response = response[1:-1]
# Decode escape sequences (e.g., \\n for newlines)
response = self._decode_escape_sequences(response)
# Decode escape sequences (e.g., \n for newlines)
response = decode_escape_sequences(response)
keywords[keyword.lower()] = response
return keywords
@@ -393,8 +367,8 @@ class CommandManager:
# Strip quotes from the response format if present
if response_format.startswith('"') and response_format.endswith('"'):
response_format = response_format[1:-1]
# Decode escape sequences (e.g., \\n for newlines)
response_format = self._decode_escape_sequences(response_format)
# Decode escape sequences (e.g., \n for newlines)
response_format = decode_escape_sequences(response_format)
syntax_patterns[pattern] = response_format
return syntax_patterns