Skip to content

Commit

Permalink
Dev done (#4)
Browse files Browse the repository at this point in the history
* Add filedialog for upload cmd

* minor things
  • Loading branch information
unaidedelf8777 authored Oct 15, 2023
1 parent 1858ffb commit e2716cd
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def download_file_from_container(container_id, file_path_in_container, local_dir
container = client.containers.get(container_id)

# Use get_archive to get a file from the container
stream, stat = container.get_archive(file_path_in_container)
stream, stat = container.get_archive(os.path.join("/mnt/data/", file_path_in_container))

# Get the file name from the stat info
file_name = os.path.basename(stat['name'])
Expand Down
47 changes: 47 additions & 0 deletions interpreter/terminal_interface/components/file_dialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Simple class and method to launch the users system filedialog """

from PyQt5.QtWidgets import QApplication, QFileDialog, QMessageBox

class FileDialog:
def get_path(self, type=None):
"""
Open a file dialog and return the selected file or folder path.
:param type: str, optional (default=None)
Specifies the type of path to select ("file", "folder", or None).
If None, the user will be asked whether to select a file or a folder.
:return: str
The path selected by the user. If the user cancels the operation, it returns None.
"""
app = QApplication.instance()
if app is None:
app = QApplication([])

options = QFileDialog.Options()
options |= QFileDialog.ReadOnly

if type is None:
msg_box = QMessageBox()
msg_box.setWindowTitle("Choose Action")
msg_box.setText("Do you want to select a file or a folder?")
btn_file = msg_box.addButton("File", QMessageBox.YesRole)
btn_folder = msg_box.addButton("Folder", QMessageBox.NoRole)
msg_box.addButton(QMessageBox.Cancel)
user_choice = msg_box.exec_()

if user_choice == QMessageBox.Cancel:
return None
type = "file" if msg_box.clickedButton() == btn_file else "folder"

if type == "file":
path, _ = QFileDialog.getOpenFileName(None, "Open File", "",
"All Files (*)", options=options)
elif type == "folder":
path = QFileDialog.getExistingDirectory(None, "Open Folder",
"", options=options)
else:
path = self.get_path(type=None) # this or val err, may aswell explicitly pass none.

return path


45 changes: 41 additions & 4 deletions interpreter/terminal_interface/magic_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from ..code_interpreters.container_utils.upload_file import copy_file_to_container
from ..code_interpreters.create_code_interpreter import SESSION_IDS_BY_OBJECT

from rich import print as Print


def handle_undo(self, arguments):
# Removes all messages after the most recent user entry (and the entry itself).
Expand Down Expand Up @@ -51,6 +53,10 @@ def handle_help(self, arguments):
"%save_message [path]": "Saves messages to a specified JSON path. If no path is provided, it defaults to 'messages.json'.",
"%load_message [path]": "Loads messages from a specified JSON path. If no path is provided, it defaults to 'messages.json'.",
"%help": "Show this help message.",
"%upload": "open a File Dialog, and select a file to upload to the container. only used when using containerized code execution",
"%upload folder": "same as upload command, except you can upload a folder instead of just a file.",
"%upload file": "same as upload command, except you can upload a file.",
"%download" : "Download a file or directory given the file or folder name in the container."
}

base_message = [
Expand Down Expand Up @@ -115,8 +121,18 @@ def handle_load_message(self, json_path):
display_markdown_message(
f"> messages json loaded from {os.path.abspath(json_path)}")

def handle_container_upload(self, *args):

def handle_container_upload(self,type=None, *args):
def is_gui_available():
try:
from PyQt5.QtWidgets import QApplication
app = QApplication([])
del app
return True
except Exception as e:
print(f"An error occurred: {str(e)}")
return False

args = list(args)
if self.use_containers:
try:
client = docker.APIClient()
Expand All @@ -128,7 +144,28 @@ def handle_container_upload(self, *args):
)
display_markdown_message(f"{error_message}")
return

if len(args) == 0:
if is_gui_available():
try:
from .components.file_dialog import FileDialog

fd = FileDialog()
if type is not None:
path = fd.get_path(type=type)
else:
path = fd.get_path(type=None)
if path is not None: # if none, they exited

args.append(path)
else: # We shall now exit on them out of spite
return
except ImportError as e:
Print(f"Internal import error {e}")
return
else:
Print(f" No filepath provided. please provide one. use the command %upload <filetype (file or folder)> <path>")
return

for filepath in args:
if os.path.exists(filepath):
session_id = SESSION_IDS_BY_OBJECT.get(self)
Expand Down Expand Up @@ -159,7 +196,7 @@ def handle_container_download(self, *args):
try:
client = docker.APIClient()
except Exception as e:
print("[BOLD][RED]Unable to connect to the Docker Container daemon. Please ensure Docker is installed and running. ignoring command[/BOLD]")
print("[BOLD][RED]Unable to connect to the Docker Container daemon. Please ensure Docker is installed and running. ignoring command[/RED][/BOLD]")
return

session_id = SESSION_IDS_BY_OBJECT.get(self)
Expand Down
69 changes: 68 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pyyaml = "^6.0.1"
docker = "^6.1.3"
semgrep = "^1.41.0"
yaspin = "^3.0.1"
pyqt5-qt5 = "5.15.2"
pyqt5 = "5.15.10"
[tool.poetry.dependencies.pyreadline3]
version = "^3.4.1"
markers = "sys_platform == 'win32'"
Expand Down

0 comments on commit e2716cd

Please sign in to comment.