File size: 7,711 Bytes
baa8e90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import os
import glob
import sys
import random
import re
import fnmatch

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
#print(__file__)
#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:

    # ๊ฐ€์ ธ์˜ฌ ํŒŒ์ผ ๋ชฉ๋ก
    card_path = os.path.join(os.path.dirname(__file__), "..", "..", "wildcards", "**", "*.txt")
    #card_path=f"{os.getcwd()}\\wildcards\\**\\*.txt"
    print(f"wildcards card_path : ", card_path , style="bold CYAN")

    # ์ •๊ทœ์‹
    #resub  = re.compile(r"(\{)([^\{\}]*)(\})")
    #resub  = re.compile(r"(\{)(((\d+)|(\d+)?-(\d+)?)?\$\$((.*)?\$\$)?)?([^\{\}]*)(\})")
    resub  = re.compile(r"(\{)(((\d+)|(\d+)?-(\d+)?)?\$\$(([^\{\}]*?)\$\$)?)?([^\{\}]*)(\})")
    recard = re.compile(r"(__)(.*?)(__)")

    # ์นด๋“œ ๋ชฉ๋ก
    is_card_Load = False
    cards = {}
    seperator=", "
    loop_max=50

    # | ๋กœ ์ž…๋ ฅ๋œ๊ฒƒ์ค‘ ํ•˜๋‚˜ ๊ฐ€์ ธ์˜ค๊ธฐ
    def sub(match):
        #print(f"sub : {(match.groups())}")
        try:        
            #m=match.group(2)
            seperator=wildcards.seperator
            s=match.group(3)
            m=match.group(9).split("|")
            p=match.group(8)
            if p:
                seperator=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=seperator.join(random.sample(m,min(n,c)))
                #print(f"n : {n} ; {r}")
                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)
                #print(f"ab : {a} ; {b}")
                r=seperator.join(
                    random.sample(
                        m,
                        random.randint(
                            a,b
                        )
                    )
                )
                #n1=int(match.group(5)) if not match.group(5) is None 
                #n2=int(match.group(6)) if not match.group(6) is None 
            else:
                r=seperator.join(
                    random.sample(
                        m,
                        random.randint(
                            0,c
                        )
                    )
                )
            #print(f"12 : {r}")
            return r


        except Exception as e:         
            console.print_exception()
            return ""
            
            

    # | ๋กœ ์ž…๋ ฅ๋œ๊ฒƒ์ค‘ ํ•˜๋‚˜ ๊ฐ€์ ธ์˜ค๊ธฐ ๋ฐ˜๋ณต
    def sub_loop(text):
        bak=text
        for i in range(1, wildcards.loop_max):
            tmp=wildcards.resub.sub(wildcards.sub, bak)
            #print(f"tmp : {tmp}")
            if bak==tmp :
                return tmp
            bak=tmp
        return bak

    # ์นด๋“œ ์ค‘์—์„œ ๊ฐ€์ ธ์˜ค๊ธฐ
    def card(match):
        #print(f"card in  : {match.group(2)}")
        lst=fnmatch.filter(wildcards.cards, match.group(2))
        if len(lst)>0:
            #print(f"card lst : {lst}")
            cd=random.choice(lst)
            #print(f"card get : {cd}")
            r=random.choice(wildcards.cards[cd])        
        else :    
            r= match.group(2)
        #print(f"card out : {r}")
        return r
        

    # ์นด๋“œ ์ค‘์—์„œ ๊ฐ€์ ธ์˜ค๊ธฐ ๋ฐ˜๋ณต. | ์˜ ๊ฒƒ๋„ ์ฒ˜๋ฆฌ
    def card_loop(text):
        bak=text
        for i in range(1, wildcards.loop_max):
            tmp=wildcards.recard.sub(wildcards.card, bak)
            #print(f"card l : {bak}")
            if bak==tmp :
                tmp=wildcards.sub_loop(tmp)
                
            if bak==tmp :
                #print(f"card le : {bak}")
                return tmp
            bak=tmp
        #print(f"card le : {bak}")
        return bak
        
    # ์นด๋“œ ํŒŒ์ผ ์ฝ๊ธฐ
    def card_load():
        #cards=wildcards.cards
        card_path=wildcards.card_path
        cards = {}
        #print(f"path : {path}")
        files=glob.glob(card_path, recursive=True)
        #print(f"files : {files}")
        
        for file in files:
            basenameAll = os.path.basename(file)
            basename = os.path.relpath(file, os.path.dirname(__file__)).replace("\\", "/").replace("../../wildcards/", "")
            #print(f"basenameAll : {basenameAll}")
            #print(f"basename : {basename}")
            file_nameAll = os.path.splitext(basenameAll)[0]
            file_name = "/"+os.path.splitext(basename)[0]
            #print(f"file_nameAll : {file_nameAll}")
            #print(f"file_name : {file_name}")
            if not file_nameAll in cards:
                cards[file_nameAll]=[]
            if not file_name in cards:
                cards[file_name]=[]
            #print(f"file_name : {file_name}")
            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()}")
        wildcards.is_card_Load=True

    # ์‹คํ–‰๊ธฐ
    def run(text,load=False):
        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:
            wildcards.card_load()

        #print(f"text : {text}")
        result=wildcards.card_loop(text)
        #print(f"result : {result}")
        return result
        
    # ============================================================

#m = p.sub(sub, test)
#print(m)
#print(__name__)
#if __name__ == '__main__' :
    # ํ…Œ์ŠคํŠธ์šฉ
#test="{3$$a1|{b2|c3|}|d4|{-$$|f|g}|{-2$$h||i}|{1-$$j|k|}}/{$$l|m|}/{0$$n|}/{9$$-$$a|b|c}/{9$$ {and|or} $$a|b|c}"
#print("[green]wildcards test : [/green]",wildcards.run(test),style="reset")
#print("wildcards test : "+wildcards.run("{9$$a|b}"))
#print("[green]wildcards test : [/green]",wildcards.run("__my__"))
#print("wildcards test : "+wildcards.run("{9$$-$$a|b|c}"))
#print("wildcards test : "+wildcards.run("{9$$ {and|or} $$a|b|c}"))
#print("wildcards test : "+wildcards.run("{{slender,|} {nature,|} {curvy,|} {thin,|} {narrow,|} {slim,|} {mini,|} {little,|}| {|very }{-$$ $$thin|slender|narrow|slim|little|skinny|mini} body, }"))
print("wildcards test : "+wildcards.run("__aest__"))
print("wildcards test : "+wildcards.run("__*test__"))
print("wildcards test : "+wildcards.run("__?est__"))
print("wildcards test : "+wildcards.run("__test__"))
print("wildcards test : "+wildcards.run("__/test__"))
print("wildcards test : "+wildcards.run("__/0/test__"))