| | 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 |
| | |
| | |
| | |
| |
|
| | 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") |
| | |
| | print(f"wildcards card_path : ", card_path , style="bold CYAN") |
| |
|
| | |
| | |
| | |
| | resub = re.compile(r"(\{)(((\d+)|(\d+)?-(\d+)?)?\$\$(([^\{\}]*?)\$\$)?)?([^\{\}]*)(\})") |
| | recard = re.compile(r"(__)(.*?)(__)") |
| |
|
| | |
| | is_card_Load = False |
| | cards = {} |
| | seperator=", " |
| | loop_max=50 |
| |
|
| | |
| | def sub(match): |
| | |
| | try: |
| | |
| | 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))) |
| | |
| | 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=seperator.join( |
| | random.sample( |
| | m, |
| | random.randint( |
| | a,b |
| | ) |
| | ) |
| | ) |
| | |
| | |
| | else: |
| | r=seperator.join( |
| | random.sample( |
| | m, |
| | random.randint( |
| | 0,c |
| | ) |
| | ) |
| | ) |
| | |
| | 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) |
| | |
| | if bak==tmp : |
| | return tmp |
| | bak=tmp |
| | return bak |
| |
|
| | |
| | def card(match): |
| | |
| | lst=fnmatch.filter(wildcards.cards, match.group(2)) |
| | if len(lst)>0: |
| | |
| | cd=random.choice(lst) |
| | |
| | r=random.choice(wildcards.cards[cd]) |
| | else : |
| | r= match.group(2) |
| | |
| | return r |
| | |
| |
|
| | |
| | def card_loop(text): |
| | bak=text |
| | for i in range(1, wildcards.loop_max): |
| | tmp=wildcards.recard.sub(wildcards.card, bak) |
| | |
| | if bak==tmp : |
| | tmp=wildcards.sub_loop(tmp) |
| | |
| | if bak==tmp : |
| | |
| | return tmp |
| | bak=tmp |
| | |
| | return bak |
| | |
| | |
| | def card_load(): |
| | |
| | card_path=wildcards.card_path |
| | cards = {} |
| | |
| | files=glob.glob(card_path, recursive=True) |
| | |
| | |
| | for file in files: |
| | basenameAll = os.path.basename(file) |
| | basename = os.path.relpath(file, os.path.dirname(__file__)).replace("\\", "/").replace("../../wildcards/", "") |
| | |
| | |
| | file_nameAll = os.path.splitext(basenameAll)[0] |
| | file_name = "/"+os.path.splitext(basename)[0] |
| | |
| | |
| | if not file_nameAll in cards: |
| | cards[file_nameAll]=[] |
| | if not file_name in cards: |
| | cards[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] |
| | |
| | wildcards.cards=cards |
| | print(f"[cyan]cards file count : [/cyan]", len(wildcards.cards)) |
| | |
| | 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() |
| |
|
| | |
| | result=wildcards.card_loop(text) |
| | |
| | return result |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | 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__")) |
| |
|