snake_case_ify all the things!

This commit is contained in:
Théophile Bastian 2018-01-18 23:17:54 +01:00
parent 0e9afd7cf1
commit 2b492cdf37
1 changed files with 48 additions and 48 deletions

View File

@ -61,115 +61,115 @@ def argparser():
return parser.parse_args() return parser.parse_args()
def isFlacFile(path): 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
flacRun = subprocess.run(['metaflac', '--list', path], flac_run = subprocess.run(['metaflac', '--list', path],
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL) stderr=subprocess.DEVNULL)
if flacRun.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
return True return True
def makeMetaflacArgs(inArgs): def make_metaflac_args(in_args):
outArgs = [] out_args = []
for arg in inArgs: for arg in in_args:
argVal = inArgs[arg] arg_val = in_args[arg]
if arg not in VORBIS_ARG_NAME or argVal is None: if arg not in VORBIS_ARG_NAME or arg_val is None:
continue continue
argName = VORBIS_ARG_NAME[arg] arg_name = VORBIS_ARG_NAME[arg]
outArgs.append('--remove-tag={}'.format(argName)) out_args.append('--remove-tag={}'.format(arg_name))
outArgs.append("--set-tag={}={}".format(argName, argVal)) out_args.append("--set-tag={}={}".format(arg_name, arg_val))
return outArgs return out_args
def editFlac(args): def edit_flac(args):
""" Perfoms the requested edition operations """ """ Perfoms the requested edition operations """
metaflacArgs = makeMetaflacArgs(args) metaflac_args = make_metaflac_args(args)
metaflacArgs += args['file'] metaflac_args += args['file']
metaflacArgs.insert(0, 'metaflac') metaflac_args.insert(0, 'metaflac')
metaflacRun = subprocess.run(metaflacArgs) metaflac_run = subprocess.run(metaflac_args)
if metaflacRun.returncode != 0: if metaflac_run.returncode != 0:
print(('\n\nMetaflac exited with return code {}. There was an error ' print(('\n\nMetaflac exited with return code {}. There was an error '
'during the execution, you should look at the output to ' 'during the execution, you should look at the output to '
'investigate it.').format(metaflacRun.returncode), 'investigate it.').format(metaflac_run.returncode),
file=sys.stderr) file=sys.stderr)
sys.exit(metaflacRun.returncode) sys.exit(metaflac_run.returncode)
def reverseTag(vorbisTag): def reverse_tag(vorbis_tag):
""" Reverses a Vorbis tag to an argument name """ """ Reverses a Vorbis tag to an argument name """
for tag in VORBIS_ARG_NAME: for tag in VORBIS_ARG_NAME:
if VORBIS_ARG_NAME[tag] == vorbisTag: if VORBIS_ARG_NAME[tag] == vorbis_tag:
return tag return tag
raise NoSuchTag(vorbisTag) raise NoSuchTag(vorbis_tag)
def getTags(path): def get_tags(path):
""" Retrieves the relevant tags for a single file """ """ Retrieves the relevant tags for a single file """
metaflacArgs = ['metaflac'] metaflac_args = ['metaflac']
for tag in VORBIS_ARG_NAME: for tag in VORBIS_ARG_NAME:
metaflacArgs += ['--show-tag', VORBIS_ARG_NAME[tag]] metaflac_args += ['--show-tag', VORBIS_ARG_NAME[tag]]
metaflacArgs.append(path) metaflac_args.append(path)
metaflacRun = subprocess.run(metaflacArgs, stdout=subprocess.PIPE) metaflac_run = subprocess.run(metaflac_args, stdout=subprocess.PIPE)
metaOut = metaflacRun.stdout.decode('utf-8') meta_out = metaflac_run.stdout.decode('utf-8')
output = {} output = {}
for line in metaOut.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 = reverseTag(tag) tag = reverse_tag(tag)
output[tag] = value output[tag] = value
return output return output
def showTags(path): def show_tags(path):
""" Shows the relevant tags already present in the given flac file """ """ Shows the relevant tags already present in the given flac file """
tags = getTags(path) tags = get_tags(path)
print("File: {}".format(path)) print("File: {}".format(path))
maxLen = 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, ' '*(maxLen - len(tag)), tags[tag])) print(' {}: {}{}'.format(tag, ' '*(max_len - len(tag)), tags[tag]))
print("") print("")
def main(): def main():
args = vars(argparser()) args = vars(argparser())
hasErrors = False has_errors = False
for cFile in args['file']: for cur_file in args['file']:
if not isFlacFile(cFile): if not is_flac_file(cur_file):
print(("Error: file {} does not exist, or is not writeable by " print(("Error: file {} does not exist, or is not writeable by "
"metaflac").format(cFile), "metaflac").format(cur_file),
file=sys.stderr) file=sys.stderr)
hasErrors = True has_errors = True
if hasErrors: 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)
editMode = False edit_mode = False
for tag in VORBIS_ARG_NAME: for tag in VORBIS_ARG_NAME:
if args[tag] is not None: if args[tag] is not None:
editMode = True edit_mode = True
break break
if editMode: if edit_mode:
editFlac(args) edit_flac(args)
else: else:
for path in args['file']: for path in args['file']:
showTags(path) show_tags(path)
if __name__ == '__main__': if __name__ == '__main__':