56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
"""
|
|
Text file processor
|
|
"""
|
|
import logging
|
|
from pathlib import Path
|
|
from typing import Dict, Any
|
|
from core import FileProcessingError
|
|
from config import settings
|
|
from .base_processor import FileProcessor
|
|
|
|
|
|
class TextProcessor(FileProcessor):
|
|
"""Processor for text files"""
|
|
|
|
def __init__(self):
|
|
super().__init__("TextProcessor")
|
|
self.logger = logging.getLogger(__name__)
|
|
|
|
def can_process(self, file_path: str) -> bool:
|
|
"""Check if file is a text file"""
|
|
ext = self.get_file_extension(file_path)
|
|
return ext in settings.TXT_EXTENSIONS
|
|
|
|
def process(self, file_path: str) -> Dict[str, Any]:
|
|
"""Process text file (copy to downloads)"""
|
|
self.validate_file(file_path)
|
|
text_path = Path(file_path)
|
|
output_path = settings.LOCAL_DOWNLOADS_PATH / text_path.name
|
|
|
|
self.logger.info(f"Processing text file: {text_path}")
|
|
|
|
try:
|
|
# Copy file to downloads directory
|
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
with open(text_path, 'r', encoding='utf-8') as src:
|
|
with open(output_path, 'w', encoding='utf-8') as dst:
|
|
dst.write(src.read())
|
|
|
|
self.logger.info(f"Text file processing completed: {output_path}")
|
|
|
|
return {
|
|
"success": True,
|
|
"text_path": str(output_path),
|
|
"text": self._read_file(output_path)
|
|
}
|
|
|
|
except Exception as e:
|
|
self.logger.error(f"Text processing failed: {e}")
|
|
raise FileProcessingError(f"Text processing failed: {e}")
|
|
|
|
def _read_file(self, file_path: Path) -> str:
|
|
"""Read file content"""
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
return f.read()
|