|
def get_all_files_in_directory(directory, ext=''):
|
|
import os
|
|
import re
|
|
custom_sort_key_re = re.compile('([0-9]+)')
|
|
|
|
def custom_sort_key(s):
|
|
|
|
return [int(x) if x.isdigit() else x for x in custom_sort_key_re.split(s)]
|
|
|
|
all_files = []
|
|
for root, dirs, files in os.walk(directory):
|
|
for file in files:
|
|
if file.endswith(ext):
|
|
file_path = os.path.join(root, file)
|
|
all_files.append(file_path)
|
|
return sorted(all_files, key=custom_sort_key)
|
|
|
|
|
|
def clearT():
|
|
import unicodedata
|
|
|
|
def full2half(input_str):
|
|
return ''.join([unicodedata.normalize('NFKC', char) for char in input_str])
|
|
|
|
|
|
def _clearT(s):
|
|
s = full2half(s)
|
|
return s.strip().replace('\n', '\\n')
|
|
|
|
return _clearT
|
|
|
|
|
|
clearT = clearT()
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
reg_removeWait = re.compile(r'\[.+?]')
|
|
|
|
|
|
def removeWait(_line):
|
|
_tmp = reg_removeWait.sub('', _line)
|
|
if _tmp:
|
|
return _tmp
|
|
else:
|
|
return _line
|
|
|
|
|
|
|
|
def startsWithAny(s: str, keys):
|
|
for x in keys:
|
|
if s.startswith(x):
|
|
return x
|
|
else:
|
|
return False
|
|
|
|
|
|
def indexWithAny(s: str, keys):
|
|
for x in keys:
|
|
if x in s:
|
|
idx = s.index(x)
|
|
return x, s[:idx], s[idx:]
|
|
else:
|
|
return False
|
|
|
|
|
|
def endsWithAny(s: str, keys):
|
|
for x in keys:
|
|
if s.endswith(x):
|
|
return x
|
|
else:
|
|
return False
|
|
|
|
|
|
def simpleSplit(_s: str, _sp, _st=0, _shift=True):
|
|
_idx = _s.index(_sp, _st)
|
|
if _shift:
|
|
return _s[:_idx], _s[_idx + len(_sp):]
|
|
else:
|
|
return _s[:_idx], _s[_idx:]
|
|
|
|
|
|
|
|
|
|
def startsWithAlnum(s: str, _chrs):
|
|
retn = ''
|
|
for char in s:
|
|
if (char.isalnum()) or (char in _chrs):
|
|
retn += char
|
|
else:
|
|
break
|
|
return retn
|
|
|
|
|
|
def startsWithCmd(s: str, _chrs=None):
|
|
if _chrs is None:
|
|
_chrs = {'_', '@'}
|
|
cmd = startsWithAlnum(s, _chrs)
|
|
if cmd:
|
|
return s.startswith(cmd + ' ')
|
|
else:
|
|
return False
|
|
|
|
|
|
def getCmdArgs(_cmd):
|
|
_args = ['']
|
|
_i = -1
|
|
_inQuota = ''
|
|
while _i < len(_cmd) - 1:
|
|
_i += 1
|
|
char = _cmd[_i]
|
|
if _inQuota:
|
|
_args[-1] += char
|
|
if char == _inQuota:
|
|
_inQuota = ''
|
|
elif char == '\\':
|
|
_i += 1
|
|
char = _cmd[_i]
|
|
_args[-1] += char
|
|
else:
|
|
pass
|
|
else:
|
|
if char == ' ':
|
|
_args.append('')
|
|
while _i < len(_cmd) - 1:
|
|
_i += 1
|
|
char = _cmd[_i]
|
|
if char != ' ':
|
|
_i -= 1
|
|
break
|
|
continue
|
|
elif char in {'"', "'"}:
|
|
_inQuota = char
|
|
else:
|
|
pass
|
|
_args[-1] += char
|
|
continue
|
|
return _args
|
|
|
|
|
|
def args2map(_args):
|
|
retn = {}
|
|
for _i in range(1, len(_args)):
|
|
_arg = _args[_i]
|
|
if '=' not in _arg:
|
|
if '=' in _arg:
|
|
_arg = _arg.replace('=', '=')
|
|
else:
|
|
continue
|
|
_idx = _arg.index('=')
|
|
_k = _arg[:_idx]
|
|
if '"' in _k or "'" in _k:
|
|
continue
|
|
_v = _arg[_idx + 1:]
|
|
if _v.startswith('"') or _v.startswith("'"):
|
|
_v = _v[1:len(_v) - 1]
|
|
retn[_k] = _v
|
|
return retn
|
|
|
|
|
|
def getCmdArgsMap(_cmd):
|
|
return args2map(getCmdArgs(_cmd))
|
|
|
|
|
|
|
|
def getBracketContent(data, w_i, _left='{', _right='}', _start=0):
|
|
retn = _left
|
|
_start += 1
|
|
w_i -= 1
|
|
while w_i < len(data) - 1:
|
|
w_i += 1
|
|
_line: str = data[w_i] + '\n'
|
|
|
|
w_j = _start - 1
|
|
_inQuota = ''
|
|
while w_j < len(_line) - 1:
|
|
w_j += 1
|
|
char = _line[w_j]
|
|
|
|
if _inQuota:
|
|
retn += char
|
|
if char == _inQuota:
|
|
_inQuota = ''
|
|
elif char == '\\':
|
|
w_j += 1
|
|
char = _line[w_j]
|
|
retn += char
|
|
else:
|
|
pass
|
|
else:
|
|
if char in {'"', "'"}:
|
|
_inQuota = char
|
|
retn += char
|
|
elif char == _left:
|
|
w_i, _retn, _line, w_j = getBracketContent(data, w_i, _left, _right, w_j)
|
|
retn += _retn
|
|
elif char == _right:
|
|
retn += char
|
|
return w_i, retn, _line, w_j
|
|
else:
|
|
retn += char
|
|
_start = 0
|
|
return w_i, retn, '', 0
|
|
|
|
|
|
def ast2list(_ast: str, _start=0):
|
|
retn = [['', '']]
|
|
|
|
w_j = _start
|
|
_inQuota = ''
|
|
while w_j < len(_ast) - 1:
|
|
w_j += 1
|
|
char = _ast[w_j]
|
|
|
|
if _inQuota:
|
|
if char == _inQuota:
|
|
_inQuota = ''
|
|
elif char == '\\':
|
|
retn[-1][1] += char
|
|
w_j += 1
|
|
char = _ast[w_j]
|
|
retn[-1][1] += char
|
|
else:
|
|
retn[-1][1] += char
|
|
else:
|
|
if char in {'"', "'"}:
|
|
_inQuota = char
|
|
|
|
elif char == '{':
|
|
_retn, w_j = ast2list(_ast, w_j)
|
|
retn[-1][1] = _retn
|
|
elif char == '}':
|
|
return retn, w_j
|
|
elif char == ',':
|
|
retn.append(['', ''])
|
|
elif char == '=':
|
|
pass
|
|
else:
|
|
if char.strip():
|
|
retn[-1][0] += char
|
|
|
|
return [['', '']], 0
|
|
|
|
def astStrip(_astOj, _sk=None, _sv=None):
|
|
retn = []
|
|
for k, v in _astOj:
|
|
if _sk and (k in _sk):
|
|
continue
|
|
if type(v) is list:
|
|
v = astStrip(v, _sk, _sv)
|
|
if not v:
|
|
continue
|
|
if type(v) is str:
|
|
if _sv and (v in _sv):
|
|
continue
|
|
retn.append((k, v))
|
|
return retn
|
|
|
|
def astGet(_astOj, _key=''):
|
|
if _key:
|
|
for k, v in _astOj:
|
|
if k == _key:
|
|
return v
|
|
else:
|
|
_d = ''
|
|
for k, v in _astOj:
|
|
if not k:
|
|
if type(v) is str:
|
|
_d += v
|
|
return _d
|
|
|
|
|
|
|
|
brackets = {'「': '」', '『': '』', '(': ')', '“': '”'}
|
|
brackets_r = {v: k for k, v in brackets.items()}
|
|
|
|
cache = []
|
|
cache_tmp = ''
|
|
cache_var_n = ''
|
|
|
|
|
|
def log_process(var_d: str, line, _n):
|
|
global cache, cache_tmp, cache_var_n
|
|
var_d = var_d.strip()
|
|
if var_d.startswith('【'):
|
|
var_n, var_d = simpleSplit(var_d, '】')
|
|
if var_n in _n:
|
|
var_n = _n[var_n]
|
|
else:
|
|
_n[var_n] = clearT(var_n[1:]).replace('???', '?')
|
|
print(line)
|
|
var_d = var_d.strip()
|
|
if cache:
|
|
sp = cache_var_n + '<sp>:' + clearT(' '.join(cache)) + '\n'
|
|
cache = []
|
|
else:
|
|
sp = ''
|
|
|
|
tmp = startsWithAny(var_d, brackets)
|
|
if tmp:
|
|
if var_d.endswith(brackets[tmp]):
|
|
return sp + var_n + ':' + clearT(var_d)
|
|
else:
|
|
cache.append(var_d)
|
|
cache_tmp = brackets[tmp]
|
|
cache_var_n = var_n
|
|
return sp.rstrip()
|
|
else:
|
|
return sp + var_n + '<sp>:' + clearT(var_d)
|
|
else:
|
|
if cache:
|
|
cache.append(var_d)
|
|
if var_d.endswith(cache_tmp):
|
|
sp = cache_var_n + ':' + clearT(' '.join(cache))
|
|
cache = []
|
|
return sp
|
|
else:
|
|
return ''
|
|
else:
|
|
return '旁白:' + clearT(var_d) |