Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import scrolledtext, filedialog, messagebox
- import sqlite3
- import os
- import subprocess
- class ScriptDataManager:
- def __init__(self, root):
- self.root = root
- self.root.title("Najeeb Advanced Script Data Manager")
- self.root.geometry("1024x700")
- self.root.config(bg="lightblue")
- # Font configuration
- self.big_font = ("Arial", 12, "bold")
- # Database setup
- self.db_name = "ScriptsData.db"
- self.conn = sqlite3.connect(self.db_name)
- self.cursor = self.conn.cursor()
- # Initialize the database
- self.init_db()
- # Top Row: Search, Add, Show All in One Line
- style_button = {"bg": "#4CAF50", "fg": "white", "width": 14, "padx": 2, "pady": 2}
- # Button frame at the top
- self.button_frame = tk.Frame(self.root)
- self.button_frame.pack(side=tk.TOP, fill=tk.X, padx=10, pady=10)
- # Buttons for actions in two rows
- self.browse_button = tk.Button(self.button_frame, text="Browse Folder", font=self.big_font, command=self.browse_folder, **style_button)
- self.browse_button.grid(row=0, column=0, padx=5, pady=5)
- self.browse_new_db_button = tk.Button(self.button_frame, text="Browse Database", font=self.big_font, command=self.browse_new_database, **style_button)
- self.browse_new_db_button.grid(row=0, column=1, padx=5, pady=5)
- self.new_script_button = tk.Button(self.button_frame, text="Add New File", font=self.big_font, command=self.add_existing_files_to_db, **style_button)
- self.new_script_button.grid(row=0, column=2, padx=5, pady=5)
- self.save_button = tk.Button(self.button_frame, text="Save", font=self.big_font, command=self.save_file, **style_button)
- self.save_button.grid(row=0, column=3, padx=5, pady=5)
- self.delete_button = tk.Button(self.button_frame, text="Delete", font=self.big_font, command=self.delete_file, **style_button)
- self.delete_button.grid(row=0, column=4, padx=5, pady=5)
- self.edit_button = tk.Button(self.button_frame, text="Edit", font=self.big_font, command=self.enable_editing, **style_button)
- self.edit_button.grid(row=0, column=5, padx=5, pady=5)
- self.show_all_button = tk.Button(self.button_frame, text="Show All Scripts", font=self.big_font, command=self.show_all_scripts, **style_button)
- self.show_all_button.grid(row=1, column=0, padx=5, pady=5)
- # Search Script field and button
- self.search_entry = tk.Entry(self.button_frame, font=self.big_font, width=20)
- self.search_entry.grid(row=1, column=1, padx=5, pady=5)
- self.search_button = tk.Button(self.button_frame, text="Search Script", font=self.big_font, command=self.search_script, **style_button)
- self.search_button.grid(row=1, column=2, padx=5, pady=5)
- # Run Cmd command field and button
- self.command_entry = tk.Entry(self.button_frame, font=self.big_font, width=20)
- self.command_entry.grid(row=1, column=3, padx=5, pady=5)
- self.run_cmd_button = tk.Button(self.button_frame, text="Run Cmd", font=self.big_font, command=self.run_cmd, **style_button)
- self.run_cmd_button.grid(row=1, column=4, padx=5, pady=5)
- # Copy to Clipboard Button
- self.copy_button = tk.Button(self.button_frame, text="Copy to Clipboard", font=self.big_font, command=self.copy_to_clipboard, **style_button)
- self.copy_button.grid(row=1, column=5, padx=5, pady=5)
- # File browsing frame with Listbox and scrollbar
- self.file_frame = tk.Frame(self.root)
- self.file_frame.pack(side=tk.LEFT, fill=tk.Y, padx=10, pady=10)
- self.scrollbar = tk.Scrollbar(self.file_frame)
- self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
- self.file_list = tk.Listbox(self.file_frame, selectmode=tk.SINGLE, width=30, height=25, font=self.big_font, yscrollcommand=self.scrollbar.set)
- self.file_list.pack(side=tk.LEFT, fill=tk.Y)
- self.scrollbar.config(command=self.file_list.yview)
- self.file_list.bind("<<ListboxSelect>>", self.load_selected_file)
- # Big view data frame for content display/edit
- self.data_frame = tk.Frame(self.root)
- self.data_frame.pack(side=tk.RIGHT, expand=True, fill=tk.BOTH, padx=10, pady=10)
- self.text_viewer = scrolledtext.ScrolledText(self.data_frame, wrap=tk.WORD, state='disabled', font=self.big_font)
- self.text_viewer.pack(fill=tk.BOTH, expand=True)
- # Initialize database and load scripts
- self.show_all_scripts()
- def init_db(self):
- """Initialize the database and create the Scripts table if it doesn't exist."""
- self.cursor.execute("""
- CREATE TABLE IF NOT EXISTS Scripts (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- name TEXT NOT NULL,
- path TEXT NOT NULL,
- content TEXT
- )
- """)
- self.conn.commit()
- def browse_folder(self):
- """Browse for a folder and save all files from it."""
- folder_path = filedialog.askdirectory()
- if folder_path:
- # Get all files in the folder without filtering by extension
- files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
- for file_name in files:
- file_path = os.path.join(folder_path, file_name)
- try:
- # Read the file content with UTF-8 encoding
- with open(file_path, 'r', encoding='utf-8') as file:
- content = file.read()
- # Insert into the database
- self.cursor.execute("INSERT INTO Scripts (name, path, content) VALUES (?, ?, ?)", (file_name, file_path, content))
- except UnicodeDecodeError:
- messagebox.showerror("Encoding Error", f"Could not read file '{file_name}' due to encoding issues.")
- continue
- except Exception as e:
- messagebox.showerror("Error", f"Error reading file '{file_name}': {e}")
- continue
- self.conn.commit()
- self.show_all_scripts()
- def browse_new_database(self):
- """Browse for a new database file."""
- db_path = filedialog.askopenfilename(filetypes=[("SQLite Database", "*.db")])
- if db_path:
- self.conn.close()
- self.conn = sqlite3.connect(db_path)
- self.cursor = self.conn.cursor()
- self.show_all_scripts()
- def add_existing_files_to_db(self):
- """Browse and add selected files to the database."""
- files = filedialog.askopenfilenames(filetypes=[("All Files", "*.*")])
- if files:
- for file_path in files:
- file_name = os.path.basename(file_path)
- try:
- with open(file_path, 'r', encoding='utf-8') as file:
- content = file.read()
- # Insert the file into the database
- self.cursor.execute("INSERT INTO Scripts (name, path, content) VALUES (?, ?, ?)", (file_name, file_path, content))
- except UnicodeDecodeError:
- messagebox.showerror("Encoding Error", f"Could not read file '{file_name}' due to encoding issues.")
- continue
- except Exception as e:
- messagebox.showerror("Error", f"Error reading file '{file_name}': {e}")
- continue
- self.conn.commit()
- self.show_all_scripts()
- def save_file(self):
- """Save the currently displayed script to the database."""
- if hasattr(self, 'current_file') and self.current_file:
- script_content = self.text_viewer.get("1.0", tk.END)
- self.cursor.execute("UPDATE Scripts SET content=? WHERE path=?", (script_content, self.current_file))
- self.conn.commit()
- messagebox.showinfo("Success", "Script saved successfully.")
- def delete_file(self):
- """Delete the selected script from the database."""
- selected = self.file_list.curselection()
- if selected:
- script_name = self.file_list.get(selected)
- self.cursor.execute("DELETE FROM Scripts WHERE name=?", (script_name,))
- self.conn.commit()
- self.show_all_scripts()
- def enable_editing(self):
- """Enable editing in the text viewer."""
- self.text_viewer.config(state='normal')
- def show_all_scripts(self):
- """Show all scripts from the database in the Listbox."""
- self.file_list.delete(0, tk.END)
- self.cursor.execute("SELECT name FROM Scripts")
- for row in self.cursor.fetchall():
- self.file_list.insert(tk.END, row[0])
- def search_script(self):
- """Search for a script by name."""
- search_term = self.search_entry.get()
- self.file_list.delete(0, tk.END)
- self.cursor.execute("SELECT name FROM Scripts WHERE name LIKE ?", ('%' + search_term + '%',))
- for row in self.cursor.fetchall():
- self.file_list.insert(tk.END, row[0])
- def run_cmd(self):
- """Run a command from the command entry."""
- command = self.command_entry.get()
- subprocess.Popen(command, shell=True)
- def copy_to_clipboard(self):
- """Copy the displayed content to the clipboard."""
- content = self.text_viewer.get("1.0", tk.END)
- self.root.clipboard_clear()
- self.root.clipboard_append(content)
- messagebox.showinfo("Success", "Content copied to clipboard.")
- def load_selected_file(self, event):
- """Load and display the selected script's content in the text viewer."""
- selected = self.file_list.curselection()
- if selected:
- script_name = self.file_list.get(selected)
- self.cursor.execute("SELECT path, content FROM Scripts WHERE name=?", (script_name,))
- result = self.cursor.fetchone()
- if result:
- path, content = result
- self.current_file = path
- self.text_viewer.config(state='normal')
- self.text_viewer.delete("1.0", tk.END)
- self.text_viewer.insert(tk.END, content)
- self.text_viewer.config(state='disabled')
- # Run the application
- root = tk.Tk()
- app = ScriptDataManager(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement