From 5ddb86ff90bc40b9b939e9ad3da36fbc86782639 Mon Sep 17 00:00:00 2001 From: zay Date: Wed, 9 Oct 2024 22:56:15 +0200 Subject: [PATCH] make placeholders reusable in custom command --- havc/command.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/havc/command.py b/havc/command.py index c3ab99d..9f5819c 100644 --- a/havc/command.py +++ b/havc/command.py @@ -55,13 +55,15 @@ def __run_static_command(self, file_info): def __run_custom_command(self, file_info): list_of_args = split_string(self.custom_command, ' ', '\'') - list_of_args[list_of_args.index(self.original_file_placeholder)] = file_info.absolute_path + file_info.extension - list_of_args[list_of_args.index(self.converted_file_placeholder)] = file_info.absolute_path + file_info.target_extension - + list_of_args = self.replace_file_names(list_of_args, self.original_file_placeholder, file_info.absolute_path, file_info.extension) + list_of_args = self.replace_file_names(list_of_args, self.converted_file_placeholder, file_info.absolute_path, file_info.target_extension) list_of_args.insert(0, self.handbrake_executable) return self.__run(list_of_args) + def get_all_occurences_in_list(self, lst, item): + return [index for index, value in enumerate(lst) if item in value] + @staticmethod def __success_of(process): result = 'Encode done!' in str(process) @@ -72,3 +74,12 @@ def __run(args): print('Current Command: ' + ' '.join([str(elem) for elem in args]) + '\n') return subprocess.run(args, stderr=subprocess.PIPE) + def replace_file_names(self, list, placeholder, path, ext): + for index in self.get_all_occurences_in_list(list, placeholder): + to_replace = path + if list[index] == placeholder: + to_replace += ext + + list[index] = list[index].replace(placeholder, to_replace) + return list +