mirror of
https://github.com/iluvcapra/mfbatch.git
synced 2025-12-31 08:50:51 +00:00
fixed bugs
This commit is contained in:
@@ -53,7 +53,7 @@ class CommandEnv:
|
|||||||
self.incr = []
|
self.incr = []
|
||||||
|
|
||||||
|
|
||||||
def process_command(metadatums: dict, line: str, dry_run: bool):
|
def handle_command(metadatums: dict, line: str, dry_run: bool):
|
||||||
commandline = line.lstrip(COMMAND_PROCHAR).rstrip("\n")
|
commandline = line.lstrip(COMMAND_PROCHAR).rstrip("\n")
|
||||||
args = shlex.split(commandline)
|
args = shlex.split(commandline)
|
||||||
command = args[0]
|
command = args[0]
|
||||||
@@ -85,7 +85,7 @@ def process_command(metadatums: dict, line: str, dry_run: bool):
|
|||||||
sys.stderr.write(f"Unrecognized command line: {line}\n")
|
sys.stderr.write(f"Unrecognized command line: {line}\n")
|
||||||
|
|
||||||
|
|
||||||
def process_file(metadatums: dict, line: str, dry_run: bool):
|
def process_flac_file(metadatums: dict, line: str, dry_run: bool):
|
||||||
line = line.rstrip("\n")
|
line = line.rstrip("\n")
|
||||||
if dry_run:
|
if dry_run:
|
||||||
sys.stderr.write(f"Would process file: {line}\n")
|
sys.stderr.write(f"Would process file: {line}\n")
|
||||||
@@ -107,39 +107,39 @@ def execute_batch_list(batch_list_path: str, dry_run: bool):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
elif line.startswith(COMMAND_PROCHAR):
|
elif line.startswith(COMMAND_PROCHAR):
|
||||||
process_command(metadatums, line, dry_run)
|
handle_command(metadatums, line, dry_run)
|
||||||
|
|
||||||
elif line == "\n":
|
elif line == "\n":
|
||||||
continue
|
continue
|
||||||
|
|
||||||
else:
|
else:
|
||||||
process_file(metadatums, line, dry_run)
|
process_flac_file(metadatums, line, dry_run)
|
||||||
|
|
||||||
|
|
||||||
def create_cwd_batch_list(command_file: str):
|
def create_cwd_batch_list(command_file: str):
|
||||||
|
metadatums = {}
|
||||||
with open(command_file, mode='w') as f:
|
with open(command_file, mode='w') as f:
|
||||||
f.write("# mfbatch\n\n")
|
f.write("# mfbatch\n\n")
|
||||||
metaflac_command = [METAFLAC_PATH, '--list']
|
metaflac_command = [METAFLAC_PATH, '--list']
|
||||||
metadatums = {}
|
|
||||||
preflight = len(list(iglob('./**/*.flac', recursive=True)))
|
preflight = len(list(iglob('./**/*.flac', recursive=True)))
|
||||||
for path in tqdm(iglob('./**/*.flac', recursive=True),
|
for path in tqdm(iglob('./**/*.flac', recursive=True),
|
||||||
total=preflight):
|
total=preflight):
|
||||||
result = run(metaflac_command + [path], capture_output=True)
|
result = run(metaflac_command + [path], capture_output=True)
|
||||||
this_file_metadata = {}
|
this_file_metadata = {}
|
||||||
for line in result.stdout.decode('utf-8').splitlines():
|
for line in result.stdout.decode('utf-8').splitlines():
|
||||||
m = match(r'^\s+comment\[\d\]: ([A-Z]+)=(.*)$', line)
|
m = match(r'^\s+comment\[\d\]: ([A-Za-z]+)=(.*)$', line)
|
||||||
if m is not None:
|
if m is not None:
|
||||||
this_file_metadata[m[1]] = m[2]
|
this_file_metadata[m[1]] = m[2]
|
||||||
|
|
||||||
for this_key in this_file_metadata.keys():
|
for this_key in this_file_metadata.keys():
|
||||||
if this_key not in metadatums:
|
if this_key not in metadatums:
|
||||||
f.write(f":set {this_key} "
|
f.write(f":set {this_key} "
|
||||||
"{shlex.quote(this_file_metadata[this_key])}\n")
|
f"{shlex.quote(this_file_metadata[this_key])}\n")
|
||||||
metadatums[this_key] = this_file_metadata[this_key]
|
metadatums[this_key] = this_file_metadata[this_key]
|
||||||
else:
|
else:
|
||||||
if this_file_metadata[this_key] != metadatums[this_key]:
|
if this_file_metadata[this_key] != metadatums[this_key]:
|
||||||
f.write(f":set {this_key} "
|
f.write(f":set {this_key} "
|
||||||
"{shlex.quote(this_file_metadata[this_key])}"
|
f"{shlex.quote(this_file_metadata[this_key])}"
|
||||||
"\n")
|
"\n")
|
||||||
metadatums[this_key] = this_file_metadata[this_key]
|
metadatums[this_key] = this_file_metadata[this_key]
|
||||||
|
|
||||||
@@ -156,8 +156,8 @@ def main():
|
|||||||
op = OptionParser(usage="%prog [-c] [-W] [options]")
|
op = OptionParser(usage="%prog [-c] [-W] [options]")
|
||||||
|
|
||||||
op.add_option('-c', '--create',
|
op.add_option('-c', '--create',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help='Create a new list')
|
help='Create a new list')
|
||||||
op.add_option('-W', '--write', default=False,
|
op.add_option('-W', '--write', default=False,
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help="Execute batch list, write to files")
|
help="Execute batch list, write to files")
|
||||||
@@ -177,17 +177,15 @@ def main():
|
|||||||
if options.path is not None:
|
if options.path is not None:
|
||||||
os.chdir(options.path)
|
os.chdir(options.path)
|
||||||
|
|
||||||
command_file = options.batchfile
|
|
||||||
|
|
||||||
if options.create:
|
if options.create:
|
||||||
create_cwd_batch_list(command_file)
|
create_cwd_batch_list(options.batchfile)
|
||||||
|
|
||||||
if options.edit:
|
if options.edit:
|
||||||
editor_command = [os.getenv('EDITOR'), command_file]
|
editor_command = [os.getenv('EDITOR'), options.batchfile]
|
||||||
run(editor_command)
|
run(editor_command)
|
||||||
|
|
||||||
if options.write:
|
if options.write:
|
||||||
execute_batch_list(command_file, dry_run=True)
|
execute_batch_list(options.batchfile, dry_run=True)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user