Apply Black

This commit is contained in:
Théophile Bastian 2021-03-21 23:18:36 +01:00
parent 861874d5db
commit b198ad5d28
2 changed files with 73 additions and 69 deletions

View File

@ -14,14 +14,14 @@ import sys
VORBIS_ARG_NAME = { VORBIS_ARG_NAME = {
'title': 'TITLE', "title": "TITLE",
'track': 'TRACKNUMBER', "track": "TRACKNUMBER",
'artist': 'ARTIST', "artist": "ARTIST",
'album': 'ALBUM', "album": "ALBUM",
'albumnumber': 'DISCNUMBER', "albumnumber": "DISCNUMBER",
'genre': 'GENRE', "genre": "GENRE",
'year': 'DATE', "year": "DATE",
'comment': 'COMMENT', "comment": "COMMENT",
} }
@ -38,26 +38,22 @@ def argparser():
""" Parses the arguments from sys.argv """ """ Parses the arguments from sys.argv """
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Edit flac files' metadata", description="Edit flac files' metadata",
epilog=('When no option modifying the tags is passed, the currently ' epilog=(
'set tags are shown.')) "When no option modifying the tags is passed, the currently "
parser.add_argument('-a', '--artist', "set tags are shown."
help="Specify artist name") ),
parser.add_argument('-c', '--comment', )
help="Specify an arbitrary comment") parser.add_argument("-a", "--artist", help="Specify artist name")
parser.add_argument('-g', '--genre', parser.add_argument("-c", "--comment", help="Specify an arbitrary comment")
help="Specify genre (in plain text)") parser.add_argument("-g", "--genre", help="Specify genre (in plain text)")
parser.add_argument('-l', '--album', parser.add_argument("-l", "--album", help="Specify album name")
help="Specify album name") parser.add_argument("-m", "--albumnumber", help="Specify album number")
parser.add_argument('-m', '--albumnumber', parser.add_argument("-n", "--track", help="Specify track number")
help="Specify album number") parser.add_argument("-t", "--title", help="Specify track title")
parser.add_argument('-n', '--track', parser.add_argument("-y", "--year", help="Specify album year")
help="Specify track number") parser.add_argument(
parser.add_argument('-t', '--title', "file", nargs="+", metavar="FILE", help="The file(s) to work on"
help="Specify track title") )
parser.add_argument('-y', '--year',
help="Specify album year")
parser.add_argument('file', nargs='+', metavar='FILE',
help="The file(s) to work on")
return parser.parse_args() return parser.parse_args()
@ -65,9 +61,11 @@ def is_flac_file(path):
""" Checks whether `path` refers to an existing, writeable flac file """ """ Checks whether `path` refers to an existing, writeable flac file """
if not os.path.isfile(path) or not os.access(path, os.W_OK): if not os.path.isfile(path) or not os.access(path, os.W_OK):
return False return False
flac_run = subprocess.run(['metaflac', '--list', path], flac_run = subprocess.run(
stdout=subprocess.DEVNULL, ["metaflac", "--list", path],
stderr=subprocess.DEVNULL) stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if flac_run.returncode != 0: if flac_run.returncode != 0:
return False # Metaflac failed to list the files' metadata return False # Metaflac failed to list the files' metadata
@ -83,7 +81,7 @@ def make_metaflac_args(in_args):
continue continue
arg_name = VORBIS_ARG_NAME[arg] arg_name = VORBIS_ARG_NAME[arg]
out_args.append('--remove-tag={}'.format(arg_name)) out_args.append("--remove-tag={}".format(arg_name))
out_args.append("--set-tag={}={}".format(arg_name, arg_val)) out_args.append("--set-tag={}={}".format(arg_name, arg_val))
return out_args return out_args
@ -92,15 +90,19 @@ def make_metaflac_args(in_args):
def edit_flac(args): def edit_flac(args):
""" Perfoms the requested edition operations """ """ Perfoms the requested edition operations """
metaflac_args = make_metaflac_args(args) metaflac_args = make_metaflac_args(args)
metaflac_args += args['file'] metaflac_args += args["file"]
metaflac_args.insert(0, 'metaflac') metaflac_args.insert(0, "metaflac")
metaflac_run = subprocess.run(metaflac_args) metaflac_run = subprocess.run(metaflac_args)
if metaflac_run.returncode != 0: if metaflac_run.returncode != 0:
print(('\n\nMetaflac exited with return code {}. There was an error ' print(
'during the execution, you should look at the output to ' (
'investigate it.').format(metaflac_run.returncode), "\n\nMetaflac exited with return code {}. There was an error "
file=sys.stderr) "during the execution, you should look at the output to "
"investigate it."
).format(metaflac_run.returncode),
file=sys.stderr,
)
sys.exit(metaflac_run.returncode) sys.exit(metaflac_run.returncode)
@ -114,18 +116,18 @@ def reverse_tag(vorbis_tag):
def get_tags(path): def get_tags(path):
""" Retrieves the relevant tags for a single file """ """ Retrieves the relevant tags for a single file """
metaflac_args = ['metaflac'] metaflac_args = ["metaflac"]
for tag in VORBIS_ARG_NAME: for tag in VORBIS_ARG_NAME:
metaflac_args += ['--show-tag', VORBIS_ARG_NAME[tag]] metaflac_args += ["--show-tag", VORBIS_ARG_NAME[tag]]
metaflac_args.append(path) metaflac_args.append(path)
metaflac_run = subprocess.run(metaflac_args, stdout=subprocess.PIPE) metaflac_run = subprocess.run(metaflac_args, stdout=subprocess.PIPE)
meta_out = metaflac_run.stdout.decode('utf-8') meta_out = metaflac_run.stdout.decode("utf-8")
output = {} output = {}
for line in meta_out.split('\n'): for line in meta_out.split("\n"):
split = line.split('=') split = line.split("=")
tag, value = split[0], '='.join(split[1:]) tag, value = split[0], "=".join(split[1:])
if not tag: if not tag:
continue continue
tag = reverse_tag(tag) tag = reverse_tag(tag)
@ -140,7 +142,7 @@ def show_tags(path):
max_len = max([len(tag) for tag in tags]) max_len = max([len(tag) for tag in tags])
for tag in tags: for tag in tags:
print(' {}: {}{}'.format(tag, ' '*(max_len - len(tag)), tags[tag])) print(" {}: {}{}".format(tag, " " * (max_len - len(tag)), tags[tag]))
print("") print("")
@ -148,15 +150,17 @@ def main():
args = vars(argparser()) args = vars(argparser())
has_errors = False has_errors = False
for cur_file in args['file']: for cur_file in args["file"]:
if not is_flac_file(cur_file): if not is_flac_file(cur_file):
print(("Error: file {} does not exist, or is not writeable by " print(
"metaflac").format(cur_file), (
file=sys.stderr) "Error: file {} does not exist, or is not writeable by " "metaflac"
).format(cur_file),
file=sys.stderr,
)
has_errors = True has_errors = True
if has_errors: if has_errors:
print("One or more file cannot be manipulated. Aborting.", print("One or more file cannot be manipulated. Aborting.", file=sys.stderr)
file=sys.stderr)
sys.exit(1) sys.exit(1)
edit_mode = False edit_mode = False
@ -168,9 +172,9 @@ def main():
if edit_mode: if edit_mode:
edit_flac(args) edit_flac(args)
else: else:
for path in args['file']: for path in args["file"]:
show_tags(path) show_tags(path)
if __name__ == '__main__': if __name__ == "__main__":
main() main()

View File

@ -4,23 +4,23 @@ from setuptools import setup
setup( setup(
name='flacinfo', name="flacinfo",
version='1.0.0b1', version="1.0.0b1",
description='A script analoguous to `mp3info`, but for flac files', description="A script analoguous to `mp3info`, but for flac files",
url='https://github.com/tobast/flacinfo', url="https://git.tobast.fr/tobast/flacinfo",
author='Théophile `tobast` Bastian', author="Théophile `tobast` Bastian",
classifiers=[ classifiers=[
'Development Status :: 4 - Beta', "Development Status :: 4 - Beta",
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', "License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
'Environment :: Console', "Environment :: Console",
'Programming Language :: Python :: 3', "Programming Language :: Python :: 3",
], ],
python_requires='>=3', python_requires=">=3",
keywords='flac metadata', keywords="flac metadata",
py_modules=['flacinfo'], py_modules=["flacinfo"],
entry_points={ entry_points={
'console_scripts': [ "console_scripts": [
'flacinfo = flacinfo:main', "flacinfo = flacinfo:main",
] ]
} },
) )