-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_handler.py
More file actions
74 lines (61 loc) · 2.44 KB
/
file_handler.py
File metadata and controls
74 lines (61 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
def read_file(file_path):
"""Dosya içeriğini oku"""
try:
if not os.path.exists(file_path):
return None, "Dosya bulunamadi."
# Dosya uzantısını kontrol et
ext = os.path.splitext(file_path)[1].lower()
# Metin tabanlı dosyalar için
if ext in ['.py', '.txt', '.js', '.java', '.cpp', '.c', '.cs', '.json', '.xml', '.html', '.css', '.md']:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
return content, None
else:
return None, f"Desteklenmeyen dosya turu: {ext}"
except PermissionError:
return None, "Dosya erisim izni reddedildi."
except Exception as e:
return None, f"Dosya okuma hatasi: {str(e)}"
def write_file(file_path, content):
"""Dosyaya yaz"""
try:
# Dizini oluştur (yoksa)
directory = os.path.dirname(file_path)
if directory and not os.path.exists(directory):
os.makedirs(directory)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
return True, None
except Exception as e:
return False, f"Dosya yazma hatasi: {str(e)}"
def list_files(directory):
"""Dizindeki dosyaları listele"""
try:
if not os.path.exists(directory):
return None, "Dizin bulunamadi."
files = []
for item in os.listdir(directory):
full_path = os.path.join(directory, item)
if os.path.isfile(full_path):
files.append(item)
return files, None
except Exception as e:
return None, f"Dizin okuma hatasi: {str(e)}"
def extract_file_path(message):
"""Mesajdan dosya yolunu çıkar"""
keywords = ["konum:", "dosya:", "path:", "yol:"]
for keyword in keywords:
if keyword in message.lower():
parts = message.lower().split(keyword)
if len(parts) > 1:
path = parts[1].strip().split()[0]
return path
# Eğer \ veya / içeren bir yol varsa
words = message.split()
for word in words:
if '\\' in word or '/' in word:
# Dosya yolu gibi görünüyor
if any(word.endswith(ext) for ext in ['.py', '.txt', '.js', '.java', '.cpp', '.c', '.cs']):
return word
return None