From 00b19f9950eac86ee087aed18a21d91635d1e527 Mon Sep 17 00:00:00 2001 From: Tahsin Date: Sat, 22 Jun 2024 15:47:07 -0400 Subject: [PATCH] remove config and format --- hardbrake | 27 ++++++++---------- src/config.py | 3 -- src/file.py | 75 +++++++++++++++++++++++++++++--------------------- src/manager.py | 44 ++++++++++++++--------------- 4 files changed, 75 insertions(+), 74 deletions(-) delete mode 100644 src/config.py diff --git a/hardbrake b/hardbrake index b7dfa54..fb8e4a9 100755 --- a/hardbrake +++ b/hardbrake @@ -1,28 +1,25 @@ #!/usr/bin/env python import click -from src import app, VERSION, config +from src import app, VERSION @click.command() @click.option('--test', is_flag=True, help='Run testing function', default=False, ) @click.option('--version', '-v', is_flag=True, help='Check version', default=False, ) @click.option('--update', is_flag=True, help='Check for update', default=False, ) -@click.option('--show-command', is_flag=True, help='Show Command', default=False, ) @click.argument('input', type=click.File('r'), default=None, required=False) -def exec(test: str, version: str, update: str, show_command: bool, input: click.File): - config.app["show-command"] = show_command - - if test: - app.test() - elif version: - print(f'v: {VERSION}') - elif update: - app.update() - else: - name = input.name if input else None - files = [name] if name else [] - app.main(files) +def exec(test: str, version: str, update: str, input: click.File): + if test: + app.test() + elif version: + print(f'v: {VERSION}') + elif update: + app.update() + else: + name = input.name if input else None + files = [name] if name else [] + app.main(files) exec() diff --git a/src/config.py b/src/config.py deleted file mode 100644 index 4c7f4e9..0000000 --- a/src/config.py +++ /dev/null @@ -1,3 +0,0 @@ -app: dict[str, bool] = { - 'show-command': False, -} diff --git a/src/file.py b/src/file.py index cba25dd..b74bc31 100644 --- a/src/file.py +++ b/src/file.py @@ -2,52 +2,63 @@ import os import tempfile import src.prompts as prompts +import src.loader as loader +import time output_path = os.path.join(tempfile.gettempdir(), "OUTFILE") cmd = f"ranger --choosefiles={output_path}" def select_files() -> list[str]: - completed_process = subprocess.run(cmd, shell=True) - if completed_process.returncode != 0: - raise Exception("Something went wrong while selecting files") + completed_process = subprocess.run(cmd, shell=True) + if completed_process.returncode != 0: + raise Exception("Something went wrong while selecting files") - allowed_extensions = ["mp4", "mkv", "avi", "mov", "m4v", "flv", "wmv"] + allowed_extensions = ["mp4", "mkv", "avi", "mov", "m4v", "flv", "wmv"] - if not os.path.exists(output_path): - return [] + if not os.path.exists(output_path): + return [] - with open(output_path, "r") as f: - data = f.read() - filtered_data = map(lambda x: x.strip(), data.splitlines()) - filtered_data = list(filter(lambda x: len(x) > 0, filtered_data)) - filtered_data = list(filter(lambda x: x.split( - ".")[-1].lower() in allowed_extensions, filtered_data)) + with open(output_path, "r") as f: + data = f.read() + filtered_data = map(lambda x: x.strip(), data.splitlines()) + filtered_data = list(filter(lambda x: len(x) > 0, filtered_data)) + filtered_data = list(filter(lambda x: x.split( + ".")[-1].lower() in allowed_extensions, filtered_data)) - os.remove(output_path) + os.remove(output_path) - return filtered_data + return filtered_data class _Manager: - files: list[str] = [] - original_files: list[str] = [] - - def add_file(self, file_path: str) -> None: - """Add a file to the list that has been interacted with""" - self.files.append(file_path) - - def add_original_file(self, file_path: str) -> None: - """Add original files to the list that has been interacted with""" - self.original_files.append(file_path) - - def delete_file(self): - # """Delete files from the list that has been interacted with""" - delete_original_files = prompts.ask_boolean("Do you want to delete the original files?") - if delete_original_files: - for file_to_delete in self.original_files: - os.remove(file_to_delete) - print(f"Deleted {file_to_delete}") + files: list[str] = [] + original_files: list[str] = [] + + def add_file(self, file_path: str) -> None: + """Add a file to the list that has been interacted with""" + self.files.append(file_path) + + def add_original_file(self, file_path: str) -> None: + """Add original files to the list that has been interacted with""" + self.original_files.append(file_path) + + def delete_file(self): + # """Delete files from the list that has been interacted with""" + delete_original_files = prompts.ask_boolean("Do you want to delete the original files?") + if delete_original_files: + loader.loader.start() + loader.loader.add_task("del_files", len(self.original_files)) + + for i, j in enumerate(self.original_files): + file_path = j + number = i + 1 + + os.remove(file_path) + loader.loader.update("del_files", number) + time.sleep(0.01) + + loader.loader.stop() manager = _Manager() diff --git a/src/manager.py b/src/manager.py index ddac7b8..f6b3340 100644 --- a/src/manager.py +++ b/src/manager.py @@ -1,48 +1,44 @@ import src.event as event import src.loader as loader import src.utils as utils -import src.config as config from rich import print from rich.panel import Panel from rich.syntax import Syntax +import time def manage(cmds: list[str]): - loader.loader.start() + loader.loader.start() - def handle_progress(cmd: str, percent: int): - loader.loader.update(utils.hash(cmd), percent) + all_task_name = f"all ({len(cmds)})" - def get_process(cmd: str): - return event.ProcessWithEvent(cmd, progress_cb=handle_progress) + loader.loader.add_task(all_task_name, len(cmds)) + loader.loader.add_task("current", 100) - pwe_list = list(map(get_process, cmds)) + def handle_progress(cmd: str, percent: int): + loader.loader.update("current", percent) - for pwe in pwe_list: - loader.loader.add_task(utils.hash(pwe.cmd), 100) + for i in range(len(cmds)): + loader.loader.update("current", 0) + cmd = cmds[i] + time.sleep(0.01) + p = event.ProcessWithEvent(cmd, progress_cb=handle_progress) + p.start() - for pwe in pwe_list: - if config.app['show-command']: - syntax = Syntax(pwe.cmd, "bash", theme="monokai", word_wrap=True) - panel = Panel(syntax, title="Command", ) - print(panel) + loader.loader.update(all_task_name, i + 1) - pwe.start() - - loader.loader.stop() + loader.loader.stop() def main(): - cmd1 = "source /Users/tahsin/.commonrc && hb_test 10" - cmd2 = "source /Users/tahsin/.commonrc && hb_test 12" - cmd3 = "source /Users/tahsin/.commonrc && hb_test 5" - cmd4 = "ls" + cmds = [] - cmds = [cmd1, cmd2, cmd3, cmd4] + for i in range(1000): + cmds.append("ls") - manage(cmds) + manage(cmds) if __name__ == "__main__": - main() + main()