acecalisto3 commited on
Commit
084423b
·
verified ·
1 Parent(s): e739aa7

Update utils.py

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