Spaces:
Build error
Build error
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -3,48 +3,29 @@ import glob
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
|
| 6 |
-
def parse_file_content(string: str)
|
| 7 |
-
"""
|
| 8 |
-
Parses the content of a file and returns the action and description.
|
| 9 |
-
:param string: The content of a file.
|
| 10 |
-
:return: A tuple containing the action and description.
|
| 11 |
-
"""
|
| 12 |
first_break = string.find("---")
|
| 13 |
last_break = string.rfind("---")
|
| 14 |
if first_break == -1 and last_break == -1 or first_break == last_break:
|
| 15 |
return None, None
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
description = string[nl_after:].strip() if nl_after > last_break else ""
|
| 20 |
|
| 21 |
-
return string[first_break + 4 : last_break], description
|
| 22 |
|
| 23 |
-
|
| 24 |
-
def parse_action(string: str) -> tuple[str, str | None]:
|
| 25 |
-
"""
|
| 26 |
-
Parses the action from a string.
|
| 27 |
-
:param string: The string to parse the action from.
|
| 28 |
-
:return: A tuple containing the action and action input.
|
| 29 |
-
"""
|
| 30 |
assert string.startswith("action:")
|
| 31 |
idx = string.find("action_input=")
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return
|
| 35 |
|
| 36 |
|
| 37 |
-
def extract_imports(file_contents
|
| 38 |
-
"""
|
| 39 |
-
Extracts imports, functions, and classes from a file's contents.
|
| 40 |
-
:param file_contents: The contents of a file.
|
| 41 |
-
:return: A tuple containing the imports, functions, and classes.
|
| 42 |
-
"""
|
| 43 |
module_ast = ast.parse(file_contents)
|
| 44 |
imports = []
|
| 45 |
functions = [n for n in module_ast.body if isinstance(n, ast.FunctionDef)]
|
| 46 |
classes = [n for n in module_ast.body if isinstance(n, ast.ClassDef)]
|
| 47 |
-
|
| 48 |
for node in ast.walk(module_ast):
|
| 49 |
if isinstance(node, ast.Import):
|
| 50 |
for alias in node.names:
|
|
@@ -57,16 +38,10 @@ def extract_imports(file_contents: str) -> tuple[list[str], list[ast.FunctionDef
|
|
| 57 |
imports.append(f"{module_name}.{name}")
|
| 58 |
else:
|
| 59 |
imports.append(name)
|
| 60 |
-
|
| 61 |
return imports, functions, classes
|
| 62 |
|
| 63 |
|
| 64 |
-
def read_python_module_structure(path
|
| 65 |
-
"""
|
| 66 |
-
Reads the structure of a Python module and returns a prompt, content, and internal imports map.
|
| 67 |
-
:param path: The path to the Python module.
|
| 68 |
-
:return: A tuple containing the structure prompt, content, and internal imports map.
|
| 69 |
-
"""
|
| 70 |
file_types = ["*.py"]
|
| 71 |
code = []
|
| 72 |
for file_type in file_types:
|
|
@@ -75,7 +50,7 @@ def read_python_module_structure(path: str) -> tuple[str, dict[str, str], dict[s
|
|
| 75 |
structure_prompt = "Files:\n"
|
| 76 |
structure_prompt += "(listing all files and their functions and classes)\n\n"
|
| 77 |
|
| 78 |
-
def get_file_name(i
|
| 79 |
return "./{}.py".format(i.replace(".", "/"))
|
| 80 |
|
| 81 |
content = {}
|
|
@@ -85,19 +60,20 @@ def read_python_module_structure(path: str) -> tuple[str, dict[str, str], dict[s
|
|
| 85 |
continue
|
| 86 |
with open(fn, "r") as f:
|
| 87 |
content[fn] = f.read()
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
for i in
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
|
| 6 |
+
def parse_file_content(string: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
first_break = string.find("---")
|
| 8 |
last_break = string.rfind("---")
|
| 9 |
if first_break == -1 and last_break == -1 or first_break == last_break:
|
| 10 |
return None, None
|
| 11 |
+
nl_after = string.find("\n", last_break)
|
| 12 |
+
description = string[nl_after:]
|
| 13 |
+
return string[first_break + 4 : last_break], description.strip("\n")
|
|
|
|
| 14 |
|
|
|
|
| 15 |
|
| 16 |
+
def parse_action(string: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
assert string.startswith("action:")
|
| 18 |
idx = string.find("action_input=")
|
| 19 |
+
if idx == -1:
|
| 20 |
+
return string[8:], None
|
| 21 |
+
return string[8 : idx - 1], string[idx + 13 :].strip("'").strip('"')
|
| 22 |
|
| 23 |
|
| 24 |
+
def extract_imports(file_contents):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
module_ast = ast.parse(file_contents)
|
| 26 |
imports = []
|
| 27 |
functions = [n for n in module_ast.body if isinstance(n, ast.FunctionDef)]
|
| 28 |
classes = [n for n in module_ast.body if isinstance(n, ast.ClassDef)]
|
|
|
|
| 29 |
for node in ast.walk(module_ast):
|
| 30 |
if isinstance(node, ast.Import):
|
| 31 |
for alias in node.names:
|
|
|
|
| 38 |
imports.append(f"{module_name}.{name}")
|
| 39 |
else:
|
| 40 |
imports.append(name)
|
|
|
|
| 41 |
return imports, functions, classes
|
| 42 |
|
| 43 |
|
| 44 |
+
def read_python_module_structure(path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
file_types = ["*.py"]
|
| 46 |
code = []
|
| 47 |
for file_type in file_types:
|
|
|
|
| 50 |
structure_prompt = "Files:\n"
|
| 51 |
structure_prompt += "(listing all files and their functions and classes)\n\n"
|
| 52 |
|
| 53 |
+
def get_file_name(i):
|
| 54 |
return "./{}.py".format(i.replace(".", "/"))
|
| 55 |
|
| 56 |
content = {}
|
|
|
|
| 60 |
continue
|
| 61 |
with open(fn, "r") as f:
|
| 62 |
content[fn] = f.read()
|
| 63 |
+
imports, functions, classes = extract_imports(content[fn])
|
| 64 |
+
internal_imports = list(
|
| 65 |
+
{".".join(i.split(".")[:-1]) for i in imports if i.startswith("app.")}
|
| 66 |
+
)
|
| 67 |
+
internal_imports_map[fn] = [get_file_name(i) for i in internal_imports]
|
| 68 |
+
structure_prompt += f"{fn}\n"
|
| 69 |
+
for function in functions:
|
| 70 |
+
structure_prompt += " {}()\n".format(function.name, function.args.args)
|
| 71 |
+
|
| 72 |
+
for class_ in classes:
|
| 73 |
+
structure_prompt += " {}\n".format(class_.name)
|
| 74 |
+
methods = [n for n in class_.body if isinstance(n, ast.FunctionDef)]
|
| 75 |
+
for method in methods:
|
| 76 |
+
structure_prompt += " {}.{}()\n".format(class_.name, method.name)
|
| 77 |
+
structure_prompt += "\n"
|
| 78 |
+
|
| 79 |
+
return structure_prompt, content, internal_imports_map
|