mirror of
https://github.com/codeflash-ai/codeflash-internal.git
synced 2026-05-04 18:25:18 +00:00
80 lines
2.4 KiB
Python
80 lines
2.4 KiB
Python
"""Python language support for the aiservice.
|
|
|
|
This module provides Python-specific implementations for code validation,
|
|
multi-file context detection, and code formatting.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from aiservice.common.markdown_utils import split_markdown_code as _split_markdown_code
|
|
from languages import register_language
|
|
from languages.python.validator import PythonValidator
|
|
|
|
|
|
@register_language("python")
|
|
class PythonLanguage:
|
|
"""Python language support implementation."""
|
|
|
|
@property
|
|
def language(self) -> str:
|
|
"""The language identifier."""
|
|
return "python"
|
|
|
|
def get_validator(self) -> PythonValidator:
|
|
"""Get the Python code validator."""
|
|
return PythonValidator()
|
|
|
|
def is_multi_context(self, code: str) -> bool:
|
|
"""Check if code is in multi-file markdown format.
|
|
|
|
Multi-file Python code starts with ```python: followed by a filepath.
|
|
|
|
Args:
|
|
code: The source code to check.
|
|
|
|
Returns:
|
|
True if the code is in multi-file markdown format.
|
|
|
|
"""
|
|
return code.strip().startswith("```python:")
|
|
|
|
def get_code_block_tag(self) -> str:
|
|
"""Get the markdown code block tag for Python.
|
|
|
|
Returns:
|
|
The string 'python' used in markdown code blocks.
|
|
|
|
"""
|
|
return "python"
|
|
|
|
def split_markdown_code(self, markdown: str) -> dict[str, str]:
|
|
"""Split markdown into filepath -> code dict.
|
|
|
|
Parses markdown with ```python:filepath blocks and returns
|
|
a dictionary mapping file paths to their code content.
|
|
|
|
Args:
|
|
markdown: The markdown text containing code blocks.
|
|
|
|
Returns:
|
|
A dictionary mapping file paths to code content.
|
|
|
|
"""
|
|
return _split_markdown_code(markdown, language="python")
|
|
|
|
def group_code(self, file_to_code: dict[str, str]) -> str:
|
|
"""Combine code files into markdown format.
|
|
|
|
Args:
|
|
file_to_code: A dictionary mapping file paths to code content.
|
|
|
|
Returns:
|
|
Markdown-formatted string with ```python:filepath blocks.
|
|
|
|
"""
|
|
blocks = []
|
|
for file_path, code in file_to_code.items():
|
|
# Ensure code ends with newline before closing ```
|
|
normalized = code if code.endswith("\n") else code + "\n"
|
|
blocks.append(f"```python:{file_path}\n{normalized}```")
|
|
return "\n".join(blocks)
|