-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdixie.py
More file actions
131 lines (109 loc) · 4.53 KB
/
dixie.py
File metadata and controls
131 lines (109 loc) · 4.53 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
'''
1. PUT dixie.py ONE STEP ABOVE THE FOLDER(s) WHERE YOU HAVE FILES TO COMPILE.
2. RUN dixie.py.
3. ENTER THE NAMES OF THE FILES YOU WANT TO SEARCH FOR, SEPARATED BY COMMAS (e.g., cont.html, jessie.py, random.js).
4. A TEXT FILE "file_contents.txt" WILL BE GENERATED THAT YOU CAN USE TO CONVERSE IN CODE WITH ChatGPT.
'''
import os
import tkinter as tk
from tkinter import messagebox
import sqlite3
# Connect to the SQLite database file
db_file_path = os.path.join(os.getcwd(), 'file_search.db')
conn = sqlite3.connect(db_file_path)
cursor = conn.cursor()
# Create the table if it doesn't exist
cursor.execute('''CREATE TABLE IF NOT EXISTS search_history
(folder_name TEXT, file_names TEXT)''')
conn.commit()
def search_files(folder_path, files):
"""
Searches for specified files within a given folder path.
Returns a dictionary containing the file names as keys and their contents as values.
"""
file_contents = {}
for root, _, filenames in os.walk(folder_path):
for file in filenames:
if file in files:
file_path = os.path.join(root, file)
try:
with open(file_path, 'r', encoding='cp1252') as f:
content = f.read()
file_contents[file] = content
except UnicodeDecodeError:
# Display an error dialog box
messagebox.showerror("Decoding Error", f"The file '{file}' cannot be decoded from cp1252 to Unicode and will be skipped.")
return file_contents
def write_contents_to_file(file_contents):
"""
Writes the contents of the files to a text file named 'file_contents.txt'.
Each file's content is preceded by the file name.
"""
file_path = os.path.join(os.getcwd(), 'file_contents.txt')
with open(file_path, 'w') as f:
f.write("***Codebase:")
for file, content in file_contents.items():
f.write(f"\n\n**{file}:-\n{content.strip()}")
def save_search_history(folder_name, file_names):
"""
Saves the folder name and file names to the search_history table in the SQLite database.
"""
cursor.execute("INSERT INTO search_history VALUES (?, ?)", (folder_name, file_names))
conn.commit()
def retrieve_last_search():
"""
Retrieves the last folder name and file names entered from the search_history table.
Returns a tuple of (folder_name, file_names) or None if no previous search found.
"""
cursor.execute("SELECT folder_name, file_names FROM search_history ORDER BY ROWID DESC LIMIT 1")
row = cursor.fetchone()
if row:
return row[0], row[1]
return None
def submit_form(event=None):
"""
Event handler for the form submission.
Retrieves the folder name, requested file names, performs the file search,
writes the contents to a file, saves the search history, and displays a success message.
"""
folder_name = folder_entry.get()
requested_files = entry.get()
requested_files = [file.strip() for file in requested_files.split(",")]
file_contents = search_files(os.getcwd(), requested_files)
write_contents_to_file(file_contents)
save_search_history(folder_name, ', '.join(requested_files))
messagebox.showinfo("Success", "File contents have been written to 'file_contents.txt'.")
window.destroy()
# Retrieve the last search history and pre-fill the form fields
last_search = retrieve_last_search()
if last_search:
folder_entry_text = last_search[0]
entry_text = last_search[1]
else:
folder_entry_text = ""
entry_text = ""
window = tk.Tk()
window.title("File Search")
window.geometry("600x200") # Adjusted the window size
# Label for folder name entry
folder_label = tk.Label(window, text="Enter the folder name to search in:")
folder_label.pack()
# Entry field for folder name
folder_entry = tk.Entry(window, width=60)
folder_entry.insert(0, folder_entry_text)
folder_entry.pack(pady=5)
# Label for file names entry
label = tk.Label(window, text="Enter the file names (separated by commas):")
label.pack()
# Entry field for file names
entry = tk.Entry(window, width=60)
entry.insert(0, entry_text)
entry.pack(pady=10)
# Submit button
submit_button = tk.Button(window, text="Submit", command=submit_form)
submit_button.pack()
# Bind the <Return> key event to the submit_form function
window.bind('<Return>', submit_form)
window.mainloop()
# Close the connection to the SQLite database
conn.close()