This repository was archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_file.py
More file actions
45 lines (33 loc) · 1.25 KB
/
Copy pathwrite_file.py
File metadata and controls
45 lines (33 loc) · 1.25 KB
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
import os
import re
def write(chat, direct, filename=None):
regex = r"(\S+)\n\s*```[^\n]*\n(.+?)```"
matches = re.finditer(regex, chat, re.DOTALL)
files = []
for match in matches:
# Strip the filename of any non-allowed characters and convert / to \
path = re.sub(r'[<>"|?*]', "", match.group(1))
# Remove leading and trailing brackets
path = re.sub(r"^\[(.*)\]$", r"\1", path)
# Remove leading and trailing backticks
path = re.sub(r"^`(.*)`$", r"\1", path)
# Remove trailing ]
path = re.sub(r"\]$", "", path)
path = re.sub(r":", "", path)
path = re.sub(r"`", "", path)
# Get the code
code = match.group(2)
# Add the file to the list
files.append((path, code))
# Get all the text before the first ``` block
# readme = chat.split("```")[0]
# files.append(("README.md", readme))
for file_name, file_content in files:
if filename:
file_path = os.path.join(direct, filename)
else:
file_path = os.path.join(direct, file_name)
dirt = os.path.dirname(file_path)
os.makedirs(dirt, exist_ok=True)
with open(file_path, "w") as file:
file.write(file_content)