Shadow Agency Code: Difference between revisions

From Chrysalis Archive
Jump to navigation Jump to search
mNo edit summary
m Don86326 moved page PromptEngineer Code to Shadow Agency Code
 
(No difference)

Latest revision as of 12:28, 29 May 2024

Shadow Agency ∞  Theory of a Personal KOS ☀  Structured-Key DataShadow  ☀  Shadow Agency Code ☀  AI Agency Workflow Schemes ☀  The Cognitive Mesh ☀  Interlocution Protocol

PromptEngineer Python Code Category
main.py ☀  prompt_engineer.py ☀  config_master.py ☀  README.md

main.py

©XenoEngineer 17:34, 12 May 2024 (UTC)

  • Hello-world app-framework
  • Persists size/position of window
  • Loads/saves text to edit window
  • Enpoint menu implemented
  • Browses to home page
  • Displays help file
import os
import tkinter as tk
from tkinter import filedialog
import webbrowser

from prompt_engineer.utils.ini_file import INIFile
from prompt_engineer.utils.database import DatabaseManager

class PromptEngineerApp(tk.Tk):
    def __init__(self, config_file):
        super().__init__()
        self.config = INIFile(config_file)
        
        self.data_folder = 'data'
        self.schema_file = 'db.schema.ini'
        
        self.db_manager = DatabaseManager(self.data_folder, self.schema_file)
        self.db_manager.connect()
        
        self.load_window_settings()
        self.create_widgets()

        self.menubar = tk.Menu(self)
        self.configure(menu=self.menubar)

        self.create_file_menu(self.menubar)
        self.create_help_menu(self.menubar)
        self.create_endpoints_menu(self.menubar)

        self.create_menu()  # Call create_menu() here
        self.file_path = None

    def browse_endpoint(self):
        pass
    
    def browse_table_of_endpoints(self):
        # Create a new window for browsing endpoints
        browse_window = tk.Tk()
        browse_window.title("Browse Endpoints")

        # Create a treeview widget to display the endpoints
        self.endpoint_treeview = ttk.Treeview(browse_window, height=10)

        # Define columns for the treeview
        self.endpoint_treeview['columns'] = ('ID', 'Name', 'URL', 'Method', 'Headers', 'Body')

        # Create headings for the columns
        self.endpoint_treeview.heading('#0', text='')
        self.endpoint_treeview.heading('ID', text='ID')
        self.endpoint_treeview.heading('Name', text='Name')
        self.endpoint_treeview.heading('URL', text='URL')
        self.endpoint_treeview.heading('Method', text='Method')
        self.endpoint_treeview.heading('Headers', text='Headers')
        self.endpoint_treeview.heading('Body', text='Body')

        # Pack the treeview widget into the window
        self.endpoint_treeview.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        # Populate the treeview with endpoint data
        self.populate_endpoint_treeview(self.endpoint_treeview)

        # Bind events to the treeview
        self.endpoint_treeview.bind('<Double-Button-1>', self.on_endpoint_double_click)

        # Start the browse window's mainloop
        browse_window.mainloop()
                        
    def load_window_settings(self):
        self.geometry(self.config.get('Window', 'geometry', '800x600'))
        self.title(self.config.get('Window', 'title', 'PromptEngineer'))

    def create_widgets(self):
        self.text_area = tk.Text(self)
        self.text_area.pack(fill=tk.BOTH, expand=True)

    def save_window_settings(self):
        self.config.set('Window', 'geometry', self.geometry())
        self.config.save()

    def destroy(self):
        self.save_window_settings()
        super().destroy()

    def create_menu(self):
        menubar = tk.Menu(self)
        self.configure(menu=menubar)

        self.create_file_menu(menubar)
        self.create_endpoints_menu(menubar)
        self.create_help_menu(menubar)
        
    def create_file_menu(self, menubar):
        file_menu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label="File", menu=file_menu)
        file_menu.add_command(label="Open", command=self.file_open)
        file_menu.add_command(label="Save", command=self.file_save)
        file_menu.add_command(label="Save As", command=self.file_save_as)
        file_menu.add_command(label="Close", command=self.file_close)
        file_menu.add_separator()
        file_menu.add_command(label="Exit", command=self.destroy)

    def create_endpoints_menu(self, menubar):
        endpoints_menu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Endpoints", menu=endpoints_menu)
        endpoints_menu.add_command(label="Browse", command=self.browse_endpoint) 
        endpoints_menu.add_command(label="Add", command=self.add_endpoint)
        endpoints_menu.add_command(label="Edit", command=self.edit_endpoint)
        endpoints_menu.add_command(label="Delete", command=self.delete_endpoint)
        endpoints_menu.add_command(label="Refresh", command=self.refresh_endpoint)

        
    def create_help_menu(self, menubar):
        help_menu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Help", menu=help_menu)
        help_menu.add_command(label="Home Site", command=self.open_home_site)
        help_menu.add_command(label="Help", command=self.open_help)
        
    def add_endpoint(self):
        pass
    
    def edit_endpoint(self):
        pass
    
    def delete_endpoint(self):
        # Implement the logic to delete an endpoint
        pass

    def refresh_endpoint(self):
        # Implement the logic to refresh an endpoint
        pass
                               
    def open_home_site(self):
        url = "https://groupkos.com/chrysalis/index.php/PromptEngineer"
        webbrowser.open(url)

    def open_help(self):
        file_path = "docs/README.md"
        try:
            with open(file_path, 'r') as file:
                content = file.read()
                self.text_area.delete(1.0, tk.END)
                self.text_area.insert(tk.END, content)
        except FileNotFoundError:
            self.text_area.delete(1.0, tk.END)
            self.text_area.insert(tk.END, "Help file not found.")

    def file_open(self):
        initial_dir = os.path.dirname(os.path.abspath(__file__))
        file_path = filedialog.askopenfilename(initialdir=initial_dir)
        if file_path:
            try:
                with open(file_path, 'r') as file:
                    content = file.read()
                    self.text_area.delete(1.0, tk.END)
                    self.text_area.insert(tk.END, content)
                    self.file_path = file_path
            except FileNotFoundError:
                self.text_area.delete(1.0, tk.END)
                self.text_area.insert(tk.END, "File not found.")
                
    def file_save(self):
        if self.file_path:
            content = self.text_area.get(1.0, tk.END)
            try:
                with open(self.file_path, 'w') as file:
                    file.write(content)
            except IOError:
                self.text_area.delete(1.0, tk.END)
                self.text_area.insert(tk.END, "Error occurred while saving the file.")
        else:
            self.file_save_as()

    def file_save_as(self):
        initial_dir = os.path.dirname(os.path.abspath(__file__))
        file_path = filedialog.asksaveasfilename(defaultextension='.txt', initialdir=initial_dir)
        if file_path:
            content = self.text_area.get(1.0, tk.END)
            try:
                with open(file_path, 'w') as file:
                    file.write(content)
                    self.file_path = file_path
            except IOError:
                self.text_area.delete(1.0, tk.END)
                self.text_area.insert(tk.END, "Error occurred while saving the file.")
                
    def file_close(self):
        self.file_path = None
        self.text_area.delete(1.0, tk.END)
    
    


def main():
    app = PromptEngineerApp('prompt_engineer/utils/config.ini')
    app.mainloop()


if __name__ == '__main__':
    main()