This commit is contained in:
Jamie Hardt
2024-07-01 22:34:36 -07:00
parent afffe213e3
commit 17bbfccb7e
4 changed files with 27 additions and 28 deletions

View File

@@ -29,7 +29,7 @@ def execute_batch_list(batch_list_path: str, dry_run: bool, interactive: bool):
parser.eval(line, line_no, interactive) parser.eval(line, line_no, interactive)
def create_batch_list(command_file: str, recursive= True): def create_batch_list(command_file: str, recursive=True):
""" """
Read all FLAC files in the cwd and create a batchfile that re-creates all Read all FLAC files in the cwd and create a batchfile that re-creates all
of their metadata. of their metadata.
@@ -69,31 +69,31 @@ def main():
op = ArgumentParser(usage="%prog (-c | -e | -W) [options]") op = ArgumentParser(usage="%prog (-c | -e | -W) [options]")
op.add_argument('-c', '--create', default=False, op.add_argument('-c', '--create', default=False,
action='store_true', action='store_true',
help='create a new list') help='create a new list')
op.add_argument('-e', '--edit', action='store_true', op.add_argument('-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_argument('-W', '--write', default=False, op.add_argument('-W', '--write', default=False,
action='store_true', action='store_true',
help="execute batch list, write to files") help="execute batch list, write to files")
op.add_argument('-p', '--path', metavar='DIR', op.add_argument('-p', '--path', metavar='DIR',
help='chdir to DIR before running', help='chdir to DIR before running',
default=None) default=None)
op.add_argument('-n', '--dry-run', action='store_true', op.add_argument('-n', '--dry-run', action='store_true',
help="dry-run -W.") help="dry-run -W.")
op.add_argument('-f', '--batchfile', metavar='FILE', op.add_argument('-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\"",
default='MFBATCH_LIST') default='MFBATCH_LIST')
op.add_argument('-y', '--yes', default=False, action='store_true', op.add_argument('-y', '--yes', default=False, action='store_true',
dest='yes', help="automatically confirm all prompts, " dest='yes', help="automatically confirm all prompts, "
"inhibits interactive editing in -W mode") "inhibits interactive editing in -W mode")
op.add_argument('--help-commands', action='store_true', default=False, op.add_argument('--help-commands', action='store_true', default=False,
dest='help_commands', dest='help_commands',
help='print a list of available commands for batch lists ' help='print a list of available commands for batch lists '
'and interactive writing.') 'and interactive writing.')
options = op.parse_args() options = op.parse_args()

View File

@@ -50,7 +50,7 @@ class CommandEnv:
artwork_desc: Optional[str] artwork_desc: Optional[str]
def __init__(self) -> None: def __init__(self) -> None:
self.metadatums = {} self.metadatums = {}
self.incr = {} self.incr = {}
self.patterns = {} self.patterns = {}
self.onces = {} self.onces = {}
@@ -163,13 +163,13 @@ they appear in the batchfile.
def _handle_command(self, line, lineno): def _handle_command(self, line, lineno):
args = shlex.split(line) args = shlex.split(line)
actions = [m_name for m_name in dir(self) actions = [m_name for m_name in dir(self)
if not (m_name.startswith('_') or m_name == 'eval') ] if not (m_name.startswith('_') or m_name == 'eval')]
if args[0] in actions: if args[0] in actions:
try: try:
self.__getattribute__(args[0])(args[1:]) self.__getattribute__(args[0])(args[1:])
except KeyError as exc: except KeyError as exc:
raise CommandArgumentError(command=args[0], raise CommandArgumentError(command=args[0],
line=lineno) from exc line=lineno) from exc
else: else:
raise UnrecognizedCommandError(command=args[0], line=lineno) raise UnrecognizedCommandError(command=args[0], line=lineno)
@@ -191,7 +191,6 @@ they appear in the batchfile.
self.env.revert_onces() self.env.revert_onces()
self.env.clear_file_keys() self.env.clear_file_keys()
while True: while True:
self.env.set_file_keys(line) self.env.set_file_keys(line)
@@ -221,7 +220,7 @@ they appear in the batchfile.
if interactive: if interactive:
val = input('Write? [Y/n/a/:] > ') val = input('Write? [Y/n/a/:] > ')
if val == '' or val.startswith('Y') or val.startswith('y'): if val == '' or val.startswith('Y') or val.startswith('y'):
write_metadata_impl() write_metadata_impl()
break break
elif val.startswith(self.COMMAND_LEADER): elif val.startswith(self.COMMAND_LEADER):
self._handle_command(val.lstrip(self.COMMAND_LEADER), self._handle_command(val.lstrip(self.COMMAND_LEADER),
@@ -230,7 +229,7 @@ they appear in the batchfile.
elif val == 'a': elif val == 'a':
print("Aborting write session...", file=sys.stdout) print("Aborting write session...", file=sys.stdout)
break break
write_metadata_impl() write_metadata_impl()
def set(self, args): def set(self, args):

View File

@@ -70,5 +70,5 @@ def write_metadata(path: str, data: FlacMetadata,
metadatum_f = metadatum_f + f"{key}={val}\n" metadatum_f = metadatum_f + f"{key}={val}\n"
run([metaflac_path, "--import-tags-from=-", path], run([metaflac_path, "--import-tags-from=-", path],
input=metadatum_f.encode('utf-8'), check=True) input=metadatum_f.encode('utf-8'), check=True)

View File

@@ -2,6 +2,7 @@
mfbatch util - utility functions mfbatch util - utility functions
""" """
def readline_with_escaped_newlines(f): def readline_with_escaped_newlines(f):
""" """
A readline line generator, except lines ending with a backslash will be A readline line generator, except lines ending with a backslash will be
@@ -22,6 +23,5 @@ def readline_with_escaped_newlines(f):
line = line[:-1] line = line[:-1]
continue continue
else: yield line, line_no
yield line, line_no line = ''
line = ''