forked from lilly1987/ComfyUI_node_Lilly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwildcards.py
141 lines (108 loc) · 4.05 KB
/
wildcards.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import random
import re
import os
import chardet
import glob
if (
__name__ == os.path.splitext(os.path.basename(__file__))[0]
or __name__ == "__main__"
):
from ConsoleColor import print, console, ccolor
else:
from .ConsoleColor import print, console, ccolor
# ============================================================
class wildcards:
# List of files to be imported
directory = os.path.join(os.path.dirname(__file__), "..", "..", "wildcards")
file_extension = "txt"
print("wildcard files path : ", directory, style="bold CYAN")
resub = re.compile(
r"(\{)(((\d+)|(\d+)?-(\d+)?)?\$\$(([^\{\}]*?)\$\$)?)?([^\{\}]*)(\})"
)
# List of cards
# is_card_Load = False
cards = {}
separator = ", "
loop_max = 50
# Fetch
def sub(match):
try:
# m=match.group(2)
separator = wildcards.separator
s = match.group(3)
m = match.group(9).split("|")
p = match.group(8)
if p:
separator = p
if s is None:
return random.choice(m)
c = len(m)
n = int(match.group(4)) if match.group(4) else None
if n:
r = separator.join(random.sample(m, min(n, c)))
return r
n1 = match.group(5)
n2 = match.group(6)
if n1 or n2:
a = min(int(n1 if n1 else c), int(n2 if n2 else c), c)
b = min(max(int(n1 if n1 else 0), int(n2 if n2 else 0)), c)
r = separator.join(random.sample(m, random.randint(a, b)))
else:
r = separator.join(random.sample(m, random.randint(0, c)))
return r
except Exception as e:
console.print_exception()
return ""
# Repeat the process of retrieving from among the cards. Also handle the ones separated by |.
def card_loop(text):
bak = text
for i in range(1, wildcards.loop_max):
tmp = wildcards.resub.sub(wildcards.sub, bak)
if bak == tmp:
return tmp
bak = tmp
return bak
# Execute
def run(input_text, load=False):
text = input_text
if text is None or not isinstance(text, str):
print("[red]text is not str : [/red]", text)
return None
matches = re.findall(r"__(.*?)__", text)
for match in matches:
pattern = match
card_file = f"{wildcards.directory}/{pattern}.{wildcards.file_extension}"
recursive = False
multiple = False
if "*" in pattern:
multiple = True
if "**" in pattern:
parts = pattern.split("**", 1)
recursive = True
pattern = f"{parts[0]}"
card_file = (
f"{wildcards.directory}/{pattern}/**/*.{wildcards.file_extension}"
)
if multiple:
files = glob.glob(card_file, recursive=recursive)
random_file = random.choice(files)
card_file = random_file
try:
with open(card_file, "rb") as f:
raw_data = f.read()
encoding = chardet.detect(raw_data)["encoding"]
with open(card_file, "r", encoding=encoding) as f:
lines = [
line.strip()
for line in f
if line.strip() and not line.startswith("#")
]
if lines:
random_line = random.choice(lines)
match = re.escape(match)
text = re.sub(f"__{match}__", random_line, text, count=1)
except (FileNotFoundError, IOError) as error:
print(f"Error reading file {card_file}: {error}")
result = wildcards.card_loop(text)
return result
# print("wildcards test : "+wildcards.run("{3$$a1|{b2|c3|}|d4|{-$$|f|g}|{-2$$h||i}|{1-$$j|k|}}/{$$l|m|}/{0$$n|}"))