txtp_segmenter: add names with regex

This commit is contained in:
bnnm 2021-10-10 11:24:03 +02:00
parent d71bbc16be
commit 472141b1a8

View File

@ -1,10 +1,6 @@
# !/usr/bin/python
import os
import sys
import glob
import argparse
import re
import os, glob, argparse, re
def parse():
@ -12,28 +8,34 @@ def parse():
"creates segmented .txtp from a list of files obtained using wildcards"
)
epilog = (
"examples:\n"
"%(prog)s bgm_*.ogg\n"
"- get all files that start with bgm_ and end with .ogg\n"
"%(prog)s bgm_??.* -n bgm_main.txtp -cls 2\n"
"- get all files that start with bgm_ and end with 2 chars plus any extension\n"
"%(prog)s files/bgm_*_all.ogg -s\n"
"- create single .txtp per every bgm_(something)_all.ogg inside files dir\n"
"%(prog)s **/*.ogg -l\n"
"- find all .ogg in all subdirs but list only\n"
"%(prog)s files/*.ogg -f .+(a|all)[.]ogg$\n"
"- find all .ogg in files except those that end with 'a.ogg' or 'all.ogg'\n"
"%(prog)s files/*.ogg -f .+(00[01])[.]ogg$\n"
"- find all .ogg in files that end with '0.ogg' or '1.ogg'\n"
'examples:\n'
'%(prog)s bgm_*.ogg\n'
'- get all files that start with bgm_ and end with .ogg\n'
'%(prog)s bgm_??.* -n bgm_main.txtp -cla\n'
'- get all files that start with bgm_ and end with 2 chars plus any extension + loop\n'
'%(prog)s files/bgm_*_all.ogg -s\n'
'- create single .txtp per every bgm_(something)_all.ogg inside files dir\n'
'%(prog)s **/*.ogg -l\n'
'- list results only\n'
'%(prog)s files/*.ogg -fi .+(00[01])[.]ogg$\n'
'- find all .ogg in files including those that end with 0.ogg or 1.ogg\n'
'%(prog)s files/*.ogg -fe .+(a|all)[.]ogg$\n'
'- find all .ogg in files excluding those that end with a.ogg or all.ogg\n'
'%(prog)s files/*.* -fi "(.+)(_intro|_loop)([.].+)$" -n "\\1)_full" -cfa\n'
'- makes intro+loop .txtp named (first part without _intro/_loop)_full.txtp + loops\n'
'%(prog)s files/*.* -fe "(.+)(_intro|_loop)([.].+)$" -s\n'
'- makes single .txtp for files that don\'t have intro+loop pairs\n'
)
parser = argparse.ArgumentParser(description=description, epilog=epilog, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("files", help="files to match")
parser.add_argument("-n","--name", help="generated txtp name (adapts 'files' by default)")
parser.add_argument("-f","--filter", help="filter matched files with regex and keep rest")
parser.add_argument("-i","--include", help="include matched files with regex and ignore rest")
parser.add_argument("-n","--name", help="generated txtp name (auto from 'files' by default)\nMay use regex groups like '\\1.ogg' when used with filter-include")
parser.add_argument("-fi","--filter-include", help="include files matched with regex and ignore rest")
parser.add_argument("-fe","--filter-exclude", help="exclude files matched with regex and keep rest")
parser.add_argument("-s","--single", help="generate single files per list match", action='store_true')
parser.add_argument("-l","--list", help="list only results and don't write", action='store_true')
parser.add_argument("-l","--list", help="list only results and don't write .txtp", action='store_true')
parser.add_argument("-cla","--command-loop-auto", help="sets auto-loop (last segment)", action='store_true')
parser.add_argument("-clf","--command-loop-force", help="sets auto-loop (last segment) even with 1 segment", action='store_true')
parser.add_argument("-cls","--command-loop-start", help="sets loop start segment")
parser.add_argument("-cle","--command-loop-end", help="sets loop end segment")
parser.add_argument("-cv","--command-volume", help="sets volume")
@ -42,35 +44,36 @@ def parse():
return parser.parse_args()
def is_file_ok(args, glob_file):
if not os.path.isfile(glob_file):
def is_file_ok(args, file):
if not os.path.isfile(file):
return False
if glob_file.endswith(".py"):
if file.endswith(".py"):
return False
if args.filter:
filename_test = os.path.basename(glob_file)
p = re.compile(args.filter)
if p.match(filename_test) != None:
file_test = os.path.basename(file)
if args.filter_exclude:
if args.p_exclude.match(file_test) != None:
return False
if args.include:
filename_test = os.path.basename(glob_file)
p = re.compile(args.include)
if p.match(filename_test) == None:
if args.filter_include:
if args.p_include.match(file_test) == None:
return False
return True
def get_txtp_name(args, segment):
def get_txtp_name(args, file):
txtp_name = ''
if args.name:
if args.filter_include and args.name and '\\' in args.name:
file_test = os.path.basename(file)
txtp_name = args.p_include.sub(args.name, file_test)
elif args.name:
txtp_name = args.name
elif args.single:
txtp_name = os.path.splitext(os.path.basename(segment))[0]
txtp_name = os.path.splitext(os.path.basename(file))[0]
else:
txtp_name = os.path.splitext(os.path.basename(args.files))[0]
@ -80,45 +83,45 @@ def get_txtp_name(args, segment):
if txtp_name.endswith('_'):
txtp_name = txtp_name[:-1]
if txtp_name == '':
txtp_name = 'bgm'
if not txtp_name:
txtp_name = 'bgm'
if not txtp_name.endswith(".txtp"):
txtp_name += ".txtp"
return txtp_name
def main():
args = parse()
if args.filter_include:
args.p_include = re.compile(args.filter_include)
if args.filter_exclude:
args.p_exclude = re.compile(args.filter_exclude)
# get target files
glob_files = glob.glob(args.files)
files = glob.glob(args.files)
# process matches and add to output list
files = []
segments = []
for glob_file in glob_files:
if not is_file_ok(args, glob_file):
txtps = {}
for file in files:
if not is_file_ok(args, file):
continue
if args.single:
name = get_txtp_name(args, glob_file)
segments = [glob_file]
files.append( (name,segments) )
else:
segments.append(glob_file)
name = get_txtp_name(args, file)
if not name in txtps:
txtps[name] = []
txtps[name].append(file)
if not args.single:
name = get_txtp_name(args, '')
files.append( (name,segments) )
if not files or not segments:
if not txtps:
print("no files found")
exit()
# list info
for name, segments in files:
for name, segments in txtps.items():
print("file: " + name)
for segment in segments:
print(" " + segment)
@ -127,10 +130,17 @@ def main():
exit()
# write resulting files
for name, segments in files:
for name, segments in txtps.items():
len_segments = len(segments)
with open(name,"w+") as ftxtp:
for segment in segments:
ftxtp.write(segment + "\n")
for i, segment in enumerate(segments):
if args.command_loop_force and len_segments == 1:
ftxtp.write(segment + " #e\n")
else:
ftxtp.write(segment + "\n")
if args.command_loop_auto or args.command_loop_force and len_segments > 1:
ftxtp.write("loop_mode = auto\n")
if args.command_loop_start:
ftxtp.write("loop_start_segment = " + args.command_loop_start + "\n")
if args.command_loop_end: