-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatgpt.scanner.py
163 lines (129 loc) · 6.26 KB
/
chatgpt.scanner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""
Project Structure and Full Content Extraction Script
wThis script scans the entire project directory under 'app/src/main/' and extracts relevant
information from all Kotlin (.kt) and XML (.xml) files. The extracted information includes:
1. **Project Structure**: A detailed representation of all directories and files, providing an
overview of the entire structure.
2. **Kotlin and XML Files**: Full content extraction of relevant Kotlin (.kt) and XML (.xml)
files. Additionally, the script performs a search for relevant elements such as class
definitions, function signatures, and key XML tags to provide a comprehensive understanding of
the codebase.
The output of this script is intended to be detailed, easy to read, and suitable for sharing
directly in a chat, enabling efficient collaboration without manually navigating the entire project.
Functions: - extract_full_content_from_kotlin(file_path): Extracts the full content from Kotlin
files and highlights class and function definitions. - extract_full_content_from_xml(file_path):
Extracts the full content from XML files and highlights key XML tags. - scan_project_structure(
directory): Scans the directory and compiles the project structure and full content of relevant
files. - main(): The main function to execute the extraction and print the results.
Usage: - Run this script in the root directory of your project. - The script will output the
project structure along with the full content of Kotlin and XML files, highlighting relevant
elements.
Note:
- This script is designed for small to medium-sized projects where an overview of the structure and
key elements is needed for quick reference or discussion.
"""
import os
import re
# Constants
OUTPUT_FILENAME = "chatgpt.output.txt" # Output file name
def extract_full_content_from_kotlin(file_path):
"""
Extracts the full content from a Kotlin file and highlights class and function definitions.
Args:
file_path (str): The path to the Kotlin file.
Returns: dict: A dictionary containing the full content and highlighted elements (classes and
functions).
"""
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Find class and function definitions
classes = re.findall(r'class\s+\w+\s*(\(.*\))?\s*{', content)
functions = re.findall(r'fun\s+\w+\s*\(.*\)\s*{', content)
return {
"full_content": content,
"classes": classes,
"functions": functions
}
def extract_full_content_from_xml(file_path):
"""
Extracts the full content from an XML file and highlights key XML tags.
Args:
file_path (str): The path to the XML file.
Returns:
dict: A dictionary containing the full content and highlighted elements (key XML tags).
"""
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Extract key XML tags (excluding comments and empty lines)
key_tags = [line.strip() for line in content.splitlines()
if re.search(r'<[^!?].*?>', line.strip())]
return {
"full_content": content,
"key_tags": key_tags
}
def scan_project_structure(directory):
"""
Scans the directory and compiles the project structure and full content of relevant files.
Args:
directory (str): The root directory to scan.
Returns: tuple: A tuple containing the project structure as a list and the extracted content
as a dictionary.
"""
extracted_info = {}
project_structure = []
relevant_info = ""
for root, _, files in os.walk(directory):
# Store the project structure
level = root.replace(directory, '').count(os.sep)
indent = ' ' * 4 * level
project_structure.append(f"{indent}{os.path.basename(root)}/")
for file in files:
if file.endswith('.kt') or file.endswith('.xml'):
file_path = os.path.join(root, file)
# Extract full content based on file type
if file.endswith('.kt'):
relevant_info = extract_full_content_from_kotlin(file_path)
elif file.endswith('.xml'):
relevant_info = extract_full_content_from_xml(file_path)
if relevant_info:
extracted_info[file_path] = relevant_info
# Add file to project structure
sub_indent = ' ' * 4 * (level + 1)
project_structure.append(f"{sub_indent}{file}")
return project_structure, extracted_info
def main():
"""
Main function to execute the project structure and content extraction.
Outputs the project structure and full content of relevant files to a file.
"""
project_directory = "app/src/main/" # Directory of your project
project_structure, extracted_info = scan_project_structure(project_directory)
# Get the directory where the script is located
script_directory = os.path.dirname(os.path.abspath(__file__))
output_file_path = os.path.join(script_directory, OUTPUT_FILENAME)
with open(output_file_path, 'w', encoding='utf-8') as output_file:
# Write the project structure
output_file.write("Project Structure:\n")
for line in project_structure:
output_file.write(f"{line}\n")
output_file.write("\n\n")
# Write the extracted full content and highlighted elements
output_file.write("Extracted Full Content and Highlights:\n")
for file_path, info in extracted_info.items():
output_file.write(f"File: {file_path}\n")
output_file.write("Full Content:\n")
output_file.write(f"{info['full_content']}\n\n")
output_file.write("Highlighted Elements:\n")
if "classes" in info:
for cls in info["classes"]:
output_file.write(f" Class: {cls}\n")
if "functions" in info:
for func in info["functions"]:
output_file.write(f" Function: {func}\n")
if "key_tags" in info:
for tag in info["key_tags"]:
output_file.write(f" XML Tag: {tag}\n")
output_file.write("\n")
print(f"Output has been written to {OUTPUT_FILENAME}")
if __name__ == "__main__":
main()