-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow editing of code blocks before execution
- Loading branch information
1 parent
86a6430
commit 0be3623
Showing
8 changed files
with
146 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
25 changes: 25 additions & 0 deletions
25
interpreter/code_interpreters/languages/utils/language_tools.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from ...language_map import language_map | ||
|
||
|
||
def get_language_file_extension(language_name): | ||
""" | ||
Get the file extension for a given language | ||
""" | ||
language = language_map[language_name.lower()] | ||
|
||
if language.file_extension: | ||
return language.file_extension | ||
else: | ||
return language | ||
|
||
|
||
def get_language_proper_name(language_name): | ||
""" | ||
Get the proper name for a given language | ||
""" | ||
language = language_map[language_name.lower()] | ||
|
||
if language.proper_name: | ||
return language.proper_name | ||
else: | ||
return language |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import os | ||
from yaspin import yaspin | ||
|
||
from .temporary_file import create_temporary_file, cleanup_temporary_file | ||
from .open_file import open_file | ||
from ..code_interpreters.languages.utils.language_tools import get_language_file_extension, get_language_proper_name | ||
|
||
|
||
def edit_code(code, language, interpreter): | ||
""" | ||
Edit the code and listen for changes with watchdog | ||
""" | ||
|
||
temp_code = code | ||
|
||
temp_file = create_temporary_file( | ||
temp_code, get_language_file_extension(language), verbose=interpreter.debug_mode | ||
) | ||
|
||
language_name = get_language_proper_name(language) | ||
|
||
file_name = os.path.basename(temp_file) | ||
|
||
if interpreter.debug_mode: | ||
print(f"Editing {language_name} code in {file_name}") | ||
print("---") | ||
|
||
# Run semgrep | ||
try: | ||
print(" Press `ENTER` after you've saved your edits.") | ||
|
||
open_file(temp_file) | ||
|
||
with yaspin(text=f" Editing {language_name} code...").green.right.dots as loading: | ||
# HACK: we're just listening for the user to come back and hit Enter | ||
# but we aren't actually doing anything with it and since we're inside | ||
# of a yaspin handler, the input prompt doesn't actually render | ||
done = input(" Press `ENTER` when you're ready to continue:") | ||
|
||
loading.stop() | ||
loading.hide() | ||
|
||
if done == "": | ||
print(f" {language_name} code updated.") | ||
print("") # <- Aesthetic choice | ||
|
||
temp_code = open(temp_file).read() | ||
|
||
if interpreter.debug_mode: | ||
print(f"Getting updated {language_name} code from {file_name}") | ||
print("---") | ||
|
||
cleanup_temporary_file(temp_file, verbose=interpreter.debug_mode) | ||
|
||
if interpreter.debug_mode: | ||
print(f"Deleting {file_name}") | ||
print("---") | ||
|
||
except Exception as e: | ||
print(f"Could not edit {language} code.") | ||
print(e) | ||
print("") # <- Aesthetic choice | ||
|
||
return temp_code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import os | ||
import platform | ||
import subprocess | ||
|
||
def open_file(file_path): | ||
if platform.system() == 'Windows': | ||
os.startfile(file_path) # This will open the file with the default application, e.g., Notepad | ||
else: | ||
try: | ||
# Try using xdg-open on non-Windows platforms | ||
subprocess.call(['xdg-open', file_path]) | ||
except FileNotFoundError: | ||
# Fallback to using 'open' on macOS if 'xdg-open' is not available | ||
subprocess.call(['open', file_path]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters