-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
360 lines (298 loc) · 13 KB
/
main.py
File metadata and controls
360 lines (298 loc) · 13 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import sqlite3
import csv
import json
import os
import shutil
from simple_term_menu import TerminalMenu
# Klasörleri oluşturmak için yardımcı fonksiyon
def create_folders():
folders = ["SQLData", "CSVData", "JSONData", "IsmetifyData", "Backup"]
for folder in folders:
if not os.path.exists(folder):
os.makedirs(folder)
# SQLite veritabanı oluşturma
def create_sqlite_database():
conn = sqlite3.connect("SQLData/mydatabase.db")
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS contacts
(id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name TEXT,
last_name TEXT,
phone_number TEXT,
email TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP)''')
conn.commit()
conn.close()
# CSV, JSON ve İsmetify dosyalarını oluşturma
def create_data_files():
files = {
"CSVData/contacts.csv": ["first_name", "last_name", "phone_number", "email"],
"JSONData/contacts.json": [],
"IsmetifyData/contacts.txt": [],
}
for file, header in files.items():
if not os.path.exists(file):
with open(file, 'w', newline='') as f:
if header:
writer = csv.writer(f)
writer.writerow(header)
# SQLite veritabanına kişi ekleme fonksiyonu
def add_contact_to_sqlite(first_name, last_name, phone_number, email):
conn = sqlite3.connect("SQLData/mydatabase.db")
cursor = conn.cursor()
cursor.execute("SELECT id FROM contacts WHERE first_name = ? AND email = ?", (first_name, email))
existing_contact = cursor.fetchone()
if existing_contact:
print("A contact with the same name and email already exists.")
else:
cursor.execute("INSERT INTO contacts (first_name, last_name, phone_number, email) VALUES (?, ?, ?, ?)",
(first_name, last_name, phone_number, email))
conn.commit()
conn.close()
print("Contact added successfully to SQLite.")
# CSV verilerine kişi ekleme fonksiyonu
def add_contact_to_csv(first_name, last_name, phone_number, email):
new_contact = [first_name, last_name, phone_number, email]
with open("CSVData/contacts.csv", 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow(new_contact)
print("Contact added successfully to CSV.")
# JSON verilerine kişi ekleme fonksiyonu
def add_contact_to_json(first_name, last_name, phone_number, email):
new_contact_dict = {"first_name": first_name, "last_name": last_name, "phone_number": phone_number, "email": email}
json_file = "JSONData/contacts.json"
if not os.path.exists(json_file):
with open(json_file, 'w') as file:
json.dump([new_contact_dict], file, indent=4)
else:
with open(json_file, 'r') as file:
try:
json_data = json.load(file)
except json.decoder.JSONDecodeError:
json_data = []
json_data.append(new_contact_dict)
with open(json_file, 'w') as file:
json.dump(json_data, file, indent=4)
print("Contact added successfully to JSON.")
# İsmetify verilerine kişi ekleme fonksiyonu
def add_contact_to_ismetify(first_name, last_name, phone_number, email):
new_contact_str = "|".join([first_name, last_name, phone_number, email])
with open("IsmetifyData/contacts.txt", 'a') as file:
file.write(new_contact_str + '\n')
print("Contact added successfully to İsmetify.")
# Kişi ekleme işlemlerini birleştiren fonksiyon
def add_contact_to_all_sources(first_name, last_name, phone_number, email):
add_contact_to_sqlite(first_name, last_name, phone_number, email)
add_contact_to_csv(first_name, last_name, phone_number, email)
add_contact_to_json(first_name, last_name, phone_number, email)
add_contact_to_ismetify(first_name, last_name, phone_number, email)
backup_all()
# Ana menü içindeki fonksiyonlar
def list_contacts_menu():
contacts = list_contacts()
if not contacts:
print("Listelenecek kişi bulunamadı.")
else:
for contact in contacts:
print("Ad: {}, Soyad: {}, Telefon: {}, E-posta: {}, Oluşturulma Tarihi: {}, Güncellenme Tarihi: {}"
.format(contact[1], contact[2], contact[3], contact[4], contact[5], contact[6]))
# Kişileri Listeleme işlemi
def list_contacts():
conn = sqlite3.connect("SQLData/mydatabase.db")
cursor = conn.cursor()
# Varsayılan olarak, ad ve soyada göre sırala
#cursor.execute("SELECT * FROM contacts ORDER BY first_name, last_name")
cursor.execute("SELECT *, created_at, updated_at FROM contacts ORDER BY first_name, last_name")
contacts = cursor.fetchall()
conn.close()
return contacts
# SQLite veritabanından kişi silme fonksiyonu
def delete_contact_from_sqlite(first_name, last_name, email):
conn = sqlite3.connect("SQLData/mydatabase.db")
cursor = conn.cursor()
cursor.execute("SELECT id FROM contacts WHERE first_name = ? AND last_name = ? AND email = ?", (first_name, last_name, email))
existing_contact = cursor.fetchone()
if existing_contact:
contact_id = existing_contact[0]
cursor.execute("DELETE FROM contacts WHERE id = ?", (contact_id,))
conn.commit()
conn.close()
print("Contact deleted successfully from SQLite.")
else:
print("Contact not found in SQLite.")
# CSV verilerinden kişi silme fonksiyonu
def delete_contact_from_csv(first_name, last_name, email):
with open("CSVData/contacts.csv", 'r') as file:
csv_data = list(csv.reader(file))
deleted = False
for row in csv_data:
if row[0] == first_name and row[1] == last_name and row[3] == email:
csv_data.remove(row)
deleted = True
if deleted:
with open("CSVData/contacts.csv", 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(csv_data)
print("Contact deleted successfully from CSV.")
else:
print("Contact not found in CSV.")
# JSON verilerinden kişi silme fonksiyonu
def delete_contact_from_json(first_name, last_name, email):
json_file = "JSONData/contacts.json"
if not os.path.exists(json_file):
print("Contact not found in JSON.")
return
with open(json_file, 'r') as file:
json_data = json.load(file)
deleted = False
for contact in json_data:
if contact.get("first_name") == first_name and contact.get("last_name") == last_name and contact.get("email") == email:
json_data.remove(contact)
deleted = True
if deleted:
with open(json_file, 'w') as file:
json.dump(json_data, file, indent=4)
print("Contact deleted successfully from JSON.")
else:
print("Contact not found in JSON.")
# İsmetify verilerinden kişi silme fonksiyonu
def delete_contact_from_ismetify(first_name, last_name, email):
ismetify_file = "IsmetifyData/contacts.txt"
if not os.path.exists(ismetify_file):
print("Contact not found in İsmetify.")
return
with open(ismetify_file, 'r') as file:
lines = file.readlines()
deleted = False
with open(ismetify_file, 'w') as file:
for line in lines:
contact_data = line.strip().split('|')
if contact_data[0] == first_name and contact_data[1] == last_name and contact_data[3] == email:
deleted = True
else:
file.write(line)
if deleted:
print("Contact deleted successfully from İsmetify.")
else:
print("Contact not found in İsmetify.")
# Kişiyi tüm veri kaynaklarından silen fonksiyon
def delete_contact_from_all_sources(first_name, last_name, email):
delete_contact_from_sqlite(first_name, last_name, email)
delete_contact_from_csv(first_name, last_name, email)
delete_contact_from_json(first_name, last_name, email)
delete_contact_from_ismetify(first_name, last_name, email)
backup_all()
# Ana menü içindeki fonksiyonlar
def edit_contact_menu():
contacts = list_contacts()
if not contacts:
print("Düzenlenecek kişi bulunamadı.")
else:
for index, contact in enumerate(contacts):
print("{}. Ad: {}, Soyad: {}, Telefon: {}, E-posta: {}".format(index, contact[1], contact[2], contact[3], contact[4]))
contact_index = input("Düzenlemek istediğiniz kişinin sıra numarasını girin (q: İptal): ")
if contact_index == 'q':
print("İptal edildi.")
elif contact_index.isdigit():
contact_index = int(contact_index)
if 0 <= contact_index < len(contacts):
contact = contacts[contact_index]
first_name, last_name, phone_number, email = contact[1], contact[2], contact[3], contact[4]
# Düzenlenecek kişinin bilgilerini göster
print("Düzenleniyor: ")
print("Ad: {}".format(first_name))
print("Soyad: {}".format(last_name))
print("Telefon Numarası: {}".format(phone_number))
print("E-posta: {}".format(email))
# Yeni bilgileri al
new_first_name = input("Yeni Ad: ")
new_last_name = input("Yeni Soyad: ")
new_phone_number = input("Yeni Telefon Numarası: ")
new_email = input("Yeni E-posta: ")
# Kişiyi güncelle
delete_contact_from_all_sources(first_name, last_name, email)
add_contact_to_all_sources(new_first_name, new_last_name, new_phone_number, new_email)
print("Kişi başarıyla güncellendi.")
else:
print("Geçersiz bir sıra numarası girdiniz.")
else:
print("Geçersiz giriş. Lütfen sıra numarasını girin veya 'q' ile iptal edin.")
backup_all()
# Yedekleme işlemleri için yardımcı fonksiyonlar
def backup_sql_data():
shutil.copy("SQLData/mydatabase.db", "Backup/mydatabase_backup.db")
def backup_csv_data():
shutil.copy("CSVData/contacts.csv", "Backup/contacts_backup.csv")
def backup_json_data():
shutil.copy("JSONData/contacts.json", "Backup/contacts_backup.json")
def backup_ismetify_data():
shutil.copy("IsmetifyData/contacts.txt", "Backup/contacts_backup.txt")
def backup_all():
backup_sql_data()
backup_csv_data()
backup_json_data()
backup_ismetify_data()
# Yedekten geri yükleme işlemleri için yardımcı fonksiyonlar
def restore_sql_data():
shutil.copy("Backup/mydatabase_backup.db", "SQLData/mydatabase.db")
def restore_csv_data():
shutil.copy("Backup/contacts_backup.csv", "CSVData/contacts.csv")
def restore_json_data():
shutil.copy("Backup/contacts_backup.json", "JSONData/contacts.json")
def restore_ismetify_data():
shutil.copy("Backup/contacts_backup.txt", "IsmetifyData/contacts.txt")
# Yedekten geri yükleme işlemleri için menü
def restore_backup():
print("Yedekten Geri Yükleme Menüsü")
print("1. SQL Verilerini Geri Yükle")
print("2. CSV Verilerini Geri Yükle")
print("3. JSON Verilerini Geri Yükle")
print("4. İsmetify Verilerini Geri Yükle")
choice = input("Geri yüklemek istediğiniz veri türünün numarasını girin: ")
if choice == '1':
restore_sql_data()
print("SQL verileri başarıyla geri yüklendi.")
elif choice == '2':
restore_csv_data()
print("CSV verileri başarıyla geri yüklendi.")
elif choice == '3':
restore_json_data()
print("JSON verileri başarıyla geri yüklendi.")
elif choice == '4':
restore_ismetify_data()
print("İsmetify verileri başarıyla geri yüklendi.")
else:
print("Geçersiz seçenek. Lütfen tekrar deneyin.")
def main_menu():
menu_title = "Ana Menü"
menu_items = ["Kişi Ekle", "Kişi Sil", "Kişileri Listele", "Kişi Düzenle", "Yedekten Geri Yükle", "Çıkış"]
menu = TerminalMenu(menu_items, title=menu_title)
while True:
selected_index = menu.show()
if selected_index == 0:
first_name = input("Ad: ")
last_name = input("Soyad: ")
phone_number = input("Telefon Numarası: ")
email = input("E-posta: ")
add_contact_to_all_sources(first_name, last_name, phone_number, email)
elif selected_index == 1:
first_name = input("Ad: ")
last_name = input("Soyad: ")
email = input("E-posta: ")
delete_contact_from_all_sources(first_name, last_name, email)
elif selected_index == 2:
list_contacts_menu()
elif selected_index == 3:
edit_contact_menu()
elif selected_index == 4:
restore_backup()
pass
elif selected_index == 5:
print("Çıkış yapılıyor...")
break
if __name__ == "__main__":
create_folders()
create_sqlite_database()
create_data_files()
main_menu()