Advertisement
Najeebsk

CMDER-INI.py

Feb 25th, 2024 (edited)
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.10 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk
  3. import subprocess
  4. import configparser
  5. import os
  6.  
  7.  
  8. class AliasButtonExample(tk.Tk):
  9.     def __init__(self):
  10.         super().__init__()
  11.         self.geometry('1000x675')
  12.         self.config(bg='green')
  13.         self.title("Najeeb CMD ALIASES COMMANDS RUNNER")
  14.  
  15.         self.console_frame = ConsoleFrame(self)
  16.         self.console_frame.grid(row=0, column=0, columnspan=3, padx=5, pady=5, sticky="nsew")
  17.  
  18.         self.aliases = {}
  19.  
  20.         self.create_widgets()
  21.  
  22.     def create_widgets(self):
  23.         style = ttk.Style()
  24.         style.configure("TButton", foreground="blue", background="lightgray", font=('Arial', 10, 'bold'))
  25.         style.configure("TLabel", foreground="darkblue", background="lightgray", font=('Arial', 10))
  26.  
  27.         tk.Label(self, text="Command:").grid(row=1, column=0, padx=5, pady=5)
  28.         tk.Label(self, text="Najeeb Aliases:").grid(row=3, column=0, sticky="n", padx=5, pady=5)
  29.  
  30.         self.command_entry = tk.Entry(self, width=100)
  31.         self.command_entry.grid(row=1, column=1, padx=5, pady=5)
  32.  
  33.         self.run_button = ttk.Button(self, text="Run Command", command=self.run_command, style="TButton")
  34.         self.run_button.grid(row=1, column=2, rowspan=1, padx=5, pady=5, sticky="nsew")
  35.  
  36.         self.aliases_frame = tk.Frame(self)
  37.         self.aliases_frame.grid(row=4, column=0, columnspan=3, padx=5, pady=5, sticky="nsew")
  38.  
  39.         self.aliases_text = tk.Text(self.aliases_frame, wrap="word", state="normal", height=10, width=106, font=('Arial', 12))
  40.         self.aliases_text.grid(row=0, column=0, padx=5, pady=5, sticky="nsew")
  41.  
  42.         self.aliases_scrollbar = tk.Scrollbar(self.aliases_frame, orient="vertical", command=self.aliases_text.yview)
  43.         self.aliases_scrollbar.grid(row=0, column=1, sticky="ns")
  44.         self.aliases_text.config(yscrollcommand=self.aliases_scrollbar.set)
  45.  
  46.         self.search_label = tk.Label(self, text="Search Aliases:")
  47.         self.search_label.grid(row=5, column=0, padx=5, pady=5, sticky="e")
  48.  
  49.         self.search_entry = tk.Entry(self, width=100)
  50.         self.search_entry.grid(row=5, column=1, padx=5, pady=5)
  51.  
  52.         self.search_button = ttk.Button(self, text="Search", command=self.search_aliases, style="TButton")
  53.         self.search_button.grid(row=5, column=2, padx=5, pady=5)
  54.  
  55.         self.save_button = ttk.Button(self, text="Save Console Text", command=self.save_console_text, style="TButton")
  56.         self.save_button.grid(row=3, column=1, padx=5, pady=5)
  57.  
  58.         self.grid_columnconfigure(1, weight=1)
  59.  
  60.         self.read_aliases()
  61.  
  62.     def run_command(self):
  63.         command = self.command_entry.get()
  64.         process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  65.         stdout, stderr = process.communicate()
  66.         output = stdout.decode()
  67.         error = stderr.decode()
  68.         self.console_frame.console_window.append_text(output)
  69.         if error:
  70.             self.console_frame.console_window.append_text(error, tag="error")
  71.  
  72.     def read_aliases(self):
  73.         try:
  74.             config = configparser.ConfigParser()
  75.             config.read("Aliases.ini")
  76.             for section in config.sections():
  77.                 for option in config.options(section):
  78.                     try:
  79.                         command = config.get(section, option)
  80.                         alias = option
  81.                         # Custom parsing for Windows CMD syntax
  82.                         command = self.parse_cmd_command(command)
  83.                         self.aliases[alias] = command
  84.                         self.aliases_text.insert("end", f"{alias}: {command}\n")
  85.                     except configparser.InterpolationSyntaxError as e:
  86.                         print(f"Error reading alias '{option}' in section '{section}': {e}")
  87.         except configparser.Error as e:
  88.             print(f"Error reading configuration file: {e}")
  89.  
  90.     def parse_cmd_command(self, command):
  91.         # Replace %var% with os.environ['var']
  92.         command = os.path.expandvars(command)
  93.         # Replace %var:~x,y% with proper slicing
  94.         command = self.parse_cmd_substring(command)
  95.         return command
  96.  
  97.     def parse_cmd_substring(self, command):
  98.         # Implement custom logic to handle substring expansion
  99.         # For simplicity, I'll provide a basic implementation for date and time expansion
  100.  
  101.         # Date expansion
  102.         command = command.replace('%date%', self.get_current_date())
  103.  
  104.         # Time expansion
  105.         command = command.replace('%time%', self.get_current_time())
  106.  
  107.         return command
  108.  
  109.     def get_current_date(self):
  110.         return os.popen('echo %date%').read().strip()
  111.  
  112.     def get_current_time(self):
  113.         return os.popen('echo %time%').read().strip()
  114.  
  115.     def search_aliases(self):
  116.         query = self.search_entry.get().strip().lower()
  117.         self.aliases_text.delete('1.0', tk.END)
  118.         if query:
  119.             for alias, command in self.aliases.items():
  120.                 if query in alias.lower() or query in command.lower():
  121.                     self.aliases_text.insert(tk.END, f"{alias}: {command}\n")
  122.  
  123.     def save_console_text(self):
  124.         text_to_save = self.console_frame.console_window.get_text()
  125.         with open("console_output.txt", "w") as file:
  126.             file.write(text_to_save)
  127.  
  128.  
  129. class ConsoleFrame(tk.Frame):
  130.     def __init__(self, parent):
  131.         super().__init__(parent)
  132.         self.console_window = ConsoleWindow(self)
  133.         self.console_window.pack(expand=True, fill="both")
  134.         self.clear_button = ttk.Button(self, text="Clear Console", command=self.clear_console)
  135.         self.clear_button.pack()
  136.  
  137.     def clear_console(self):
  138.         self.console_window.clear_text()
  139.  
  140.  
  141. class ConsoleWindow(tk.Frame):
  142.     def __init__(self, parent):
  143.         super().__init__(parent)
  144.         self.text_area = tk.Text(self, wrap="word", state="normal", height=15, width=100, font=('Arial', 12))
  145.         self.text_area.pack(expand=True, fill="both")
  146.         self.scrollbar = tk.Scrollbar(self, command=self.text_area.yview)
  147.         self.scrollbar.pack(side="right", fill="y")
  148.         self.text_area.config(yscrollcommand=self.scrollbar.set)
  149.  
  150.         # Bind the text widget to automatically copy selected text to the clipboard
  151.         self.text_area.bind("<Control-c>", self.copy_to_clipboard)
  152.  
  153.     def append_text(self, text, tag=None):
  154.         self.text_area.config(state="normal")
  155.         if tag:
  156.             self.text_area.insert("end", text, tag)
  157.         else:
  158.             self.text_area.insert("end", text)
  159.         self.text_area.config(state="disabled")
  160.         self.text_area.see("end")
  161.  
  162.     def clear_text(self):
  163.         self.text_area.config(state="normal")
  164.         self.text_area.delete("1.0", "end")
  165.         self.text_area.config(state="disabled")
  166.  
  167.     def get_text(self):
  168.         return self.text_area.get("1.0", "end-1c")
  169.  
  170.     def copy_to_clipboard(self, event=None):
  171.         self.clipboard_clear()
  172.         text = self.text_area.get(tk.SEL_FIRST, tk.SEL_LAST)
  173.         self.clipboard_append(text)
  174.  
  175.  
  176. if __name__ == "__main__":
  177.     app = AliasButtonExample()
  178.     app.mainloop()
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement