-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathoneoff.py
54 lines (46 loc) · 1.83 KB
/
oneoff.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
"""
'oneoff.py' is a script that performs one-off operations on the GPT files
- Reformat all the GPT files in the source path and save them to the destination path.
"""
from gptparser import GptMarkdownFile
from typing import Tuple
import os
def reformat_gpt_files(src_path: str, dst_path: str) -> Tuple[bool, str]:
"""
Reformat all the GPT files in the source path and save them to the destination path.
:param src_path: str, path to the source directory.
:param dst_path: str, path to the destination directory.
"""
if not os.path.exists(src_path):
return (False, f"Source path '{src_path}' does not exist.")
if not os.path.exists(dst_path):
os.makedirs(dst_path)
print(f"Reformatting GPT files in '{src_path}' and saving them to '{dst_path}'...")
nb_ok = nb_total = 0
for src_file_path in os.listdir(src_path):
_, ext = os.path.splitext(src_file_path)
if ext != '.md':
continue
nb_total += 1
dst_file_path = os.path.join(dst_path, src_file_path)
src_file_path = os.path.join(src_path, src_file_path)
ok, gpt = GptMarkdownFile.parse(src_file_path)
if ok:
ok, msg = gpt.save(dst_file_path)
if ok:
id = gpt.id()
if id:
info = f"; id={id.id}"
if id.name:
info += f", name='{id.name}'"
else:
info = ''
print(f"[+] saved '{os.path.basename(src_file_path)}'{info}")
nb_ok += 1
else:
print(f"[!] failed to save '{src_file_path}': {msg}")
else:
print(f"[!] failed to parse '{src_file_path}': {gpt}")
msg = f"Reformatted {nb_ok} out of {nb_total} GPT files."
ok = nb_ok == nb_total
return (ok, msg)