forked from stitionai/devika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_code.py
35 lines (29 loc) · 1.1 KB
/
read_code.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
import os
from src.config import Config
"""
TODO: Replace this with `code2prompt` - https://github.com/mufeedvh/code2prompt
"""
class ReadCode:
def __init__(self, project_name: str):
config = Config()
project_path = config.get_projects_dir()
self.directory_path = os.path.join(project_path, project_name.lower().replace(" ", "-"))
def read_directory(self):
files_list = []
for root, _dirs, files in os.walk(self.directory_path):
for file in files:
try:
file_path = os.path.join(root, file)
with open(file_path, 'r') as file_content:
files_list.append({"filename": file_path, "code": file_content.read()})
except:
pass
return files_list
def code_set_to_markdown(self):
code_set = self.read_directory()
markdown = ""
for code in code_set:
markdown += f"### {code['filename']}:\n\n"
markdown += f"```\n{code['code']}\n```\n\n"
markdown += "---\n\n"
return markdown