Advertisement
Najeebsk

SCRIPTSDATA2.0.pyw

Oct 30th, 2024 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.43 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import scrolledtext, filedialog, messagebox
  3. import sqlite3
  4. import os
  5. import subprocess
  6.  
  7. class ScriptDataManager:
  8.     def __init__(self, root):
  9.         self.root = root
  10.         self.root.title("Najeeb Advanced Script Data Manager")
  11.         self.root.geometry("1024x700")
  12.         self.root.config(bg="lightblue")
  13.        
  14.         # Font configuration
  15.         self.big_font = ("Arial", 12, "bold")
  16.  
  17.         # Database setup
  18.         self.db_name = "ScriptsData.db"
  19.         self.conn = sqlite3.connect(self.db_name)
  20.         self.cursor = self.conn.cursor()
  21.        
  22.         # Initialize the database
  23.         self.init_db()
  24.  
  25.         # Top Row: Search, Add, Show All in One Line
  26.         style_button = {"bg": "#4CAF50", "fg": "white", "width": 14, "padx": 2, "pady": 2}
  27.  
  28.         # Button frame at the top
  29.         self.button_frame = tk.Frame(self.root)
  30.         self.button_frame.pack(side=tk.TOP, fill=tk.X, padx=10, pady=10)
  31.  
  32.         # Buttons for actions in two rows
  33.         self.browse_button = tk.Button(self.button_frame, text="Browse Folder", font=self.big_font, command=self.browse_folder, **style_button)
  34.         self.browse_button.grid(row=0, column=0, padx=5, pady=5)
  35.  
  36.         self.browse_new_db_button = tk.Button(self.button_frame, text="Browse Database", font=self.big_font, command=self.browse_new_database, **style_button)
  37.         self.browse_new_db_button.grid(row=0, column=1, padx=5, pady=5)
  38.  
  39.         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)
  40.         self.new_script_button.grid(row=0, column=2, padx=5, pady=5)
  41.  
  42.         self.save_button = tk.Button(self.button_frame, text="Save", font=self.big_font, command=self.save_file, **style_button)
  43.         self.save_button.grid(row=0, column=3, padx=5, pady=5)
  44.  
  45.         self.delete_button = tk.Button(self.button_frame, text="Delete", font=self.big_font, command=self.delete_file, **style_button)
  46.         self.delete_button.grid(row=0, column=4, padx=5, pady=5)
  47.  
  48.         self.edit_button = tk.Button(self.button_frame, text="Edit", font=self.big_font, command=self.enable_editing, **style_button)
  49.         self.edit_button.grid(row=0, column=5, padx=5, pady=5)
  50.  
  51.         self.show_all_button = tk.Button(self.button_frame, text="Show All Scripts", font=self.big_font, command=self.show_all_scripts, **style_button)
  52.         self.show_all_button.grid(row=1, column=0, padx=5, pady=5)
  53.  
  54.         # Search Script field and button
  55.         self.search_entry = tk.Entry(self.button_frame, font=self.big_font, width=20)
  56.         self.search_entry.grid(row=1, column=1, padx=5, pady=5)
  57.         self.search_button = tk.Button(self.button_frame, text="Search Script", font=self.big_font, command=self.search_script, **style_button)
  58.         self.search_button.grid(row=1, column=2, padx=5, pady=5)
  59.  
  60.         # Run Cmd command field and button
  61.         self.command_entry = tk.Entry(self.button_frame, font=self.big_font, width=20)
  62.         self.command_entry.grid(row=1, column=3, padx=5, pady=5)
  63.         self.run_cmd_button = tk.Button(self.button_frame, text="Run Cmd", font=self.big_font, command=self.run_cmd, **style_button)
  64.         self.run_cmd_button.grid(row=1, column=4, padx=5, pady=5)
  65.  
  66.         # Copy to Clipboard Button
  67.         self.copy_button = tk.Button(self.button_frame, text="Copy to Clipboard", font=self.big_font, command=self.copy_to_clipboard, **style_button)
  68.         self.copy_button.grid(row=1, column=5, padx=5, pady=5)
  69.  
  70.         # File browsing frame with Listbox and scrollbar
  71.         self.file_frame = tk.Frame(self.root)
  72.         self.file_frame.pack(side=tk.LEFT, fill=tk.Y, padx=10, pady=10)
  73.  
  74.         self.scrollbar = tk.Scrollbar(self.file_frame)
  75.         self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  76.  
  77.         self.file_list = tk.Listbox(self.file_frame, selectmode=tk.SINGLE, width=30, height=25, font=self.big_font, yscrollcommand=self.scrollbar.set)
  78.         self.file_list.pack(side=tk.LEFT, fill=tk.Y)
  79.         self.scrollbar.config(command=self.file_list.yview)
  80.         self.file_list.bind("<<ListboxSelect>>", self.load_selected_file)
  81.  
  82.         # Big view data frame for content display/edit
  83.         self.data_frame = tk.Frame(self.root)
  84.         self.data_frame.pack(side=tk.RIGHT, expand=True, fill=tk.BOTH, padx=10, pady=10)
  85.  
  86.         self.text_viewer = scrolledtext.ScrolledText(self.data_frame, wrap=tk.WORD, state='disabled', font=self.big_font)
  87.         self.text_viewer.pack(fill=tk.BOTH, expand=True)
  88.  
  89.         # Initialize database and load scripts
  90.         self.show_all_scripts()
  91.  
  92.     def init_db(self):
  93.         """Initialize the database and create the Scripts table if it doesn't exist."""
  94.         self.cursor.execute("""
  95.        CREATE TABLE IF NOT EXISTS Scripts (
  96.            id INTEGER PRIMARY KEY AUTOINCREMENT,
  97.            name TEXT NOT NULL,
  98.            path TEXT NOT NULL,
  99.            content TEXT
  100.        )
  101.        """)
  102.         self.conn.commit()
  103.  
  104.     def browse_folder(self):
  105.         """Browse for a folder and save all files from it."""
  106.         folder_path = filedialog.askdirectory()
  107.         if folder_path:
  108.             # Get all files in the folder without filtering by extension
  109.             files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
  110.             for file_name in files:
  111.                 file_path = os.path.join(folder_path, file_name)
  112.                 try:
  113.                     # Read the file content with UTF-8 encoding
  114.                     with open(file_path, 'r', encoding='utf-8') as file:
  115.                         content = file.read()
  116.                     # Insert into the database
  117.                     self.cursor.execute("INSERT INTO Scripts (name, path, content) VALUES (?, ?, ?)", (file_name, file_path, content))
  118.                 except UnicodeDecodeError:
  119.                     messagebox.showerror("Encoding Error", f"Could not read file '{file_name}' due to encoding issues.")
  120.                     continue
  121.                 except Exception as e:
  122.                     messagebox.showerror("Error", f"Error reading file '{file_name}': {e}")
  123.                     continue
  124.             self.conn.commit()
  125.             self.show_all_scripts()
  126.  
  127.     def browse_new_database(self):
  128.         """Browse for a new database file."""
  129.         db_path = filedialog.askopenfilename(filetypes=[("SQLite Database", "*.db")])
  130.         if db_path:
  131.             self.conn.close()
  132.             self.conn = sqlite3.connect(db_path)
  133.             self.cursor = self.conn.cursor()
  134.             self.show_all_scripts()
  135.  
  136.     def add_existing_files_to_db(self):
  137.         """Browse and add selected files to the database."""
  138.         files = filedialog.askopenfilenames(filetypes=[("All Files", "*.*")])
  139.         if files:
  140.             for file_path in files:
  141.                 file_name = os.path.basename(file_path)
  142.                 try:
  143.                     with open(file_path, 'r', encoding='utf-8') as file:
  144.                         content = file.read()
  145.                     # Insert the file into the database
  146.                     self.cursor.execute("INSERT INTO Scripts (name, path, content) VALUES (?, ?, ?)", (file_name, file_path, content))
  147.                 except UnicodeDecodeError:
  148.                     messagebox.showerror("Encoding Error", f"Could not read file '{file_name}' due to encoding issues.")
  149.                     continue
  150.                 except Exception as e:
  151.                     messagebox.showerror("Error", f"Error reading file '{file_name}': {e}")
  152.                     continue
  153.             self.conn.commit()
  154.             self.show_all_scripts()
  155.  
  156.     def save_file(self):
  157.         """Save the currently displayed script to the database."""
  158.         if hasattr(self, 'current_file') and self.current_file:
  159.             script_content = self.text_viewer.get("1.0", tk.END)
  160.             self.cursor.execute("UPDATE Scripts SET content=? WHERE path=?", (script_content, self.current_file))
  161.             self.conn.commit()
  162.             messagebox.showinfo("Success", "Script saved successfully.")
  163.  
  164.     def delete_file(self):
  165.         """Delete the selected script from the database."""
  166.         selected = self.file_list.curselection()
  167.         if selected:
  168.             script_name = self.file_list.get(selected)
  169.             self.cursor.execute("DELETE FROM Scripts WHERE name=?", (script_name,))
  170.             self.conn.commit()
  171.             self.show_all_scripts()
  172.  
  173.     def enable_editing(self):
  174.         """Enable editing in the text viewer."""
  175.         self.text_viewer.config(state='normal')
  176.  
  177.     def show_all_scripts(self):
  178.         """Show all scripts from the database in the Listbox."""
  179.         self.file_list.delete(0, tk.END)
  180.         self.cursor.execute("SELECT name FROM Scripts")
  181.         for row in self.cursor.fetchall():
  182.             self.file_list.insert(tk.END, row[0])
  183.  
  184.     def search_script(self):
  185.         """Search for a script by name."""
  186.         search_term = self.search_entry.get()
  187.         self.file_list.delete(0, tk.END)
  188.         self.cursor.execute("SELECT name FROM Scripts WHERE name LIKE ?", ('%' + search_term + '%',))
  189.         for row in self.cursor.fetchall():
  190.             self.file_list.insert(tk.END, row[0])
  191.  
  192.     def run_cmd(self):
  193.         """Run a command from the command entry."""
  194.         command = self.command_entry.get()
  195.         subprocess.Popen(command, shell=True)
  196.  
  197.     def copy_to_clipboard(self):
  198.         """Copy the displayed content to the clipboard."""
  199.         content = self.text_viewer.get("1.0", tk.END)
  200.         self.root.clipboard_clear()
  201.         self.root.clipboard_append(content)
  202.         messagebox.showinfo("Success", "Content copied to clipboard.")
  203.  
  204.     def load_selected_file(self, event):
  205.         """Load and display the selected script's content in the text viewer."""
  206.         selected = self.file_list.curselection()
  207.         if selected:
  208.             script_name = self.file_list.get(selected)
  209.             self.cursor.execute("SELECT path, content FROM Scripts WHERE name=?", (script_name,))
  210.             result = self.cursor.fetchone()
  211.             if result:
  212.                 path, content = result
  213.                 self.current_file = path
  214.                 self.text_viewer.config(state='normal')
  215.                 self.text_viewer.delete("1.0", tk.END)
  216.                 self.text_viewer.insert(tk.END, content)
  217.                 self.text_viewer.config(state='disabled')
  218.  
  219. # Run the application
  220. root = tk.Tk()
  221. app = ScriptDataManager(root)
  222. root.mainloop()
  223.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement