nva-Zunko / Create_InfoFiles_from_Readme.py
t1's picture
Upload 42 files
51b0588 verified
raw
history blame
No virus
3.29 kB
import os
import json
import sys
EXTRA_FILE = "ponyDiffusionV6XL_LoRAReadme.txt" # Readmeのファイル名
class Section:
def __init__(self, title, trigger, content):
self.title = title
self.trigger = trigger
self.content = content
def write_to_file(self):
filename = f"{self.title}.txt"
if filename == "このファイルについて.txt":
pass
elif not os.path.exists(filename):
with open(filename, 'w', encoding='utf-8-sig') as file: # UTF-8 BOM付きで書き込む
file.write(self.content)
print(f"{section.title}.txtと{section.title}.jsonを出力しました")
else:
pass
# print(f"File {filename} は既に出力済みです")
def write_to_json(self):
filename = f"{self.title}.json"
if filename == "このファイルについて.json":
pass
elif not os.path.exists(filename):
data = {
"description": self.content,
"sd version": "SDXL",
"activation text": self.trigger,
"preferred weight": 0,
"negative text": "",
"notes": ""
}
with open(filename, 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=4)
else:
pass
# print(f"File {filename} は既に出力済みです")
def extract_sections(filename):
sections = {}
with open(filename, 'r', encoding='utf-8') as file:
lines = file.readlines()
current_title = ""
current_trigger = ""
current_content = []
inside_section = False
for line in lines:
stripped_line = line.strip()
if stripped_line.startswith('■'):
if inside_section:
sections[current_title] = Section(current_title, current_trigger, "\n".join(current_content))
current_title = stripped_line[1:].strip()
current_trigger = ""
current_content = []
inside_section = True
elif "で呼び出し" in stripped_line:
current_trigger = stripped_line.replace("で呼び出し", "").strip()
current_content.append(stripped_line)
elif stripped_line == "":
if inside_section:
sections[current_title] = Section(current_title, current_trigger, "\n".join(current_content))
current_title = ""
current_trigger = ""
current_content = []
inside_section = False
else:
current_content.append(stripped_line)
if inside_section:
sections[current_title] = Section(current_title, current_trigger, "\n".join(current_content))
return sections
if __name__ == "__main__":
#READMEを読み込み、txtとjsonを作成
try:
sections = extract_sections(EXTRA_FILE)
except:
print(f"{EXTRA_FILE}が存在しません。")
sys.exit()
for title, section in sections.items():
section.write_to_file()
section.write_to_json()