datawithsuman commited on
Commit
6d1681d
·
verified ·
1 Parent(s): 9e3d5c6

Added code to handle java files

Browse files
Files changed (1) hide show
  1. app.py +50 -13
app.py CHANGED
@@ -42,21 +42,58 @@ if uploaded_files:
42
  f.write(uploaded_file.getbuffer())
43
  st.success("File uploaded...")
44
 
 
 
 
45
  st.success("Fetching list of functions...")
46
  file_path = f"./data/{uploaded_file.name}"
47
- def extract_functions_from_file(file_path):
48
- with open(file_path, "r") as file:
49
- file_content = file.read()
50
-
51
- parsed_content = ast.parse(file_content)
52
- functions = {}
53
-
54
- for node in ast.walk(parsed_content):
55
- if isinstance(node, ast.FunctionDef):
56
- func_name = node.name
57
- func_body = ast.get_source_segment(file_content, node)
58
- functions[func_name] = func_body
59
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  return functions
61
 
62
  functions = extract_functions_from_file(file_path)
 
42
  f.write(uploaded_file.getbuffer())
43
  st.success("File uploaded...")
44
 
45
+ # Check file type
46
+ _, file_extension = os.path.splitext(uploaded_file.name)
47
+
48
  st.success("Fetching list of functions...")
49
  file_path = f"./data/{uploaded_file.name}"
50
+
51
+ def extract_functions_from_file(file_path, file_extension):
52
+
53
+ if file_extension == 'py':
54
+ with open(file_path, "r") as file:
55
+ file_content = file.read()
56
+
57
+ parsed_content = ast.parse(file_content)
58
+ functions = {}
59
+
60
+ for node in ast.walk(parsed_content):
61
+ if isinstance(node, ast.FunctionDef):
62
+ func_name = node.name
63
+ func_body = ast.get_source_segment(file_content, node)
64
+ functions[func_name] = func_body
65
+
66
+ elif file_extension == 'java':
67
+ with open(file_path, 'r') as file:
68
+ lines = file.readlines()
69
+
70
+ functions = {}
71
+ inside_method = False
72
+ method_name = None
73
+ method_body = []
74
+ brace_count = 0
75
+
76
+ method_signature_pattern = re.compile(r'((?:public|protected|private|static|\s)*)\s+[\w<>\[\]]+\s+(\w+)\s*\([^)]*\)\s*\{')
77
+
78
+ for line in lines:
79
+ if not inside_method:
80
+ match = method_signature_pattern.search(line)
81
+ if match:
82
+ modifiers, method_name = match.groups()
83
+ inside_method = True
84
+ method_body.append(line)
85
+ brace_count = line.count('{') - line.count('}')
86
+ else:
87
+ method_body.append(line)
88
+ brace_count += line.count('{') - line.count('}')
89
+ if brace_count == 0:
90
+ inside_method = False
91
+ functions[method_name] = ''.join(method_body)
92
+ method_body = []
93
+
94
+ if 'main' in functions.keys():
95
+ del(functions['main'])
96
+
97
  return functions
98
 
99
  functions = extract_functions_from_file(file_path)