ReMe/reme_cli/utils/case_converter.py
jinli.yl 5fc57e7f4e feat(core): refactor application architecture with new component base classes
- Add BaseClient, BaseFileStore, BaseFileWatcher, BaseJob, BaseService, and BaseStep classes
- Move component initialization logic from ApplicationContext to Application class
- Add logo printing and logging initialization in Application startup
- Create client module with base client implementation
- Add file store base class with embedding resolution and validation
- Implement file watcher base class with watchfiles integration
- Add job base class for sequential step execution orchestration
- Create service base class for job exposure mechanisms
- Refactor BaseStep with LLM workflow execution capabilities
- Add case converter utility for naming convention transformations
- Update import structure and module organization
- Add proper type hints and docstrings across all components
- Implement component registry integration for dynamic loading
- Add error handling for missing backend configurations
2026-04-14 16:43:36 +08:00

46 lines
1.5 KiB
Python

"""Case conversion utility for PascalCase, camelCase, and snake_case.
Provides bidirectional conversion between snake_case and PascalCase,
with special handling for common acronyms (LLM, API, URL, etc.).
"""
import re
# Acronyms that should remain uppercase in Pascal/camelCase
_ACRONYMS = frozenset({"LLM", "API", "URL", "HTTP", "JSON", "XML", "AI", "MCP"})
_ACRONYM_MAP = {word.lower(): word for word in _ACRONYMS}
def camel_to_snake(content: str) -> str:
"""Convert PascalCase or camelCase to snake_case.
Handles acronyms correctly by normalizing them before conversion.
For example, "OpenAILLMClient" becomes "open_ai_llm_client".
Args:
content: A string in PascalCase or camelCase format.
Returns:
The converted snake_case string.
"""
# Normalize acronyms to title case (e.g., LLM -> Llm) to assist regex splitting
for word in _ACRONYMS:
content = content.replace(word, word.capitalize())
# Insert underscores between case transitions and convert to lowercase
return re.sub(r"(?<!^)(?=[A-Z])", "_", content).lower()
def snake_to_camel(content: str) -> str:
"""Convert snake_case to PascalCase.
Preserves defined acronyms in uppercase form.
For example, "open_ai_llm_client" becomes "OpenAILLMClient".
Args:
content: A string in snake_case format.
Returns:
The converted PascalCase string with acronyms preserved.
"""
return "".join(_ACRONYM_MAP.get(part.lower(), part.capitalize()) for part in content.split("_") if part)