From 7186f2c895b660aa4ac4356cdfaa8f1f71571342 Mon Sep 17 00:00:00 2001 From: lilly Date: Tue, 18 Jul 2023 18:44:10 +0900 Subject: [PATCH] Update wildcards.py https://github.com/lilly1987/ComfyUI_node_Lilly/commit/0545b7e3f39e0f28c02319f96202f0c03413995d supports .txt files in different encodings. --- wildcards.py | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/wildcards.py b/wildcards.py index 54fa6d7..7ee64cc 100644 --- a/wildcards.py +++ b/wildcards.py @@ -10,6 +10,19 @@ else: #print(os.path.basename(__file__)) #print(os.getcwd()) +import subprocess +import pkg_resources + +required = {'chardet'} +installed = {pkg.key for pkg in pkg_resources.working_set} +missing = required - installed + +if missing: + python = sys.executable + subprocess.check_call([python, '-m', 'pip', 'install', *missing], stdout=subprocess.DEVNULL) + +import chardet + # ============================================================ class wildcards: @@ -149,16 +162,19 @@ class wildcards: if not file_name in cards: cards[file_name]=[] #print(f"file_name : {file_name}") - f = open(file, 'r', encoding='UTF8') - lines = f.readlines() - for line in lines: - line=line.strip() - # 주석 빈줄 제외 - if line.startswith("#") or len(line)==0: - continue - cards[file_nameAll]+=[line] - cards[file_name]+=[line] - #print(f"line : {line}") + with open(file, "rb") as f: + raw_data = f.read() + encoding = chardet.detect(raw_data)["encoding"] + with open(file, "r", encoding=encoding) as f: + lines = f.readlines() + for line in lines: + line=line.strip() + # 주석 빈줄 제외 + if line.startswith("#") or len(line)==0: + continue + cards[file_nameAll]+=[line] + cards[file_name]+=[line] + #print(f"line : {line}") wildcards.cards=cards print(f"[cyan]cards file count : [/cyan]", len(wildcards.cards)) #print(f"cards : {cards.keys()}") @@ -166,7 +182,7 @@ class wildcards: # 실행기 def run(text,load=False): - if text is None or type(text) is not str: + if text is None or not isinstance(text, str): print("[red]text is not str : [/red]",text) return None if not wildcards.is_card_Load or load: