Update CodeFileEditAtomicFlow.py
Browse files- CodeFileEditAtomicFlow.py +39 -23
CodeFileEditAtomicFlow.py
CHANGED
|
@@ -4,34 +4,38 @@ from typing import Dict, Any
|
|
| 4 |
from flows.base_flows.atomic import AtomicFlow
|
| 5 |
import os
|
| 6 |
|
|
|
|
| 7 |
class CodeFileEditAtomicFlow(AtomicFlow):
|
| 8 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
directory = os.path.dirname(code_lib_location)
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
try:
|
| 15 |
-
|
| 16 |
-
"# The below code will be appended to " +
|
| 17 |
-
code_lib_location + "\n"
|
| 18 |
-
"# Edit the code directly or provide your thoughts down below if you have any suggestions.\n"
|
| 19 |
-
"# Please do provide your feedback in the thoughts section so that JARVIS knows what you "
|
| 20 |
-
"are doing.\n"
|
| 21 |
-
"###########\n"
|
| 22 |
-
"# Code:\n" +
|
| 23 |
-
code_str +
|
| 24 |
-
"\n############\n"
|
| 25 |
-
"# Thoughts:"
|
| 26 |
-
)
|
| 27 |
-
with open(temp_file_location, "w") as file:
|
| 28 |
file.write(content)
|
| 29 |
-
file_written_timestamp = os.path.getmtime(
|
| 30 |
|
| 31 |
-
return True, f"Code written to {
|
| 32 |
|
| 33 |
except Exception as e:
|
| 34 |
-
return False, str(e),
|
| 35 |
|
| 36 |
def _check_input(self, input_data: Dict[str, Any]):
|
| 37 |
assert "code" in input_data, "code is not passed to CodeFileEditAtomicFlow"
|
|
@@ -41,21 +45,33 @@ class CodeFileEditAtomicFlow(AtomicFlow):
|
|
| 41 |
code_lib_loc = input_data["memory_files"]["code_library"]
|
| 42 |
assert os.path.exists(code_lib_loc), f"{code_lib_loc} does not exist"
|
| 43 |
assert os.path.isfile(code_lib_loc), f"{code_lib_loc} is not a file"
|
|
|
|
|
|
|
| 44 |
|
| 45 |
def run(
|
| 46 |
self,
|
| 47 |
input_data: Dict[str, Any]
|
| 48 |
):
|
| 49 |
self._check_input(input_data)
|
|
|
|
|
|
|
| 50 |
code_str = input_data['code']
|
| 51 |
-
language_of_code = input_data["language_of_code"]
|
| 52 |
code_lib_location = input_data["memory_files"]["code_library"]
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
if result:
|
| 55 |
try:
|
| 56 |
subprocess.run(["code", temp_file_location], timeout=10)
|
| 57 |
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
|
| 58 |
webbrowser.open(temp_file_location)
|
|
|
|
|
|
|
| 59 |
response = {}
|
| 60 |
response["code_editor_output"] = code_editor_output
|
| 61 |
response["temp_code_file_location"] = temp_file_location
|
|
|
|
| 4 |
from flows.base_flows.atomic import AtomicFlow
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
|
| 8 |
class CodeFileEditAtomicFlow(AtomicFlow):
|
| 9 |
+
def _generate_content(self, code_lib_location, code_str) -> str:
|
| 10 |
+
content = (
|
| 11 |
+
"# The below code will be appended to " +
|
| 12 |
+
code_lib_location + "\n"
|
| 13 |
+
"# Edit the code directly or provide your thoughts down below if you have any suggestions.\n"
|
| 14 |
+
"# Please do provide your feedback in the thoughts section so that JARVIS knows what you "
|
| 15 |
+
"are doing.\n"
|
| 16 |
+
"###########\n"
|
| 17 |
+
"# Code:\n" +
|
| 18 |
+
code_str +
|
| 19 |
+
"\n############\n"
|
| 20 |
+
"# Thoughts:"
|
| 21 |
+
)
|
| 22 |
+
return content
|
| 23 |
+
|
| 24 |
+
def _generate_temp_file_location(self, code_lib_location):
|
| 25 |
directory = os.path.dirname(code_lib_location)
|
| 26 |
+
ret = os.path.join(directory, 'temp.py')
|
| 27 |
+
return ret
|
| 28 |
+
|
| 29 |
+
def _write_code_content_to_file(self, file_location, content: str):
|
| 30 |
try:
|
| 31 |
+
with open(file_location, "w") as file:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
file.write(content)
|
| 33 |
+
file_written_timestamp = os.path.getmtime(file_location)
|
| 34 |
|
| 35 |
+
return True, f"Code written to {file_location}", file_location, file_written_timestamp
|
| 36 |
|
| 37 |
except Exception as e:
|
| 38 |
+
return False, str(e), file_location, 0
|
| 39 |
|
| 40 |
def _check_input(self, input_data: Dict[str, Any]):
|
| 41 |
assert "code" in input_data, "code is not passed to CodeFileEditAtomicFlow"
|
|
|
|
| 45 |
code_lib_loc = input_data["memory_files"]["code_library"]
|
| 46 |
assert os.path.exists(code_lib_loc), f"{code_lib_loc} does not exist"
|
| 47 |
assert os.path.isfile(code_lib_loc), f"{code_lib_loc} is not a file"
|
| 48 |
+
language_of_code = input_data["language_of_code"]
|
| 49 |
+
assert language_of_code.lower() == 'python', "sorry!! only writing python code is supported."
|
| 50 |
|
| 51 |
def run(
|
| 52 |
self,
|
| 53 |
input_data: Dict[str, Any]
|
| 54 |
):
|
| 55 |
self._check_input(input_data)
|
| 56 |
+
|
| 57 |
+
# ~~~ Generating temp file location and content to write ~~~
|
| 58 |
code_str = input_data['code']
|
|
|
|
| 59 |
code_lib_location = input_data["memory_files"]["code_library"]
|
| 60 |
+
content_to_write = self._generate_content(code_lib_location, code_str)
|
| 61 |
+
file_location_to_write = self._generate_temp_file_location(code_lib_location)
|
| 62 |
+
|
| 63 |
+
# ~~~ Calling the writer function ~~~
|
| 64 |
+
result, code_editor_output, temp_file_location, file_written_timestamp = self._write_code_content_to_file(
|
| 65 |
+
file_location_to_write, content_to_write)
|
| 66 |
+
|
| 67 |
+
# ~~~ Opening up the file for the user to see ~~~
|
| 68 |
if result:
|
| 69 |
try:
|
| 70 |
subprocess.run(["code", temp_file_location], timeout=10)
|
| 71 |
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
|
| 72 |
webbrowser.open(temp_file_location)
|
| 73 |
+
|
| 74 |
+
# ~~~ Generating return variables ~~~
|
| 75 |
response = {}
|
| 76 |
response["code_editor_output"] = code_editor_output
|
| 77 |
response["temp_code_file_location"] = temp_file_location
|