Spaces:
Runtime error
Runtime error
Delete server/file_manager.py
Browse files- server/file_manager.py +0 -104
server/file_manager.py
DELETED
|
@@ -1,104 +0,0 @@
|
|
| 1 |
-
"""Работа с файлами"""
|
| 2 |
-
|
| 3 |
-
import os
|
| 4 |
-
import fnmatch
|
| 5 |
-
from typing import Dict, List, Tuple, Any
|
| 6 |
-
from datetime import datetime
|
| 7 |
-
|
| 8 |
-
class FileManager:
|
| 9 |
-
SUPPORTED_EXTENSIONS = {
|
| 10 |
-
'.txt': 'text', '.py': 'python', '.js': 'javascript',
|
| 11 |
-
'.html': 'html', '.css': 'css', '.json': 'json',
|
| 12 |
-
'.yaml': 'yaml', '.yml': 'yaml', '.md': 'markdown',
|
| 13 |
-
'.csv': 'csv', '.xml': 'xml', '.log': 'log',
|
| 14 |
-
'.sql': 'sql', '.sh': 'bash', '.bat': 'batch',
|
| 15 |
-
'.ps1': 'powershell', '.ipynb': 'jupyter',
|
| 16 |
-
}
|
| 17 |
-
|
| 18 |
-
def __init__(self, base_dir: str = "."):
|
| 19 |
-
self.base_dir = base_dir
|
| 20 |
-
|
| 21 |
-
def read_file(self, file_path: str, max_size: int = 100000) -> Tuple[str, str]:
|
| 22 |
-
full_path = os.path.join(self.base_dir, file_path)
|
| 23 |
-
if not os.path.exists(full_path):
|
| 24 |
-
return ("", f"❌ Файл не найден: {file_path}")
|
| 25 |
-
try:
|
| 26 |
-
size = os.path.getsize(full_path)
|
| 27 |
-
if size > max_size:
|
| 28 |
-
return ("", f"⚠️ Файл слишком большой ({size} bytes > {max_size})")
|
| 29 |
-
with open(full_path, 'r', encoding='utf-8', errors='ignore') as f:
|
| 30 |
-
content = f.read()
|
| 31 |
-
ext = os.path.splitext(file_path)[1].lower()
|
| 32 |
-
lang = self.SUPPORTED_EXTENSIONS.get(ext, 'text')
|
| 33 |
-
return (content, f"✅ Прочитано {len(content)} chars ({lang})")
|
| 34 |
-
except Exception as e:
|
| 35 |
-
return ("", f"❌ Ошибка чтения: {e}")
|
| 36 |
-
|
| 37 |
-
def read_multiple_files(self, file_paths: List[str], max_total: int = 50000) -> Dict[str, Tuple[str, str]]:
|
| 38 |
-
results = {}
|
| 39 |
-
total = 0
|
| 40 |
-
for fp in file_paths:
|
| 41 |
-
content, status = self.read_file(fp)
|
| 42 |
-
if content:
|
| 43 |
-
total += len(content)
|
| 44 |
-
if total > max_total:
|
| 45 |
-
results[fp] = ("", "⚠️ Превышен общий лимит")
|
| 46 |
-
break
|
| 47 |
-
results[fp] = (content, status)
|
| 48 |
-
return results
|
| 49 |
-
|
| 50 |
-
def list_files(self, directory: str = ".", pattern: str = "*", recursive: bool = False) -> List[str]:
|
| 51 |
-
base = os.path.join(self.base_dir, directory)
|
| 52 |
-
if not os.path.exists(base):
|
| 53 |
-
return []
|
| 54 |
-
results = []
|
| 55 |
-
if recursive:
|
| 56 |
-
for root, dirs, files in os.walk(base):
|
| 57 |
-
for f in files:
|
| 58 |
-
rel = os.path.relpath(os.path.join(root, f), self.base_dir)
|
| 59 |
-
if self._match_pattern(f, pattern):
|
| 60 |
-
results.append(rel)
|
| 61 |
-
else:
|
| 62 |
-
for f in os.listdir(base):
|
| 63 |
-
if os.path.isfile(os.path.join(base, f)) and self._match_pattern(f, pattern):
|
| 64 |
-
results.append(os.path.join(directory, f))
|
| 65 |
-
return results
|
| 66 |
-
|
| 67 |
-
def save_file(self, file_path: str, content: str) -> str:
|
| 68 |
-
full_path = os.path.join(self.base_dir, file_path)
|
| 69 |
-
try:
|
| 70 |
-
os.makedirs(os.path.dirname(full_path), exist_ok=True)
|
| 71 |
-
with open(full_path, 'w', encoding='utf-8') as f:
|
| 72 |
-
f.write(content)
|
| 73 |
-
return f"✅ Сохранено: {file_path} ({len(content)} chars)"
|
| 74 |
-
except Exception as e:
|
| 75 |
-
return f"❌ Ошибка сохранения: {e}"
|
| 76 |
-
|
| 77 |
-
def analyze_file(self, file_path: str) -> Dict[str, Any]:
|
| 78 |
-
full_path = os.path.join(self.base_dir, file_path)
|
| 79 |
-
if not os.path.exists(full_path):
|
| 80 |
-
return {"error": "Файл не найден"}
|
| 81 |
-
try:
|
| 82 |
-
stat = os.stat(full_path)
|
| 83 |
-
ext = os.path.splitext(file_path)[1].lower()
|
| 84 |
-
lang = self.SUPPORTED_EXTENSIONS.get(ext, 'unknown')
|
| 85 |
-
with open(full_path, 'r', encoding='utf-8', errors='ignore') as f:
|
| 86 |
-
content = f.read()
|
| 87 |
-
lines = content.count('\n') + 1
|
| 88 |
-
return {
|
| 89 |
-
"path": file_path,
|
| 90 |
-
"size": stat.st_size,
|
| 91 |
-
"lines": lines,
|
| 92 |
-
"language": lang,
|
| 93 |
-
"modified": datetime.fromtimestamp(stat.st_mtime).isoformat(),
|
| 94 |
-
"chars": len(content)
|
| 95 |
-
}
|
| 96 |
-
except Exception as e:
|
| 97 |
-
return {"error": str(e)}
|
| 98 |
-
|
| 99 |
-
def _match_pattern(self, filename: str, pattern: str) -> bool:
|
| 100 |
-
if pattern == "*":
|
| 101 |
-
return True
|
| 102 |
-
return fnmatch.fnmatch(filename, pattern)
|
| 103 |
-
|
| 104 |
-
FILE_MANAGER = FileManager()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|