acecalisto3 commited on
Commit
ca7a428
1 Parent(s): 0ebed5d

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +103 -0
utils.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ast
2
+ import glob
3
+ import os
4
+
5
+
6
+ def parse_file_content(string: str) -> tuple[str | None, str | None]:
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
+ # Find the newline after the last separator
18
+ nl_after = string.find("\n", last_break) + 1
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
+ action = string[8: idx - 1] if idx > 8 else string[8:]
33
+ action_input = string[idx + 13 :].strip("'").strip('"') if idx > 8 else None
34
+ return action, action_input
35
+
36
+
37
+ def extract_imports(file_contents: str) -> tuple[list[str], list[ast.FunctionDef], list[ast.ClassDef]]:
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:
51
+ imports.append(alias.name)
52
+ elif isinstance(node, ast.ImportFrom):
53
+ module_name = node.module
54
+ for alias in node.names:
55
+ name = alias.name
56
+ if module_name:
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: str) -> tuple[str, dict[str, str], dict[str, list[str]]]:
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:
73
+ code += glob.glob(os.path.join(path, "**", file_type), recursive=True)
74
+
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: str) -> str:
79
+ return "./{}.py".format(i.replace(".", "/"))
80
+
81
+ content = {}
82
+ internal_imports_map = {}
83
+ for fn in code:
84
+ if os.path.basename(fn) == "gpt.py":
85
+ continue
86
+ with open(fn, "r") as f:
87
+ content[fn] = f.read()
88
+
89
+ imports, functions, classes = extract_imports(content[fn])
90
+ internal_imports = [
91
+ ".".join(i.split(".")[:-1])
92
+ for i in imports
93
+ if i.startswith("app.")
94
+ ]
95
+ internal_imports_map[fn] = [
96
+ get_file_name(i) for i in set(internal_imports)
97
+ ]
98
+
99
+ structure_prompt += f"{fn}\n"
100
+ for function in functions:
101
+ structure_prompt += f" {function.name}({function.args})\n"
102
+
103
+ return structure_prompt, content, internal_imports_map