-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_utils.py
58 lines (51 loc) · 2 KB
/
file_utils.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
import os
OUTPUT_DIRECTORY = "ai_generated_files"
CODE_SUBDIRECTORY = "code"
TEST_SUBDIRECTORY = "tests"
IMAGE_SUBDIRECTORY = "images"
CODE_FILES = set()
def sanitize_folder_name(name):
import re
"""
Transform the user's prompt into a valid folder name.
"""
# Keep only alphanumeric characters and spaces
sanitized_name = re.sub(r'[^a-zA-Z0-9 ]', '', name)
# Replace spaces with underscores and lowercase everything
sanitized_name = sanitized_name.replace(' ', '_').lower()
# Limit the folder name's length to 50 characters
return sanitized_name[:50]
def create_unique_folder(base_name):
"""
Create a unique folder based on the base name.
If the folder already exists, append a number to make it unique.
"""
counter = 1
folder_name = base_name
while os.path.exists(os.path.join(OUTPUT_DIRECTORY, folder_name)):
folder_name = f"{base_name}_{counter}"
counter += 1
os.makedirs(os.path.join(OUTPUT_DIRECTORY, folder_name))
return folder_name
def write_to_file(project_folder_name, subdirectory, filename, content):
"""
Save file to the specified subdirectory.
"""
filepath = os.path.join(OUTPUT_DIRECTORY, project_folder_name, subdirectory, filename)
with open(filepath, 'w') as f:
f.write(content)
print(f"[File Created] {os.path.abspath(filepath)}")
if subdirectory == CODE_SUBDIRECTORY: # Only add to the set if it's a code file and if it's not already in the set
CODE_FILES.add(filepath)
def get_code_directory(project_folder_name):
return os.path.join(OUTPUT_DIRECTORY, project_folder_name, CODE_SUBDIRECTORY)
def get_image_directory(project_folder_name):
return os.path.join(OUTPUT_DIRECTORY, project_folder_name, IMAGE_SUBDIRECTORY)
def file_to_string(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()
except FileNotFoundError:
return "File not found."
except Exception as e:
return f"An error occurred: {e}"