mirror of
https://github.com/iluvcapra/mfbatch.git
synced 2025-12-31 08:50:51 +00:00
More implementation
This commit is contained in:
@@ -3,15 +3,18 @@
|
|||||||
import sys
|
import sys
|
||||||
from subprocess import run
|
from subprocess import run
|
||||||
import os
|
import os
|
||||||
from glob import iglob
|
from glob import glob
|
||||||
from re import match
|
from re import match
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
import shlex
|
import shlex
|
||||||
|
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
|
from mfbatch.util import readline_with_escaped_newlines
|
||||||
|
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
import readline
|
# import readline
|
||||||
|
|
||||||
# MFBATCH COMMAND FILE
|
# MFBATCH COMMAND FILE
|
||||||
# Every line is processed, the first character decides the kind of command
|
# Every line is processed, the first character decides the kind of command
|
||||||
@@ -40,8 +43,8 @@ import readline
|
|||||||
|
|
||||||
|
|
||||||
METAFLAC_PATH = '/opt/homebrew/bin/metaflac'
|
METAFLAC_PATH = '/opt/homebrew/bin/metaflac'
|
||||||
COMMAND_PROCHAR = ':'
|
COMMAND_LEADER = ':'
|
||||||
COMMENT_PROCHAR = '#'
|
COMMENT_LEADER = '#'
|
||||||
|
|
||||||
|
|
||||||
class CommandEnv:
|
class CommandEnv:
|
||||||
@@ -54,7 +57,7 @@ class CommandEnv:
|
|||||||
|
|
||||||
|
|
||||||
def handle_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_LEADER)
|
||||||
args = shlex.split(commandline)
|
args = shlex.split(commandline)
|
||||||
command = args[0]
|
command = args[0]
|
||||||
|
|
||||||
@@ -87,31 +90,41 @@ def handle_command(metadatums: dict, line: str, dry_run: bool):
|
|||||||
|
|
||||||
def process_flac_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:
|
sys.stdout.write(f"\nFile: \033[1m{line}\033[0m\n")
|
||||||
sys.stderr.write(f"Would process file: {line}\n")
|
for key in metadatums.keys():
|
||||||
for key in metadatums.keys():
|
value = metadatums[key]
|
||||||
sys.stderr.write(f" > Set {key}: {metadatums[key]}\n")
|
value_lines = [value[i:i+60] for i in range(0,len(value),60)]
|
||||||
else:
|
for l in value_lines:
|
||||||
pass
|
if key:
|
||||||
|
sys.stdout.write(f" {key:.<32} : \033[4m{l}\033[0m\n")
|
||||||
|
key = None
|
||||||
|
else:
|
||||||
|
sys.stdout.write(f" {' ' * 32} \033[4m{l}\033[0m\n")
|
||||||
|
|
||||||
|
|
||||||
|
resp = input('Confirm? [Y/n]: ')
|
||||||
|
if resp in ['','Y','y']:
|
||||||
|
if not dry_run:
|
||||||
|
sys.stdout.write('!! Writing not implemented\n')
|
||||||
|
else:
|
||||||
|
sys.stdout.write('Dry-run, would write file here.\n')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def execute_batch_list(batch_list_path: str, dry_run: bool):
|
def execute_batch_list(batch_list_path: str, dry_run: bool):
|
||||||
with open(batch_list_path, mode='r') as f:
|
with open(batch_list_path, mode='r') as f:
|
||||||
metadatums = {}
|
metadatums = {}
|
||||||
while True:
|
for line in readline_with_escaped_newlines(f):
|
||||||
line = f.readline()
|
|
||||||
if line is None or line == "":
|
if line == '':
|
||||||
break
|
continue
|
||||||
|
|
||||||
if line.startswith(COMMENT_PROCHAR):
|
elif line.startswith(COMMENT_LEADER):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
elif line.startswith(COMMAND_PROCHAR):
|
elif line.startswith(COMMAND_LEADER):
|
||||||
handle_command(metadatums, line, dry_run)
|
handle_command(metadatums, line, dry_run)
|
||||||
|
|
||||||
elif line == "\n":
|
|
||||||
continue
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
process_flac_file(metadatums, line, dry_run)
|
process_flac_file(metadatums, line, dry_run)
|
||||||
|
|
||||||
@@ -121,9 +134,9 @@ def create_cwd_batch_list(command_file: str):
|
|||||||
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']
|
||||||
preflight = len(list(iglob('./**/*.flac', recursive=True)))
|
flac_files = glob('./**/*.flac', recursive=True)
|
||||||
for path in tqdm(iglob('./**/*.flac', recursive=True),
|
flac_files = sorted(flac_files)
|
||||||
total=preflight):
|
for path in tqdm(flac_files, unit='File', desc='Scanning FLAC files'):
|
||||||
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():
|
||||||
@@ -155,7 +168,7 @@ def create_cwd_batch_list(command_file: str):
|
|||||||
def main():
|
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', default=False,
|
||||||
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,
|
||||||
@@ -167,6 +180,8 @@ def main():
|
|||||||
op.add_option('-e', '--edit', action='store_true',
|
op.add_option('-e', '--edit', action='store_true',
|
||||||
help="Open batch file in the default editor",
|
help="Open batch file in the default editor",
|
||||||
default=False)
|
default=False)
|
||||||
|
op.add_option('-n', '--dry-run', action='store_true',
|
||||||
|
help="Dry-run -W.")
|
||||||
op.add_option('-f', '--batchfile', metavar='FILE',
|
op.add_option('-f', '--batchfile', metavar='FILE',
|
||||||
help="Use batch list FILE for reading and writing instead "
|
help="Use batch list FILE for reading and writing instead "
|
||||||
"of the default \"MFBATCH_LIST\"",
|
"of the default \"MFBATCH_LIST\"",
|
||||||
@@ -185,7 +200,7 @@ def main():
|
|||||||
run(editor_command)
|
run(editor_command)
|
||||||
|
|
||||||
if options.write:
|
if options.write:
|
||||||
execute_batch_list(options.batchfile, dry_run=True)
|
execute_batch_list(options.batchfile, dry_run=options.dry_run)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
20
mfbatch/util.py
Normal file
20
mfbatch/util.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# util.py
|
||||||
|
|
||||||
|
def readline_with_escaped_newlines(f):
|
||||||
|
line = ''
|
||||||
|
while True:
|
||||||
|
line += f.readline()
|
||||||
|
|
||||||
|
if len(line) == 0:
|
||||||
|
break
|
||||||
|
|
||||||
|
line = line.rstrip("\n")
|
||||||
|
|
||||||
|
if len(line) > 0 and line[-1] == '\\':
|
||||||
|
line = line[:-1]
|
||||||
|
continue
|
||||||
|
|
||||||
|
else:
|
||||||
|
yield line
|
||||||
|
line = ''
|
||||||
|
|
||||||
Reference in New Issue
Block a user