mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-11-25 22:53:17 +09:00
background tasks
This commit is contained in:
1 parent
e1afe444d4
commit
9811b78b5b
215 files changed
+39531
-2917
No files matched your search
@@ -0,0 +1,8 @@
|
||||
"""Core package for Hello World application"""
|
||||
from .greeting_generator import GreetingGenerator
|
||||
from .message_formatter import MessageFormatter
|
||||
|
||||
__all__ = [
|
||||
'GreetingGenerator',
|
||||
'MessageFormatter'
|
||||
]
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,223 @@
|
||||
"""
|
||||
Greeting generator module for Hello World application
|
||||
"""
|
||||
import random
|
||||
from typing import Dict, List, Optional
|
||||
from dataclasses import dataclass
|
||||
from utils.decorators import memoize, timer
|
||||
from config import Config
|
||||
|
||||
|
||||
@dataclass
|
||||
class GreetingTemplate:
|
||||
"""Template for a greeting in a specific language"""
|
||||
language: str
|
||||
formal: str
|
||||
informal: str
|
||||
casual: str
|
||||
time_based: Dict[str, str]
|
||||
special: Dict[str, str]
|
||||
|
||||
|
||||
class GreetingGenerator:
|
||||
"""Generate greetings in multiple languages with various styles"""
|
||||
|
||||
def __init__(self, config: Config):
|
||||
self.config = config
|
||||
self.templates = self._initialize_templates()
|
||||
self.emoji_map = self._initialize_emoji_map()
|
||||
|
||||
def _initialize_templates(self) -> Dict[str, GreetingTemplate]:
|
||||
"""Initialize greeting templates for all supported languages"""
|
||||
return {
|
||||
"english": GreetingTemplate(
|
||||
language="english",
|
||||
formal="Good day, {name}!",
|
||||
informal="Hello, {name}!",
|
||||
casual="Hey {name}!",
|
||||
time_based={
|
||||
"morning": "Good morning, {name}!",
|
||||
"afternoon": "Good afternoon, {name}!",
|
||||
"evening": "Good evening, {name}!",
|
||||
"night": "Good night, {name}!"
|
||||
},
|
||||
special={
|
||||
"welcome": "Welcome, {name}!",
|
||||
"greeting": "Greetings, {name}!",
|
||||
"salutation": "Salutations, {name}!"
|
||||
}
|
||||
),
|
||||
"spanish": GreetingTemplate(
|
||||
language="spanish",
|
||||
formal="Buenos días, {name}!",
|
||||
informal="¡Hola, {name}!",
|
||||
casual="¡Ey {name}!",
|
||||
time_based={
|
||||
"morning": "¡Buenos días, {name}!",
|
||||
"afternoon": "¡Buenas tardes, {name}!",
|
||||
"evening": "¡Buenas tardes, {name}!",
|
||||
"night": "¡Buenas noches, {name}!"
|
||||
},
|
||||
special={
|
||||
"welcome": "¡Bienvenido, {name}!",
|
||||
"greeting": "¡Saludos, {name}!",
|
||||
"salutation": "¡Saludos cordiales, {name}!"
|
||||
}
|
||||
),
|
||||
"french": GreetingTemplate(
|
||||
language="french",
|
||||
formal="Bonjour, {name}!",
|
||||
informal="Salut, {name}!",
|
||||
casual="Coucou {name}!",
|
||||
time_based={
|
||||
"morning": "Bonjour, {name}!",
|
||||
"afternoon": "Bon après-midi, {name}!",
|
||||
"evening": "Bonsoir, {name}!",
|
||||
"night": "Bonne nuit, {name}!"
|
||||
},
|
||||
special={
|
||||
"welcome": "Bienvenue, {name}!",
|
||||
"greeting": "Salutations, {name}!",
|
||||
"salutation": "Mes salutations, {name}!"
|
||||
}
|
||||
),
|
||||
"german": GreetingTemplate(
|
||||
language="german",
|
||||
formal="Guten Tag, {name}!",
|
||||
informal="Hallo, {name}!",
|
||||
casual="Hey {name}!",
|
||||
time_based={
|
||||
"morning": "Guten Morgen, {name}!",
|
||||
"afternoon": "Guten Tag, {name}!",
|
||||
"evening": "Guten Abend, {name}!",
|
||||
"night": "Gute Nacht, {name}!"
|
||||
},
|
||||
special={
|
||||
"welcome": "Willkommen, {name}!",
|
||||
"greeting": "Grüße, {name}!",
|
||||
"salutation": "Herzliche Grüße, {name}!"
|
||||
}
|
||||
),
|
||||
"italian": GreetingTemplate(
|
||||
language="italian",
|
||||
formal="Buongiorno, {name}!",
|
||||
informal="Ciao, {name}!",
|
||||
casual="Ehi {name}!",
|
||||
time_based={
|
||||
"morning": "Buongiorno, {name}!",
|
||||
"afternoon": "Buon pomeriggio, {name}!",
|
||||
"evening": "Buonasera, {name}!",
|
||||
"night": "Buonanotte, {name}!"
|
||||
},
|
||||
special={
|
||||
"welcome": "Benvenuto, {name}!",
|
||||
"greeting": "Saluti, {name}!",
|
||||
"salutation": "Cordiali saluti, {name}!"
|
||||
}
|
||||
),
|
||||
"japanese": GreetingTemplate(
|
||||
language="japanese",
|
||||
formal="こんにちは、{name}さん!",
|
||||
informal="やあ、{name}!",
|
||||
casual="よー、{name}!",
|
||||
time_based={
|
||||
"morning": "おはようございます、{name}さん!",
|
||||
"afternoon": "こんにちは、{name}さん!",
|
||||
"evening": "こんばんは、{name}さん!",
|
||||
"night": "おやすみなさい、{name}さん!"
|
||||
},
|
||||
special={
|
||||
"welcome": "ようこそ、{name}さん!",
|
||||
"greeting": "ご挨拶、{name}さん!",
|
||||
"salutation": "よろしく、{name}さん!"
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
def _initialize_emoji_map(self) -> Dict[str, str]:
|
||||
"""Initialize emoji map for different languages"""
|
||||
return {
|
||||
"english": "👋",
|
||||
"spanish": "🇪🇸",
|
||||
"french": "🇫🇷",
|
||||
"german": "🇩🇪",
|
||||
"italian": "🇮🇹",
|
||||
"japanese": "🇯🇵"
|
||||
}
|
||||
|
||||
@timer
|
||||
@memoize
|
||||
def generate(self, name: str, language: str = None,
|
||||
style: str = "informal", variation: str = None) -> str:
|
||||
"""Generate a greeting for the given name and language"""
|
||||
# Use default language if not specified
|
||||
language = language or self.config.default_language
|
||||
|
||||
# Validate language
|
||||
if language not in self.templates:
|
||||
raise ValueError(
|
||||
f"Unsupported language: {language}. "
|
||||
f"Supported languages: {list(self.templates.keys())}"
|
||||
)
|
||||
|
||||
template = self.templates[language]
|
||||
|
||||
# Select greeting based on style and variation
|
||||
if variation and variation in template.time_based:
|
||||
greeting = template.time_based[variation]
|
||||
elif variation and variation in template.special:
|
||||
greeting = template.special[variation]
|
||||
elif style == "formal":
|
||||
greeting = template.formal
|
||||
elif style == "casual":
|
||||
greeting = template.casual
|
||||
else:
|
||||
greeting = template.informal
|
||||
|
||||
# Format greeting with name
|
||||
formatted_greeting = greeting.format(name=name)
|
||||
|
||||
# Add emoji if enabled
|
||||
if self.config.enable_emoji and language in self.emoji_map:
|
||||
formatted_greeting = f"{self.emoji_map[language]} {formatted_greeting}"
|
||||
|
||||
return formatted_greeting
|
||||
|
||||
def get_random_greeting(self, name: str, language: str = None) -> str:
|
||||
"""Get a random greeting variation"""
|
||||
language = language or self.config.default_language
|
||||
template = self.templates[language]
|
||||
|
||||
# Collect all available greetings
|
||||
all_greetings = [
|
||||
template.formal,
|
||||
template.informal,
|
||||
template.casual
|
||||
] + list(template.time_based.values()) + list(template.special.values())
|
||||
|
||||
# Select random greeting
|
||||
greeting = random.choice(all_greetings)
|
||||
formatted_greeting = greeting.format(name=name)
|
||||
|
||||
# Add emoji if enabled
|
||||
if self.config.enable_emoji and language in self.emoji_map:
|
||||
formatted_greeting = f"{self.emoji_map[language]} {formatted_greeting}"
|
||||
|
||||
return formatted_greeting
|
||||
|
||||
def get_available_styles(self) -> List[str]:
|
||||
"""Get list of available greeting styles"""
|
||||
return ["formal", "informal", "casual"]
|
||||
|
||||
def get_available_variations(self, language: str = None) -> Dict[str, List[str]]:
|
||||
"""Get available variations for a language"""
|
||||
language = language or self.config.default_language
|
||||
|
||||
if language not in self.templates:
|
||||
return {}
|
||||
|
||||
template = self.templates[language]
|
||||
return {
|
||||
"time_based": list(template.time_based.keys()),
|
||||
"special": list(template.special.keys())
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
"""
|
||||
Message formatter module for Hello World application
|
||||
"""
|
||||
import textwrap
|
||||
from datetime import datetime
|
||||
from typing import Optional, Dict, Any
|
||||
from config import Config
|
||||
from utils.decorators import timer
|
||||
|
||||
|
||||
class MessageFormatter:
|
||||
"""Format messages in various styles"""
|
||||
|
||||
def __init__(self, config: Config):
|
||||
self.config = config
|
||||
self.styles = self._initialize_styles()
|
||||
|
||||
def _initialize_styles(self) -> Dict[str, Any]:
|
||||
"""Initialize formatting styles"""
|
||||
return {
|
||||
"simple": self._format_simple,
|
||||
"fancy": self._format_fancy,
|
||||
"banner": self._format_banner,
|
||||
"ascii": self._format_ascii
|
||||
}
|
||||
|
||||
@timer
|
||||
def format(self, message: str, style: str = None,
|
||||
timestamp: Optional[datetime] = None,
|
||||
additional_info: Optional[Dict[str, Any]] = None) -> str:
|
||||
"""Format a message in the specified style"""
|
||||
style = style or self.config.default_style
|
||||
|
||||
if style not in self.styles:
|
||||
raise ValueError(
|
||||
f"Unsupported style: {style}. "
|
||||
f"Supported styles: {list(self.styles.keys())}"
|
||||
)
|
||||
|
||||
# Apply color if enabled
|
||||
if self.config.enable_colors:
|
||||
message = self._apply_colors(message, style)
|
||||
|
||||
# Format message
|
||||
formatted = self.styles[style](message, timestamp, additional_info)
|
||||
|
||||
# Wrap text if needed
|
||||
if self.config.max_line_length > 0:
|
||||
lines = formatted.split('\n')
|
||||
wrapped_lines = []
|
||||
for line in lines:
|
||||
if len(line) > self.config.max_line_length:
|
||||
wrapped = textwrap.fill(
|
||||
line,
|
||||
width=self.config.max_line_length,
|
||||
break_long_words=False
|
||||
)
|
||||
wrapped_lines.extend(wrapped.split('\n'))
|
||||
else:
|
||||
wrapped_lines.append(line)
|
||||
formatted = '\n'.join(wrapped_lines)
|
||||
|
||||
return formatted
|
||||
|
||||
def _format_simple(self, message: str, timestamp: Optional[datetime],
|
||||
additional_info: Optional[Dict[str, Any]]) -> str:
|
||||
"""Simple format: just the message"""
|
||||
return message
|
||||
|
||||
def _format_fancy(self, message: str, timestamp: Optional[datetime],
|
||||
additional_info: Optional[Dict[str, Any]]) -> str:
|
||||
"""Fancy format with decorations"""
|
||||
lines = []
|
||||
|
||||
# Top border
|
||||
lines.append("╔" + "═" * (len(message) + 2) + "╗")
|
||||
|
||||
# Message
|
||||
lines.append(f"║ {message} ║")
|
||||
|
||||
# Timestamp if provided
|
||||
if timestamp:
|
||||
time_str = timestamp.strftime("%Y-%m-%d %H:%M:%S")
|
||||
lines.append("╠" + "═" * (len(message) + 2) + "╣")
|
||||
lines.append(f"║ {time_str.center(len(message))} ║")
|
||||
|
||||
# Additional info if provided
|
||||
if additional_info:
|
||||
lines.append("╠" + "═" * (len(message) + 2) + "╣")
|
||||
for key, value in additional_info.items():
|
||||
info_line = f"{key}: {value}"
|
||||
lines.append(f"║ {info_line.center(len(message))} ║")
|
||||
|
||||
# Bottom border
|
||||
lines.append("╚" + "═" * (len(message) + 2) + "╝")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
def _format_banner(self, message: str, timestamp: Optional[datetime],
|
||||
additional_info: Optional[Dict[str, Any]]) -> str:
|
||||
"""Banner format with ASCII art"""
|
||||
banner_width = max(len(message) + 20, 60)
|
||||
lines = []
|
||||
|
||||
# Top banner
|
||||
lines.append("*" * banner_width)
|
||||
lines.append("*" + " " * (banner_width - 2) + "*")
|
||||
|
||||
# Message centered
|
||||
lines.append("*" + message.center(banner_width - 2) + "*")
|
||||
|
||||
# Timestamp if provided
|
||||
if timestamp:
|
||||
lines.append("*" + " " * (banner_width - 2) + "*")
|
||||
time_str = timestamp.strftime("%Y-%m-%d %H:%M:%S")
|
||||
lines.append("*" + time_str.center(banner_width - 2) + "*")
|
||||
|
||||
# Additional info if provided
|
||||
if additional_info:
|
||||
lines.append("*" + " " * (banner_width - 2) + "*")
|
||||
for key, value in additional_info.items():
|
||||
info_line = f"{key}: {value}"
|
||||
lines.append("*" + info_line.center(banner_width - 2) + "*")
|
||||
|
||||
# Bottom banner
|
||||
lines.append("*" + " " * (banner_width - 2) + "*")
|
||||
lines.append("*" * banner_width)
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
def _format_ascii(self, message: str, timestamp: Optional[datetime],
|
||||
additional_info: Optional[Dict[str, Any]]) -> str:
|
||||
"""ASCII art format"""
|
||||
lines = []
|
||||
|
||||
# ASCII art header
|
||||
lines.extend([
|
||||
" _ _ _ _ __ __ _ _ _ ",
|
||||
" | | | | | | | \\ \\ / / | | | | |",
|
||||
" | |__| | ___| | | ___ \\ \\ / /__ _ __| | __| | |",
|
||||
" | __ |/ _ \\ | |/ _ \\ \\ \\ / / _ \\| '__| |/ _` | |",
|
||||
" | | | | __/ | | (_) | \\ \\/ / (_) | | | | (_| |_|",
|
||||
" |_| |_|\\___|_|_|\\___/ \\__/ \\___/|_| |_|\\__,_(_)",
|
||||
""
|
||||
])
|
||||
|
||||
# Message
|
||||
lines.append(message.center(57))
|
||||
|
||||
# Timestamp if provided
|
||||
if timestamp:
|
||||
lines.append("")
|
||||
time_str = timestamp.strftime("%Y-%m-%d %H:%M:%S")
|
||||
lines.append(time_str.center(57))
|
||||
|
||||
# Additional info if provided
|
||||
if additional_info:
|
||||
lines.append("")
|
||||
for key, value in additional_info.items():
|
||||
info_line = f"{key}: {value}"
|
||||
lines.append(info_line.center(57))
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
def _apply_colors(self, message: str, style: str) -> str:
|
||||
"""Apply ANSI color codes to message"""
|
||||
color_map = {
|
||||
"simple": "\033[0m", # Default
|
||||
"fancy": "\033[1;36m", # Bold Cyan
|
||||
"banner": "\033[1;33m", # Bold Yellow
|
||||
"ascii": "\033[1;35m" # Bold Magenta
|
||||
}
|
||||
|
||||
reset = "\033[0m"
|
||||
color = color_map.get(style, "\033[0m")
|
||||
|
||||
return f"{color}{message}{reset}"
|
||||
|
||||
def create_progress_bar(self, current: int, total: int,
|
||||
width: int = 50, fill_char: str = "█") -> str:
|
||||
"""Create a progress bar"""
|
||||
if total == 0:
|
||||
return "[" + " " * width + "]"
|
||||
|
||||
filled = int(width * current / total)
|
||||
empty = width - filled
|
||||
|
||||
bar = "[" + fill_char * filled + " " * empty + "]"
|
||||
percentage = f" {current}/{total} ({100 * current / total:.1f}%)"
|
||||
|
||||
return bar + percentage
|
||||
|
||||
def create_table(self, headers: list, rows: list) -> str:
|
||||
"""Create a simple ASCII table"""
|
||||
if not headers or not rows:
|
||||
return ""
|
||||
|
||||
# Calculate column widths
|
||||
col_widths = [len(str(h)) for h in headers]
|
||||
for row in rows:
|
||||
for i, cell in enumerate(row):
|
||||
col_widths[i] = max(col_widths[i], len(str(cell)))
|
||||
|
||||
# Create separator
|
||||
separator = "+" + "+".join(["-" * (w + 2) for w in col_widths]) + "+"
|
||||
|
||||
# Format header
|
||||
header_row = "|" + "|".join([
|
||||
f" {str(h).ljust(w)} " for h, w in zip(headers, col_widths)
|
||||
]) + "|"
|
||||
|
||||
# Format rows
|
||||
data_rows = []
|
||||
for row in rows:
|
||||
data_row = "|" + "|".join([
|
||||
f" {str(cell).ljust(w)} "
|
||||
for cell, w in zip(row, col_widths)
|
||||
]) + "|"
|
||||
data_rows.append(data_row)
|
||||
|
||||
# Combine all parts
|
||||
table_parts = [separator, header_row, separator]
|
||||
table_parts.extend(data_rows)
|
||||
table_parts.append(separator)
|
||||
|
||||
return "\n".join(table_parts)
|
||||
Reference in new issue
Block a user