Spaces:
Running
on
L40S
Running
on
L40S
miaoyibo
commited on
Commit
·
c8c66b5
1
Parent(s):
e52721c
update location
Browse files- kimi_dev/serve/templates.py +70 -47
kimi_dev/serve/templates.py
CHANGED
@@ -22,7 +22,7 @@ def show_project_structure(structure, spacing=0) -> str:
|
|
22 |
pp_string += ' ' * spacing + str(key) + '\n'
|
23 |
else:
|
24 |
pp_string += ' ' * spacing + str(key) + '/' + '\n'
|
25 |
-
if '
|
26 |
pp_string += show_project_structure(value, spacing + 4)
|
27 |
|
28 |
return pp_string
|
@@ -112,35 +112,6 @@ def parse_python_file(file_path, file_content=None):
|
|
112 |
)
|
113 |
return class_info, function_names, file_content.splitlines()
|
114 |
|
115 |
-
def create_structure(directory_path):
|
116 |
-
"""Create the structure of the repository directory by parsing Python files.
|
117 |
-
:param directory_path: Path to the repository directory.
|
118 |
-
:return: A dictionary representing the structure.
|
119 |
-
"""
|
120 |
-
structure = {}
|
121 |
-
for root, _, files in os.walk(directory_path):
|
122 |
-
repo_name = os.path.basename(directory_path)
|
123 |
-
relative_root = os.path.relpath(root, directory_path)
|
124 |
-
if relative_root == ".":
|
125 |
-
relative_root = repo_name
|
126 |
-
curr_struct = structure
|
127 |
-
for part in relative_root.split(os.sep):
|
128 |
-
if part not in curr_struct:
|
129 |
-
curr_struct[part] = {}
|
130 |
-
curr_struct = curr_struct[part]
|
131 |
-
for file_name in files:
|
132 |
-
if file_name.endswith(".py"):
|
133 |
-
file_path = os.path.join(root, file_name)
|
134 |
-
class_info, function_names, file_lines = parse_python_file(file_path)
|
135 |
-
curr_struct[file_name] = {
|
136 |
-
"classes": class_info,
|
137 |
-
"functions": function_names,
|
138 |
-
"text": file_lines,
|
139 |
-
}
|
140 |
-
else:
|
141 |
-
curr_struct[file_name] = {}
|
142 |
-
return structure
|
143 |
-
|
144 |
# def create_structure(directory_path):
|
145 |
# """Create the structure of the repository directory by parsing Python files.
|
146 |
# :param directory_path: Path to the repository directory.
|
@@ -148,15 +119,15 @@ def create_structure(directory_path):
|
|
148 |
# """
|
149 |
# structure = {}
|
150 |
# for root, _, files in os.walk(directory_path):
|
|
|
151 |
# relative_root = os.path.relpath(root, directory_path)
|
152 |
# if relative_root == ".":
|
153 |
-
#
|
154 |
-
#
|
155 |
-
#
|
156 |
-
#
|
157 |
-
#
|
158 |
-
#
|
159 |
-
# curr_struct = curr_struct[part]
|
160 |
# for file_name in files:
|
161 |
# if file_name.endswith(".py"):
|
162 |
# file_path = os.path.join(root, file_name)
|
@@ -167,18 +138,70 @@ def create_structure(directory_path):
|
|
167 |
# "text": file_lines,
|
168 |
# }
|
169 |
# else:
|
170 |
-
#
|
171 |
-
# try:
|
172 |
-
# with open(file_path, 'r', encoding='utf-8') as f:
|
173 |
-
# file_content = f.read()
|
174 |
-
# curr_struct[file_name] = {
|
175 |
-
# "text": file_content.splitlines()
|
176 |
-
# }
|
177 |
-
# except Exception as e:
|
178 |
-
# print(f"fail to read {file_path}: {e}")
|
179 |
-
# curr_struct[file_name] = {}
|
180 |
# return structure
|
181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
def build_repo_structure(root_path):
|
183 |
"""Build repository structure using improved parsing method"""
|
184 |
return create_structure(root_path)
|
|
|
22 |
pp_string += ' ' * spacing + str(key) + '\n'
|
23 |
else:
|
24 |
pp_string += ' ' * spacing + str(key) + '/' + '\n'
|
25 |
+
if 'text' not in value:
|
26 |
pp_string += show_project_structure(value, spacing + 4)
|
27 |
|
28 |
return pp_string
|
|
|
112 |
)
|
113 |
return class_info, function_names, file_content.splitlines()
|
114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
# def create_structure(directory_path):
|
116 |
# """Create the structure of the repository directory by parsing Python files.
|
117 |
# :param directory_path: Path to the repository directory.
|
|
|
119 |
# """
|
120 |
# structure = {}
|
121 |
# for root, _, files in os.walk(directory_path):
|
122 |
+
# repo_name = os.path.basename(directory_path)
|
123 |
# relative_root = os.path.relpath(root, directory_path)
|
124 |
# if relative_root == ".":
|
125 |
+
# relative_root = repo_name
|
126 |
+
# curr_struct = structure
|
127 |
+
# for part in relative_root.split(os.sep):
|
128 |
+
# if part not in curr_struct:
|
129 |
+
# curr_struct[part] = {}
|
130 |
+
# curr_struct = curr_struct[part]
|
|
|
131 |
# for file_name in files:
|
132 |
# if file_name.endswith(".py"):
|
133 |
# file_path = os.path.join(root, file_name)
|
|
|
138 |
# "text": file_lines,
|
139 |
# }
|
140 |
# else:
|
141 |
+
# curr_struct[file_name] = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
# return structure
|
143 |
|
144 |
+
def create_structure(directory_path):
|
145 |
+
"""Create the structure of the repository directory by parsing Python files.
|
146 |
+
:param directory_path: Path to the repository directory.
|
147 |
+
:return: A dictionary representing the structure.
|
148 |
+
"""
|
149 |
+
structure = {}
|
150 |
+
for root, dirs, files in os.walk(directory_path):
|
151 |
+
relative_root = os.path.relpath(root, directory_path)
|
152 |
+
# Build the current directory position in the structure
|
153 |
+
if relative_root == ".":
|
154 |
+
curr_struct = structure
|
155 |
+
else:
|
156 |
+
curr_struct = structure
|
157 |
+
# Split by path separator and create directory structure layer by layer
|
158 |
+
path_parts = relative_root.split(os.sep)
|
159 |
+
for part in path_parts:
|
160 |
+
if part not in curr_struct:
|
161 |
+
curr_struct[part] = {}
|
162 |
+
curr_struct = curr_struct[part]
|
163 |
+
# First create empty dictionary structure for all subdirectories
|
164 |
+
for dir_name in dirs:
|
165 |
+
# Skip hidden directories and common ignored directories
|
166 |
+
if not dir_name.startswith('.') and dir_name not in ['__pycache__', 'node_modules']:
|
167 |
+
if dir_name not in curr_struct:
|
168 |
+
curr_struct[dir_name] = {}
|
169 |
+
# Process all files in the current directory
|
170 |
+
for file_name in files:
|
171 |
+
# Skip hidden files and compiled files
|
172 |
+
if file_name.startswith('.') or file_name.endswith('.pyc'):
|
173 |
+
continue
|
174 |
+
file_path = os.path.join(root, file_name)
|
175 |
+
if file_name.endswith(".py"):
|
176 |
+
# Python files: parse class and function information
|
177 |
+
try:
|
178 |
+
class_info, function_names, file_lines = parse_python_file(file_path)
|
179 |
+
curr_struct[file_name] = {
|
180 |
+
"classes": class_info,
|
181 |
+
"functions": function_names,
|
182 |
+
"text": file_lines,
|
183 |
+
}
|
184 |
+
except Exception as e:
|
185 |
+
print(f"Failed to parse Python file {file_path}: {e}")
|
186 |
+
curr_struct[file_name] = {"text": []}
|
187 |
+
else:
|
188 |
+
code_extensions = ['.js', '.ts', '.jsx', '.tsx', '.java', '.cpp', '.c', '.h', '.hpp',
|
189 |
+
'.cs', '.php', '.rb', '.go', '.rs', '.swift', '.kt', '.scala',
|
190 |
+
'.sh', '.bat', '.ps1', '.sql', '.html', '.css', '.scss', '.less',
|
191 |
+
'.json', '.xml', '.yaml', '.yml', '.toml', '.ini', '.cfg', '.conf',
|
192 |
+
'.md', '.txt', '.rst', '.tex', '.r', '.R', '.m', '.pl', '.lua']
|
193 |
+
if any(file_name.endswith(ext) for ext in code_extensions):
|
194 |
+
try:
|
195 |
+
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
196 |
+
file_content = f.read()
|
197 |
+
curr_struct[file_name] = {"text": file_content.splitlines()}
|
198 |
+
except Exception as e:
|
199 |
+
print(f"Failed to read file {file_path}: {e}")
|
200 |
+
curr_struct[file_name] = {"text": []}
|
201 |
+
else:
|
202 |
+
curr_struct[file_name] = {"text": []}
|
203 |
+
return structure
|
204 |
+
|
205 |
def build_repo_structure(root_path):
|
206 |
"""Build repository structure using improved parsing method"""
|
207 |
return create_structure(root_path)
|