latest pushes
This commit is contained in:
0
gui/__init__.py
Normal file
0
gui/__init__.py
Normal file
142
gui/bellos_main_window.py
Normal file
142
gui/bellos_main_window.py
Normal file
@@ -0,0 +1,142 @@
|
||||
# Copyright (C) 2024 Bellande Application Interoperability Xenogen Research Innovation Center, Ronaldson Bellande
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
from PyQt6.QtWidgets import (QMainWindow, QDockWidget, QFileDialog, QMessageBox, QTabWidget)
|
||||
from PyQt6.QtCore import Qt
|
||||
from PyQt6.QtGui import QAction, QIcon
|
||||
from script_editor import ScriptEditor
|
||||
from file_explorer import FileExplorer
|
||||
from terminal import Terminal
|
||||
from project_system import ProjectSettings
|
||||
from script_manager import ScriptManager
|
||||
|
||||
class BellosMainWindow(QMainWindow):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.script_manager = ScriptManager()
|
||||
self.init_ui()
|
||||
|
||||
def init_ui(self):
|
||||
self.setWindowTitle('Bellos Application Script Manager')
|
||||
self.setGeometry(100, 100, 1200, 800)
|
||||
|
||||
# Create central widget with tab support
|
||||
self.central_widget = QTabWidget()
|
||||
self.setCentralWidget(self.central_widget)
|
||||
|
||||
# Create main editor tab
|
||||
self.editor = ScriptEditor()
|
||||
self.central_widget.addTab(self.editor, "Script Editor")
|
||||
|
||||
# Create and configure dock widgets
|
||||
self.setup_file_explorer()
|
||||
self.setup_terminal()
|
||||
self.setup_menubar()
|
||||
|
||||
# Project settings tab
|
||||
self.project_settings = ProjectSettings()
|
||||
self.central_widget.addTab(self.project_settings, "Project Settings")
|
||||
|
||||
def setup_file_explorer(self):
|
||||
self.file_explorer = FileExplorer()
|
||||
file_dock = QDockWidget("File Explorer", self)
|
||||
file_dock.setWidget(self.file_explorer)
|
||||
self.addDockWidget(Qt.DockWidgetArea.LeftDockWidgetArea, file_dock)
|
||||
|
||||
# Connect file explorer signals
|
||||
self.file_explorer.file_selected.connect(self.open_file)
|
||||
|
||||
def setup_terminal(self):
|
||||
self.terminal = Terminal()
|
||||
terminal_dock = QDockWidget("Terminal", self)
|
||||
terminal_dock.setWidget(self.terminal)
|
||||
self.addDockWidget(Qt.DockWidgetArea.BottomDockWidgetArea, terminal_dock)
|
||||
|
||||
def setup_menubar(self):
|
||||
menubar = self.menuBar()
|
||||
|
||||
# File menu
|
||||
file_menu = menubar.addMenu("File")
|
||||
|
||||
new_action = QAction("New", self)
|
||||
new_action.triggered.connect(self.new_file)
|
||||
file_menu.addAction(new_action)
|
||||
|
||||
open_action = QAction("Open", self)
|
||||
open_action.triggered.connect(self.open_file_dialog)
|
||||
file_menu.addAction(open_action)
|
||||
|
||||
save_action = QAction("Save", self)
|
||||
save_action.triggered.connect(self.save_file)
|
||||
file_menu.addAction(save_action)
|
||||
|
||||
# Edit menu
|
||||
edit_menu = menubar.addMenu("Edit")
|
||||
|
||||
# Add edit actions (undo, redo, cut, copy, paste)
|
||||
undo_action = QAction("Undo", self)
|
||||
undo_action.triggered.connect(self.editor.undo)
|
||||
edit_menu.addAction(undo_action)
|
||||
|
||||
redo_action = QAction("Redo", self)
|
||||
redo_action.triggered.connect(self.editor.redo)
|
||||
edit_menu.addAction(redo_action)
|
||||
|
||||
# Run menu
|
||||
run_menu = menubar.addMenu("Run")
|
||||
|
||||
run_action = QAction("Run Script", self)
|
||||
run_action.triggered.connect(self.run_script)
|
||||
run_menu.addAction(run_action)
|
||||
|
||||
def new_file(self):
|
||||
self.editor.clear()
|
||||
self.current_file = None
|
||||
|
||||
def open_file_dialog(self):
|
||||
file_name, _ = QFileDialog.getOpenFileName(self, "Open File", "", "Bellos Scripts (*.bellos);;All Files (*)")
|
||||
if file_name:
|
||||
self.open_file(file_name)
|
||||
|
||||
def open_file(self, file_name):
|
||||
try:
|
||||
with open(file_name, 'r') as f:
|
||||
self.editor.setPlainText(f.read())
|
||||
self.current_file = file_name
|
||||
except Exception as e:
|
||||
QMessageBox.critical(self, "Error", f"Could not open file: {str(e)}")
|
||||
|
||||
def save_file(self):
|
||||
if not hasattr(self, 'current_file') or not self.current_file:
|
||||
file_name, _ = QFileDialog.getSaveFileName(self, "Save File", "", "Bellos Scripts (*.bellos);;All Files (*)")
|
||||
if file_name:
|
||||
self.current_file = file_name
|
||||
else:
|
||||
return
|
||||
|
||||
try:
|
||||
with open(self.current_file, 'w') as f:
|
||||
f.write(self.editor.toPlainText())
|
||||
except Exception as e:
|
||||
QMessageBox.critical(self, "Error", f"Could not save file: {str(e)}")
|
||||
|
||||
def run_script(self):
|
||||
script_content = self.editor.toPlainText()
|
||||
try:
|
||||
output = self.script_manager.run_script(script_content)
|
||||
self.terminal.append_output(output)
|
||||
except Exception as e:
|
||||
QMessageBox.critical(self, "Error", f"Error running script: {str(e)}")
|
||||
self.terminal.append_output(f"Error: {str(e)}")
|
112
gui/file_explorer.py
Normal file
112
gui/file_explorer.py
Normal file
@@ -0,0 +1,112 @@
|
||||
# Copyright (C) 2024 Bellande Application Interoperability Xenogen Research Innovation Center, Ronaldson Bellande
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
from PyQt6.QtWidgets import QTreeView, QAbstractItemView
|
||||
from PyQt6.QtCore import Qt, pyqtSignal
|
||||
from PyQt6.QtGui import QStandardItemModel, QStandardItem
|
||||
from typing import Optional
|
||||
|
||||
class FileExplorer(QTreeView):
|
||||
file_selected = pyqtSignal(str)
|
||||
_model: Optional[QStandardItemModel]
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self._model = None
|
||||
self.current_path = ""
|
||||
self.setup_ui()
|
||||
|
||||
def setup_ui(self):
|
||||
# Create and set up the model
|
||||
self._model = QStandardItemModel(parent=self)
|
||||
self._model.setHorizontalHeaderLabels(['Name'])
|
||||
|
||||
# Set up the tree view
|
||||
super().setModel(self._model)
|
||||
self.setAnimated(False)
|
||||
self.setIndentation(20)
|
||||
self.setSortingEnabled(True)
|
||||
self.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
|
||||
|
||||
# Connect signals
|
||||
self.clicked.connect(self.on_item_clicked)
|
||||
|
||||
def populate_tree(self, path: str, parent_item: Optional[QStandardItem] = None) -> None:
|
||||
if parent_item is None and self._model is not None:
|
||||
self._model.clear()
|
||||
self._model.setHorizontalHeaderLabels(['Name'])
|
||||
parent_item = self._model.invisibleRootItem()
|
||||
|
||||
try:
|
||||
# List directory contents
|
||||
items = os.listdir(path)
|
||||
|
||||
# Sort items: directories first, then files
|
||||
dirs = sorted([item for item in items if os.path.isdir(os.path.join(path, item))])
|
||||
files = sorted([item for item in items if os.path.isfile(os.path.join(path, item))])
|
||||
|
||||
# Add directories
|
||||
for dir_name in dirs:
|
||||
dir_path = os.path.join(path, dir_name)
|
||||
try:
|
||||
item = QStandardItem(dir_name)
|
||||
item.setData(dir_path, Qt.ItemDataRole.UserRole)
|
||||
if parent_item:
|
||||
parent_item.appendRow(item)
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
# Add files
|
||||
for file_name in files:
|
||||
file_path = os.path.join(path, file_name)
|
||||
try:
|
||||
item = QStandardItem(file_name)
|
||||
item.setData(file_path, Qt.ItemDataRole.UserRole)
|
||||
if parent_item:
|
||||
parent_item.appendRow(item)
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
except PermissionError:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def on_item_clicked(self, index):
|
||||
if self._model is None:
|
||||
return
|
||||
|
||||
item = self._model.itemFromIndex(index)
|
||||
if item:
|
||||
path = item.data(Qt.ItemDataRole.UserRole)
|
||||
if os.path.isdir(path):
|
||||
if self.isExpanded(index):
|
||||
self.collapse(index)
|
||||
else:
|
||||
item.removeRows(0, item.rowCount())
|
||||
self.populate_tree(path, item)
|
||||
self.expand(index)
|
||||
else:
|
||||
self.file_selected.emit(path)
|
||||
|
||||
def set_root_path(self, path: str) -> None:
|
||||
if os.path.exists(path):
|
||||
self.current_path = path
|
||||
self.populate_tree(path)
|
||||
|
||||
def refresh(self) -> None:
|
||||
if self.current_path:
|
||||
self.populate_tree(self.current_path)
|
165
gui/project_system.py
Normal file
165
gui/project_system.py
Normal file
@@ -0,0 +1,165 @@
|
||||
# Copyright (C) 2024 Bellande Application Interoperability Xenogen Research Innovation Center, Ronaldson Bellande
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
from PyQt6.QtWidgets import (QWidget, QVBoxLayout, QFormLayout, QLineEdit,
|
||||
QPushButton, QComboBox, QSpinBox, QCheckBox,
|
||||
QFileDialog, QMessageBox, QHBoxLayout)
|
||||
import json
|
||||
import os
|
||||
|
||||
class ProjectSettings(QWidget):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.settings_file = "bellos_settings.bellande"
|
||||
self.init_ui()
|
||||
self.load_settings()
|
||||
|
||||
def init_ui(self):
|
||||
layout = QVBoxLayout()
|
||||
form_layout = QFormLayout()
|
||||
|
||||
# Project name
|
||||
self.project_name = QLineEdit()
|
||||
form_layout.addRow("Project Name:", self.project_name)
|
||||
|
||||
# Project path
|
||||
self.project_path = QLineEdit()
|
||||
self.browse_button = QPushButton("Browse...")
|
||||
self.browse_button.clicked.connect(self.browse_path)
|
||||
path_layout = QHBoxLayout()
|
||||
path_layout.addWidget(self.project_path)
|
||||
path_layout.addWidget(self.browse_button)
|
||||
form_layout.addRow("Project Path:", path_layout)
|
||||
|
||||
# Default shell
|
||||
self.default_shell = QComboBox()
|
||||
self.default_shell.addItems(["bellos", "bash", "sh", "zsh"])
|
||||
form_layout.addRow("Default Shell:", self.default_shell)
|
||||
|
||||
# Tab width
|
||||
self.tab_width = QSpinBox()
|
||||
self.tab_width.setRange(2, 8)
|
||||
self.tab_width.setValue(4)
|
||||
form_layout.addRow("Tab Width:", self.tab_width)
|
||||
|
||||
# Editor settings
|
||||
self.auto_save = QCheckBox()
|
||||
form_layout.addRow("Auto Save:", self.auto_save)
|
||||
|
||||
self.auto_indent = QCheckBox()
|
||||
self.auto_indent.setChecked(True)
|
||||
form_layout.addRow("Auto Indent:", self.auto_indent)
|
||||
|
||||
self.show_line_numbers = QCheckBox()
|
||||
self.show_line_numbers.setChecked(True)
|
||||
form_layout.addRow("Show Line Numbers:", self.show_line_numbers)
|
||||
|
||||
# Terminal settings
|
||||
self.terminal_font_size = QSpinBox()
|
||||
self.terminal_font_size.setRange(8, 24)
|
||||
self.terminal_font_size.setValue(10)
|
||||
form_layout.addRow("Terminal Font Size:", self.terminal_font_size)
|
||||
|
||||
self.terminal_history_size = QSpinBox()
|
||||
self.terminal_history_size.setRange(100, 10000)
|
||||
self.terminal_history_size.setValue(1000)
|
||||
form_layout.addRow("Terminal History Size:", self.terminal_history_size)
|
||||
|
||||
# Bellos script settings
|
||||
self.script_extension = QLineEdit(".bellos")
|
||||
form_layout.addRow("Script Extension:", self.script_extension)
|
||||
|
||||
self.default_timeout = QSpinBox()
|
||||
self.default_timeout.setRange(0, 3600)
|
||||
self.default_timeout.setValue(30)
|
||||
form_layout.addRow("Default Script Timeout (seconds):", self.default_timeout)
|
||||
|
||||
layout.addLayout(form_layout)
|
||||
|
||||
# Buttons
|
||||
button_layout = QHBoxLayout()
|
||||
save_button = QPushButton("Save Settings")
|
||||
save_button.clicked.connect(self.save_settings)
|
||||
reset_button = QPushButton("Reset to Defaults")
|
||||
reset_button.clicked.connect(self.reset_settings)
|
||||
button_layout.addWidget(save_button)
|
||||
button_layout.addWidget(reset_button)
|
||||
layout.addLayout(button_layout)
|
||||
|
||||
self.setLayout(layout)
|
||||
|
||||
def browse_path(self):
|
||||
path = QFileDialog.getExistingDirectory(self, "Select Project Directory")
|
||||
if path:
|
||||
self.project_path.setText(path)
|
||||
|
||||
def save_settings(self):
|
||||
settings = {
|
||||
"project_name": self.project_name.text(),
|
||||
"project_path": self.project_path.text(),
|
||||
"default_shell": self.default_shell.currentText(),
|
||||
"tab_width": self.tab_width.value(),
|
||||
"auto_save": self.auto_save.isChecked(),
|
||||
"auto_indent": self.auto_indent.isChecked(),
|
||||
"show_line_numbers": self.show_line_numbers.isChecked(),
|
||||
"terminal_font_size": self.terminal_font_size.value(),
|
||||
"terminal_history_size": self.terminal_history_size.value(),
|
||||
"script_extension": self.script_extension.text(),
|
||||
"default_timeout": self.default_timeout.value()
|
||||
}
|
||||
|
||||
try:
|
||||
with open(self.settings_file, 'w') as f:
|
||||
json.dump(settings, f, indent=4)
|
||||
QMessageBox.information(self, "Success", "Settings saved successfully!")
|
||||
except Exception as e:
|
||||
QMessageBox.critical(self, "Error", f"Could not save settings: {str(e)}")
|
||||
|
||||
def load_settings(self):
|
||||
if os.path.exists(self.settings_file):
|
||||
try:
|
||||
with open(self.settings_file, 'r') as f:
|
||||
settings = json.load(f)
|
||||
|
||||
self.project_name.setText(settings.get("project_name", ""))
|
||||
self.project_path.setText(settings.get("project_path", ""))
|
||||
self.default_shell.setCurrentText(settings.get("default_shell", "bellos"))
|
||||
self.tab_width.setValue(settings.get("tab_width", 4))
|
||||
self.auto_save.setChecked(settings.get("auto_save", False))
|
||||
self.auto_indent.setChecked(settings.get("auto_indent", True))
|
||||
self.show_line_numbers.setChecked(settings.get("show_line_numbers", True))
|
||||
self.terminal_font_size.setValue(settings.get("terminal_font_size", 10))
|
||||
self.terminal_history_size.setValue(settings.get("terminal_history_size", 1000))
|
||||
self.script_extension.setText(settings.get("script_extension", ".bellos"))
|
||||
self.default_timeout.setValue(settings.get("default_timeout", 30))
|
||||
except Exception as e:
|
||||
QMessageBox.warning(self, "Warning", f"Could not load settings: {str(e)}")
|
||||
|
||||
def reset_settings(self):
|
||||
reply = QMessageBox.question(self, "Confirm Reset",
|
||||
"Are you sure you want to reset all settings to default?",
|
||||
QMessageBox.StandardButton.Yes |
|
||||
QMessageBox.StandardButton.No)
|
||||
|
||||
if reply == QMessageBox.StandardButton.Yes:
|
||||
self.default_shell.setCurrentText("bellos")
|
||||
self.tab_width.setValue(4)
|
||||
self.auto_save.setChecked(False)
|
||||
self.auto_indent.setChecked(True)
|
||||
self.show_line_numbers.setChecked(True)
|
||||
self.terminal_font_size.setValue(10)
|
||||
self.terminal_history_size.setValue(1000)
|
||||
self.script_extension.setText(".bellos")
|
||||
self.default_timeout.setValue(30)
|
103
gui/script_editor.py
Normal file
103
gui/script_editor.py
Normal file
@@ -0,0 +1,103 @@
|
||||
# Copyright (C) 2024 Bellande Application Interoperability Xenogen Research Innovation Center, Ronaldson Bellande
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
from PyQt6.QtWidgets import QPlainTextEdit
|
||||
from PyQt6.QtGui import QTextCharFormat, QSyntaxHighlighter, QColor, QFont
|
||||
|
||||
class BellosSyntaxHighlighter(QSyntaxHighlighter):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
self.highlighting_rules = []
|
||||
|
||||
# Define formats for different syntax elements
|
||||
keyword_format = QTextCharFormat()
|
||||
keyword_format.setForeground(QColor("#569CD6"))
|
||||
keyword_format.setFontWeight(QFont.Weight.Bold)
|
||||
keywords = ["if", "else", "while", "for", "in", "do", "done", "echo", "export"]
|
||||
|
||||
for word in keywords:
|
||||
self.highlighting_rules.append((
|
||||
f"\\b{word}\\b",
|
||||
keyword_format
|
||||
))
|
||||
|
||||
# String format
|
||||
string_format = QTextCharFormat()
|
||||
string_format.setForeground(QColor("#CE9178"))
|
||||
self.highlighting_rules.append((
|
||||
r'"[^"\\]*(\\.[^"\\]*)*"',
|
||||
string_format
|
||||
))
|
||||
|
||||
# Comment format
|
||||
comment_format = QTextCharFormat()
|
||||
comment_format.setForeground(QColor("#608B4E"))
|
||||
self.highlighting_rules.append((
|
||||
r"#[^\n]*",
|
||||
comment_format
|
||||
))
|
||||
|
||||
def highlightBlock(self, text):
|
||||
from PyQt6.QtCore import QRegularExpression
|
||||
|
||||
for pattern, format in self.highlighting_rules:
|
||||
expression = QRegularExpression(pattern)
|
||||
match_iterator = expression.globalMatch(text)
|
||||
while match_iterator.hasNext():
|
||||
match = match_iterator.next()
|
||||
self.setFormat(match.capturedStart(), match.capturedLength(), format)
|
||||
|
||||
class ScriptEditor(QPlainTextEdit):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.setup_editor()
|
||||
|
||||
def setup_editor(self):
|
||||
# Set font
|
||||
font = QFont("Courier New", 10)
|
||||
self.setFont(font)
|
||||
|
||||
# Set tab width
|
||||
self.setTabStopDistance(40) # 4 spaces
|
||||
|
||||
# Enable line numbers
|
||||
self.setLineWrapMode(QPlainTextEdit.LineWrapMode.NoWrap)
|
||||
|
||||
# Set syntax highlighter
|
||||
self.highlighter = BellosSyntaxHighlighter(self.document())
|
||||
|
||||
# Set placeholder text
|
||||
self.setPlaceholderText("Enter your Bellos script here...")
|
||||
|
||||
def line_number_area_width(self):
|
||||
digits = 1
|
||||
max_num = max(1, self.blockCount())
|
||||
while max_num >= 10:
|
||||
max_num //= 10
|
||||
digits += 1
|
||||
space = 3 + self.fontMetrics().horizontalAdvance('9') * digits
|
||||
return space
|
||||
|
||||
def update_line_number_area_width(self, new_block_count):
|
||||
self.setViewportMargins(self.line_number_area_width(), 0, 0, 0)
|
||||
|
||||
def update_line_number_area(self, rect, dy):
|
||||
if dy:
|
||||
self.line_number_area.scroll(0, dy)
|
||||
else:
|
||||
self.line_number_area.update(0, rect.y(), self.line_number_area.width(), rect.height())
|
||||
|
||||
if rect.contains(self.viewport().rect()):
|
||||
self.update_line_number_area_width(0)
|
139
gui/script_manager.py
Normal file
139
gui/script_manager.py
Normal file
@@ -0,0 +1,139 @@
|
||||
# Copyright (C) 2024 Bellande Application Interoperability Xenogen Research Innovation Center, Ronaldson Bellande
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import subprocess
|
||||
import os
|
||||
import signal
|
||||
import threading
|
||||
import json
|
||||
from typing import Optional, Dict, List
|
||||
|
||||
class ScriptManager:
|
||||
def __init__(self):
|
||||
self.active_processes: Dict[int, subprocess.Popen] = {}
|
||||
self.script_history: List[str] = []
|
||||
self.settings = self.load_settings()
|
||||
|
||||
def load_settings(self) -> dict:
|
||||
settings_file = "bellos_settings.json"
|
||||
default_settings = {
|
||||
"default_shell": "bellos",
|
||||
"default_timeout": 30,
|
||||
"script_extension": ".bellos"
|
||||
}
|
||||
|
||||
if os.path.exists(settings_file):
|
||||
try:
|
||||
with open(settings_file, 'r') as f:
|
||||
return json.load(f)
|
||||
except Exception:
|
||||
return default_settings
|
||||
return default_settings
|
||||
|
||||
def run_script(self, script_content: str, timeout: Optional[int] = None) -> str:
|
||||
if timeout is None:
|
||||
timeout = self.settings.get("default_timeout", 30)
|
||||
|
||||
# Create a temporary script file
|
||||
script_file = f"temp_script{self.settings.get('script_extension', '.bellos')}"
|
||||
try:
|
||||
with open(script_file, 'w') as f:
|
||||
f.write(script_content)
|
||||
|
||||
# Make the script executable
|
||||
os.chmod(script_file, 0o755)
|
||||
|
||||
# Run the script with the specified shell
|
||||
shell = self.settings.get("default_shell", "bellos")
|
||||
process = subprocess.Popen(
|
||||
[shell, script_file],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True
|
||||
)
|
||||
|
||||
self.active_processes[process.pid] = process
|
||||
|
||||
# Set up timeout handling
|
||||
timer = threading.Timer(timeout, self.kill_process, args=[process.pid])
|
||||
timer.start()
|
||||
|
||||
try:
|
||||
stdout, stderr = process.communicate()
|
||||
timer.cancel() # Cancel the timeout if process completes normally
|
||||
except subprocess.TimeoutExpired:
|
||||
return f"Script execution timed out after {timeout} seconds"
|
||||
|
||||
if process.pid in self.active_processes:
|
||||
del self.active_processes[process.pid]
|
||||
|
||||
# Add to history
|
||||
self.script_history.append(script_content)
|
||||
|
||||
# Return combined output
|
||||
output = stdout
|
||||
if stderr:
|
||||
output += f"\nErrors:\n{stderr}"
|
||||
return output
|
||||
|
||||
finally:
|
||||
# Clean up temporary script file
|
||||
if os.path.exists(script_file):
|
||||
os.remove(script_file)
|
||||
|
||||
def kill_process(self, pid: int):
|
||||
if pid in self.active_processes:
|
||||
process = self.active_processes[pid]
|
||||
try:
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
process.wait(timeout=1) # Give it a second to terminate gracefully
|
||||
except (ProcessLookupError, subprocess.TimeoutExpired):
|
||||
try:
|
||||
os.kill(pid, signal.SIGKILL) # Force kill if necessary
|
||||
except ProcessLookupError:
|
||||
pass
|
||||
finally:
|
||||
del self.active_processes[pid]
|
||||
|
||||
def kill_all_processes(self):
|
||||
for pid in list(self.active_processes.keys()):
|
||||
self.kill_process(pid)
|
||||
|
||||
def get_script_history(self) -> List[str]:
|
||||
return self.script_history
|
||||
|
||||
def clear_history(self):
|
||||
self.script_history.clear()
|
||||
|
||||
def validate_script(self, script_content: str) -> bool:
|
||||
# Basic script validation
|
||||
try:
|
||||
shell = self.settings.get("default_shell", "bellos")
|
||||
process = subprocess.run(
|
||||
[shell, "-n"],
|
||||
input=script_content,
|
||||
text=True,
|
||||
capture_output=True
|
||||
)
|
||||
return process.returncode == 0
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def get_active_processes(self) -> Dict[int, subprocess.Popen]:
|
||||
# Clean up finished processes
|
||||
for pid in list(self.active_processes.keys()):
|
||||
if self.active_processes[pid].poll() is not None:
|
||||
del self.active_processes[pid]
|
||||
return self.active_processes
|
47
gui/terminal.py
Normal file
47
gui/terminal.py
Normal file
@@ -0,0 +1,47 @@
|
||||
# Copyright (C) 2024 Bellande Application Interoperability Xenogen Research Innovation Center, Ronaldson Bellande
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
from PyQt6.QtWidgets import QTextEdit
|
||||
from PyQt6.QtGui import QTextCursor, QFont
|
||||
|
||||
class Terminal(QTextEdit):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.setup_ui()
|
||||
|
||||
def setup_ui(self):
|
||||
# Set read-only mode
|
||||
self.setReadOnly(True)
|
||||
|
||||
# Set monospace font
|
||||
font = QFont("Courier New", 10)
|
||||
self.setFont(font)
|
||||
|
||||
# Set background and text colors
|
||||
self.setStyleSheet("""
|
||||
QTextEdit {
|
||||
background-color: #1E1E1E;
|
||||
color: #FFFFFF;
|
||||
border: none;
|
||||
}
|
||||
""")
|
||||
|
||||
def append_output(self, text):
|
||||
self.moveCursor(QTextCursor.MoveOperation.End)
|
||||
self.insertPlainText(text + '\n')
|
||||
self.moveCursor(QTextCursor.MoveOperation.End)
|
||||
|
||||
def clear_output(self):
|
||||
self.clear()
|
Reference in New Issue
Block a user