From 8aaa18d6e56676edbe36487b46cd49e9da310fbd Mon Sep 17 00:00:00 2001 From: PatillaCode Date: Mon, 25 Oct 2021 13:11:23 +0200 Subject: [PATCH] apply python-black formatter --- convert/__main__.py | 1 + convert/cli.py | 11 ++-- convert/commands/audio.py | 16 ++--- convert/commands/base.py | 49 +++++++++------ convert/commands/video.py | 21 +++---- convert/converter_utils/utils.py | 105 +++++++++++++++---------------- tests/conftest.py | 7 +-- tests/test_utils.py | 30 +++------ 8 files changed, 111 insertions(+), 129 deletions(-) diff --git a/convert/__main__.py b/convert/__main__.py index 130bc63..4e28416 100644 --- a/convert/__main__.py +++ b/convert/__main__.py @@ -1,2 +1,3 @@ from .cli import main + main() diff --git a/convert/cli.py b/convert/cli.py index d50cf4e..1e85b02 100644 --- a/convert/cli.py +++ b/convert/cli.py @@ -26,9 +26,9 @@ """ +from inspect import getmembers, isclass + from docopt import docopt -from inspect import getmembers -from inspect import isclass from . import __version__ as VERSION @@ -36,6 +36,7 @@ def main(): """Main CLI entrypoint.""" import convert.commands + options = docopt(__doc__, version=VERSION) # Here we'll try to dynamically match the command the user is trying to run @@ -44,8 +45,8 @@ def main(): if hasattr(convert.commands, k) and v: module = getattr(convert.commands, k) convert.commands = getmembers(module, isclass) - command = [command[1] for command - in convert.commands - if command[0] != 'Base'][0] + command = [ + command[1] for command in convert.commands if command[0] != 'Base' + ][0] command = command(options) command.run() diff --git a/convert/commands/audio.py b/convert/commands/audio.py index b3b4a17..9194691 100755 --- a/convert/commands/audio.py +++ b/convert/commands/audio.py @@ -13,25 +13,17 @@ def __init__(self, options, *args, **kwargs): 1: { 'option_text': 'Convert to .mp3 (320k)', 'extension': 'mp3', - 'params': { - 'ar': 44100, - 'ac': 2, - 'ab': '320k ', - 'f': 'mp3' - } + 'params': {'ar': 44100, 'ac': 2, 'ab': '320k ', 'f': 'mp3'}, }, - 2: { - 'option_text': 'Convert to .wav', - 'extension': 'wav', - 'params': {} - } + 2: {'option_text': 'Convert to .wav', 'extension': 'wav', 'params': {}}, } def run(self): """Run the Audio command.""" chosen_option = let_user_pick(self.conversion_map) source_paths, output_paths, params = self.get_user_input( - self.conversion_map[chosen_option]) + self.conversion_map[chosen_option] + ) for (source_path, output_path) in list(zip(source_paths, output_paths)): run_ffmpeg(source_path, output_path, params, self.options) prmsg('completed') diff --git a/convert/commands/base.py b/convert/commands/base.py index c1bdf80..2ec659b 100644 --- a/convert/commands/base.py +++ b/convert/commands/base.py @@ -2,9 +2,8 @@ from termcolor import colored -from ..converter_utils import clear, confirmator, multi_source -from ..converter_utils import print_message as prmsg -from ..converter_utils import single_source, validate_path +from ..converter_utils import (clear, confirmator, multi_source, single_source, + validate_path) class Base(object): @@ -41,9 +40,16 @@ def get_user_input(self, conversion_data): source_path, source_name, source_folder = single_source() default_folder = '{}'.format(source_folder) - destination = input(colored( - "Enter path to destination folder " - "(Enter for same folder as source): ", 'green')) or default_folder + destination = ( + input( + colored( + "Enter path to destination folder " + "(Enter for same folder as source): ", + 'green', + ) + ) + or default_folder + ) destination = validate_path(destination, 'folder') source_paths = [] @@ -53,10 +59,13 @@ def get_user_input(self, conversion_data): if self.options['--multiple']: confirmator( self.options, - **{'ori_ext': source_extension, - 'ori_folder': source_folder, - 'out_ext': conversion_data['extension'], - 'out_folder': destination}) + **{ + 'ori_ext': source_extension, + 'ori_folder': source_folder, + 'out_ext': conversion_data['extension'], + 'out_folder': destination, + }, + ) folder = os.fsencode(source_folder) for file in os.listdir(folder): @@ -71,7 +80,8 @@ def get_user_input(self, conversion_data): if source_ext == '.{}'.format(source_extension): source_paths.append(source_path) output_path = '{}{}.{}'.format( - destination, source_name, conversion_data['extension']) + destination, source_name, conversion_data['extension'] + ) output_paths.append(output_path) # single file flow @@ -79,17 +89,20 @@ def get_user_input(self, conversion_data): # do not show confirmation message if the option is enabled confirmator( self.options, - **{'ori_path': source_path, - 'out_ext': conversion_data['extension'], - 'out_folder': destination}) + **{ + 'ori_path': source_path, + 'out_ext': conversion_data['extension'], + 'out_folder': destination, + }, + ) source_paths = [source_path] - output_paths = ['{}{}.{}'.format( - destination, source_name, conversion_data['extension'])] + output_paths = [ + '{}{}.{}'.format(destination, source_name, conversion_data['extension']) + ] return source_paths, output_paths, conversion_data['params'] def run(self): """All commands must implement this method.""" - raise NotImplementedError( - 'You must implement the run() method yourself!') + raise NotImplementedError('You must implement the run() method yourself!') diff --git a/convert/commands/video.py b/convert/commands/video.py index f3be3c8..3f76e44 100755 --- a/convert/commands/video.py +++ b/convert/commands/video.py @@ -17,8 +17,8 @@ def __init__(self, options, *args, **kwargs): 'vcodec': 'libx264', 'crf': 20, 'acodec': 'aac', - 'strict': 'experimental' - } + 'strict': 'experimental', + }, }, 2: { 'option_text': 'Convert to .mov', @@ -28,16 +28,12 @@ def __init__(self, options, *args, **kwargs): 'crf': 20, 'acodec': 'aac', 'f': 'mov', - } + }, }, 3: { 'option_text': 'Convert to .flv', 'extension': 'flv', - 'params': { - 'vcodec': 'flv1', - 'acodec': 'aac', - 'strict': 'experimental' - } + 'params': {'vcodec': 'flv1', 'acodec': 'aac', 'strict': 'experimental'}, }, 4: { 'option_text': 'Convert to .mkv', @@ -45,7 +41,7 @@ def __init__(self, options, *args, **kwargs): 'params': { 'vcodec': 'copy', 'acodec': 'copy', - } + }, }, 5: { 'option_text': 'Extract audio (output in .mp3)', @@ -55,15 +51,16 @@ def __init__(self, options, *args, **kwargs): 'ac': '2', 'ab': '320k', 'f': 'mp3', - } - } + }, + }, } def run(self): """Run the Video command.""" chosen_option = let_user_pick(self.conversion_map) source_paths, output_paths, params = self.get_user_input( - self.conversion_map[chosen_option]) + self.conversion_map[chosen_option] + ) for (source_path, output_path) in list(zip(source_paths, output_paths)): run_ffmpeg(source_path, output_path, params, self.options) prmsg('completed') diff --git a/convert/converter_utils/utils.py b/convert/converter_utils/utils.py index a28b37e..140a1da 100644 --- a/convert/converter_utils/utils.py +++ b/convert/converter_utils/utils.py @@ -8,26 +8,16 @@ def print_message(msg_type, **kwargs): """""" if msg_type == 'options': - print( - colored("\t{}) {}".format(kwargs['key'], kwargs['value']), - 'green') - ) + print(colored("\t{}) {}".format(kwargs['key'], kwargs['value']), 'green')) elif msg_type == 'choose': - print( - colored('\nPlease choose an option:', 'magenta') - ) + print(colored('\nPlease choose an option:', 'magenta')) elif msg_type == 'valid_option': - print( - colored('\n Please enter a valid option\n', 'red') - ) + print(colored('\n Please enter a valid option\n', 'red')) elif msg_type == 'exception': - print( - colored('\nAn exception ocurred:', 'red'), - '{}'.format(kwargs['exc']) - ) + print(colored('\nAn exception ocurred:', 'red'), '{}'.format(kwargs['exc'])) elif msg_type == 'confirm_multi': print( @@ -38,7 +28,7 @@ def print_message(msg_type, **kwargs): 'into', colored('.{}'.format(kwargs['out_ext']), 'yellow'), 'files to be saved in folder', - colored('{}'.format(kwargs['out_folder']), 'yellow') + colored('{}'.format(kwargs['out_folder']), 'yellow'), ) elif msg_type == 'confirm_single': @@ -48,22 +38,26 @@ def print_message(msg_type, **kwargs): 'into a', colored('.{}'.format(kwargs['out_ext']), 'yellow'), 'file to be saved in folder', - colored('{}'.format(kwargs['out_folder']), 'yellow') + colored('{}'.format(kwargs['out_folder']), 'yellow'), ) elif msg_type == 'warning': - print(colored( - ' \n' - ' WARNING \n', - 'red', - attrs=['bold', 'underline']) + print( + colored( + ' \n' + ' WARNING \n', + 'red', + attrs=['bold', 'underline'], + ) ) - print(colored( - ' Output file will be called the same as the original \n' - ' with the proper extension (.mp4, .mp3, ...) which \n' - ' may cause an overwrite - YOU HAVE BEEN WARNED \n', - 'red', - attrs=['reverse', 'bold']) + print( + colored( + ' Output file will be called the same as the original \n' + ' with the proper extension (.mp4, .mp3, ...) which \n' + ' may cause an overwrite - YOU HAVE BEEN WARNED \n', + 'red', + attrs=['reverse', 'bold'], + ) ) elif msg_type == 'wrong_path_option': @@ -71,23 +65,21 @@ def print_message(msg_type, **kwargs): colored('Wrong path type', 'red'), colored('{}'.format(kwargs['path_type']), 'yellow'), colored('only valid values are', 'red'), - colored('[file, folder]', 'yellow') + colored('[file, folder]', 'yellow'), ) elif msg_type == 'invalid_path': print( colored('\nGiven path is not valid, please confirm', 'red'), colored(kwargs['path'], 'yellow'), - colored('has no typos.\n', 'red') + colored('has no typos.\n', 'red'), ) elif msg_type == 'completed': print( colored('\n(っ◕‿◕)っ ', 'magenta'), - colored('Conversion completed', - 'green', - attrs=['bold']), - colored(' ⊂(´・◡・⊂ )∘˚\n', 'magenta') + colored('Conversion completed', 'green', attrs=['bold']), + colored(' ⊂(´・◡・⊂ )∘˚\n', 'magenta'), ) elif msg_type == 'converting': @@ -96,7 +88,7 @@ def print_message(msg_type, **kwargs): colored('{}'.format(kwargs['source_path']), 'yellow'), 'into output', colored('{}'.format(kwargs['output_path']), 'yellow'), - '... ' + '... ', ) @@ -110,12 +102,9 @@ def let_user_pick(conversion_map): print_message('choose') for key, value in conversion_map.items(): - print_message( - 'options', - **{'key': key, 'value': value['option_text']}) + print_message('options', **{'key': key, 'value': value['option_text']}) - i = input( - colored("Enter option number: ", 'magenta')) + i = input(colored("Enter option number: ", 'magenta')) try: try: @@ -157,8 +146,7 @@ def validate_path(path, path_type): def single_source(): - source_path = input( - colored("Enter path to source file: ", 'magenta')) + source_path = input(colored("Enter path to source file: ", 'magenta')) source_path = validate_path(source_path, 'file') source_name = os.path.splitext(os.path.split(source_path)[1])[0] source_folder = os.path.splitext(os.path.split(source_path)[0])[0] @@ -168,12 +156,18 @@ def single_source(): def multi_source(): source_folder = input( - colored("Enter path to source folder containing all files to " - "be converted: ", 'green')) + colored( + "Enter path to source folder containing all files to " "be converted: ", + 'green', + ) + ) source_folder = validate_path(source_folder, 'folder') source_extension = input( - colored("Enter extension of source files to be converted " - "(mp4, mp3, ...): ", 'green')) + colored( + "Enter extension of source files to be converted " "(mp4, mp3, ...): ", + 'green', + ) + ) return source_folder, source_extension @@ -210,7 +204,9 @@ def confirmator(options, **kwargs): 'ori_ext': kwargs['ori_ext'], 'ori_folder': kwargs['ori_folder'], 'out_ext': kwargs['out_ext'], - 'out_folder': kwargs['out_folder']}) + 'out_folder': kwargs['out_folder'], + }, + ) else: print_message( @@ -218,10 +214,11 @@ def confirmator(options, **kwargs): **{ 'ori_path': kwargs['ori_path'], 'out_ext': kwargs['out_ext'], - 'out_folder': kwargs['out_folder']}) + 'out_folder': kwargs['out_folder'], + }, + ) - confirmation = input( - colored('\nPlease confirm action above [y/n]: ', 'red')) + confirmation = input(colored('\nPlease confirm action above [y/n]: ', 'red')) if not user_confirmed(confirmation): sys.exit(2) @@ -247,20 +244,18 @@ def run_ffmpeg(source_path, output_path, ffmpeg_params, options): # present user with information of happening conversion if not options['--verbose']: print_message( - 'converting', - **{'source_path': source_path, - 'output_path': output_path}) + 'converting', **{'source_path': source_path, 'output_path': output_path} + ) # run the actual ffmpeg command ( - ffmpeg - .input(source_path) + ffmpeg.input(source_path) .output( output_path, **ffmpeg_params, ) .overwrite_output() - .run(quiet=not(options['--verbose'])) + .run(quiet=not (options['--verbose'])) ) except Exception as exc: diff --git a/tests/conftest.py b/tests/conftest.py index 2fcb25d..41883e4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,4 @@ -import sys import os +import sys - -sys.path.append( - os.path.join( - os.path.dirname(os.path.abspath(__file__)), '../convert/')) +sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../convert/')) diff --git a/tests/test_utils.py b/tests/test_utils.py index 3f5c249..5711dd6 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,25 +1,15 @@ +import builtins import os -import builtins import mock import pytest -# import module - -from converter_utils import confirmator -from converter_utils import let_user_pick -from converter_utils import multi_source -from converter_utils import single_source -from converter_utils import user_confirmed -from converter_utils import validate_path +from converter_utils import (let_user_pick, multi_source, single_source, + user_confirmed, validate_path) class TestUserInput(object): - def setup(self): - self.conversion_map = { - 1: {'option_text': 'bar'}, - 2: {'option_text': 'foo'} - } + self.conversion_map = {1: {'option_text': 'bar'}, 2: {'option_text': 'foo'}} self.file_path = os.path.abspath(__file__) self.folder_path = os.path.dirname(self.file_path) @@ -47,9 +37,7 @@ def test_single_source(self): def test_multi_source(self): """ """ - with mock.patch.object(builtins, - 'input', - side_effect=[self.folder_path, 'mp4']): + with mock.patch.object(builtins, 'input', side_effect=[self.folder_path, 'mp4']): source_folder, source_extension = multi_source() assert source_folder == self.folder_path + '/' assert source_extension == 'mp4' @@ -57,7 +45,7 @@ def test_multi_source(self): def test_user_confirmed(self): options = { 'True': ['y', 'yes', 'Y', ''], - 'False': ['n', 'no', 'N', 'random', 3, '#'] + 'False': ['n', 'no', 'N', 'random', 3, '#'], } for key, val in options.items(): for input_value in val: @@ -65,7 +53,6 @@ def test_user_confirmed(self): class TestPathValidation(object): - def setup(self): self.file_path = os.path.abspath(__file__) self.folder_path = os.path.dirname(self.file_path) @@ -73,15 +60,14 @@ def setup(self): def test_validate_path_success(self): """ """ assert validate_path(self.file_path, 'file') == self.file_path - assert validate_path( - self.folder_path, 'folder') == self.folder_path + '/' + assert validate_path(self.folder_path, 'folder') == self.folder_path + '/' def test_validate_path_fail(self): """ """ paths = [ {'path': '/random/path/', 'sys': 1, 'type': 'foo'}, {'path': '/random/path/to/file.ext', 'sys': 2, 'type': 'file'}, - {'path': '/random/path/to/folder', 'sys': 2, 'type': 'folder'} + {'path': '/random/path/to/folder', 'sys': 2, 'type': 'folder'}, ] for path in paths: