From 010b7d4dd1abeda240446bc6e994ef0a88de2f47 Mon Sep 17 00:00:00 2001 From: Stephan Loor Date: Thu, 18 Jul 2024 21:27:53 +0200 Subject: [PATCH 01/12] added apply-to-all flag to fix, removed fix_all and updated __init__ --- src/rejx/__init__.py | 2 -- src/rejx/cli.py | 34 +++++++++++++++++----------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/rejx/__init__.py b/src/rejx/__init__.py index 8d95cf5..a4415d9 100644 --- a/src/rejx/__init__.py +++ b/src/rejx/__init__.py @@ -6,7 +6,6 @@ diff, find_rej_files, fix, - fix_all, ls, parse_rej_file, process_rej_file, @@ -19,7 +18,6 @@ "diff", "find_rej_files", "fix", - "fix_all", "ls", "parse_rej_file", "process_rej_file", diff --git a/src/rejx/cli.py b/src/rejx/cli.py index bcba0ca..c9d87fe 100644 --- a/src/rejx/cli.py +++ b/src/rejx/cli.py @@ -11,6 +11,9 @@ from typing import Optional import typer +from typing import List +from typing_extensions import Annotated + from rich.console import Console from rich.logging import RichHandler from rich.prompt import Confirm @@ -187,7 +190,10 @@ def process_rej_file(rej_file_path: str) -> bool: @app.command() -def fix(rej_file_path: str) -> None: +def fix( + rej_files: Annotated[Optional[List[str]], typer.Argument()] = None, + apply_to_all: Optional[bool] = typer.Option(False,"--apply-to-all", help="Fix all files.") + ) -> None: """Applies changes from a specified .rej file to its corresponding original file. Args: @@ -201,22 +207,16 @@ def fix(rej_file_path: str) -> None: rejx fix path/to/file.rej ``` """ - process_rej_file(rej_file_path) - - -@app.command() -def fix_all() -> None: - """Searches for and applies changes from all .rej files in the current directory and subdirectories. - - Example: - ------- - To fix all .rej files found in the current and subdirectories, run: - ```bash - rejx fix_all - ``` - """ - for rej_file in find_rej_files(): - process_rej_file(rej_file) + if apply_to_all: + for rej_file in find_rej_files(hidden=True): + process_rej_file(rej_file) + + elif rej_files is None: + logging.error(f"No file name specified") + + else: + for rej_file in rej_files: + process_rej_file(rej_file) @app.command() From e7a4e30481381eb394a3f43d96b0895fadae4a19 Mon Sep 17 00:00:00 2001 From: Stephan Loor Date: Thu, 18 Jul 2024 21:45:45 +0200 Subject: [PATCH 02/12] updated description fix --- src/rejx/cli.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/rejx/cli.py b/src/rejx/cli.py index c9d87fe..0d822cd 100644 --- a/src/rejx/cli.py +++ b/src/rejx/cli.py @@ -188,17 +188,17 @@ def process_rej_file(rej_file_path: str) -> bool: return success - @app.command() def fix( - rej_files: Annotated[Optional[List[str]], typer.Argument()] = None, - apply_to_all: Optional[bool] = typer.Option(False,"--apply-to-all", help="Fix all files.") + rej_files: Annotated[Optional[List[str]], typer.Argument(show_default=False)] = None, + apply_to_all_files: Optional[bool] = typer.Option(False,"--all", help="Apply changes from all .rej files.", show_default=False) ) -> None: """Applies changes from a specified .rej file to its corresponding original file. Args: ---- - rej_file_path (str): The path to the .rej file to be processed. + rej_file (str): The path to the .rej file to be processed. + apply_to_all_files (Optional[bool]): Determines whether all files should be fixed. Default: False. Example: ------- @@ -206,8 +206,13 @@ def fix( ```bash rejx fix path/to/file.rej ``` + + To fix all files, run: + ```bash + rejx fix --all + ``` """ - if apply_to_all: + if apply_to_all_files: for rej_file in find_rej_files(hidden=True): process_rej_file(rej_file) From 8081325a073259befd5c5971bf0caedb0e01549e Mon Sep 17 00:00:00 2001 From: Stephan Loor Date: Thu, 18 Jul 2024 21:53:43 +0200 Subject: [PATCH 03/12] changed implementation of rej_files variable such that these appear as argument in help --- src/rejx/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rejx/cli.py b/src/rejx/cli.py index 0d822cd..1ae35be 100644 --- a/src/rejx/cli.py +++ b/src/rejx/cli.py @@ -190,7 +190,7 @@ def process_rej_file(rej_file_path: str) -> bool: @app.command() def fix( - rej_files: Annotated[Optional[List[str]], typer.Argument(show_default=False)] = None, + rej_files: Annotated[Optional[List[str]], typer.Argument(default=None)], apply_to_all_files: Optional[bool] = typer.Option(False,"--all", help="Apply changes from all .rej files.", show_default=False) ) -> None: """Applies changes from a specified .rej file to its corresponding original file. From 49ecd6f5de74cf7676a67fa5424ac43050c57f2d Mon Sep 17 00:00:00 2001 From: Stephan Loor Date: Thu, 18 Jul 2024 22:11:40 +0200 Subject: [PATCH 04/12] fixed broken function call --- src/rejx/cli.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/rejx/cli.py b/src/rejx/cli.py index 1ae35be..4667d2f 100644 --- a/src/rejx/cli.py +++ b/src/rejx/cli.py @@ -8,10 +8,9 @@ import pathlib import re from difflib import unified_diff -from typing import Optional +from typing import Optional, List import typer -from typing import List from typing_extensions import Annotated from rich.console import Console @@ -190,7 +189,7 @@ def process_rej_file(rej_file_path: str) -> bool: @app.command() def fix( - rej_files: Annotated[Optional[List[str]], typer.Argument(default=None)], + rej_files: Annotated[Optional[List[str]], typer.Argument(None)], apply_to_all_files: Optional[bool] = typer.Option(False,"--all", help="Apply changes from all .rej files.", show_default=False) ) -> None: """Applies changes from a specified .rej file to its corresponding original file. @@ -213,7 +212,7 @@ def fix( ``` """ if apply_to_all_files: - for rej_file in find_rej_files(hidden=True): + for rej_file in find_rej_files(): process_rej_file(rej_file) elif rej_files is None: From 876d196450e6cb4028a5bee4b4bb3b4a7b6b5e5d Mon Sep 17 00:00:00 2001 From: Stephan Loor Date: Thu, 18 Jul 2024 22:29:54 +0200 Subject: [PATCH 05/12] implemented workaround that allows for optional arguments --- src/rejx/cli.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/rejx/cli.py b/src/rejx/cli.py index 4667d2f..abc57e1 100644 --- a/src/rejx/cli.py +++ b/src/rejx/cli.py @@ -11,7 +11,6 @@ from typing import Optional, List import typer -from typing_extensions import Annotated from rich.console import Console from rich.logging import RichHandler @@ -189,7 +188,7 @@ def process_rej_file(rej_file_path: str) -> bool: @app.command() def fix( - rej_files: Annotated[Optional[List[str]], typer.Argument(None)], + rej_files: List[str] = typer.Argument( default = None ), apply_to_all_files: Optional[bool] = typer.Option(False,"--all", help="Apply changes from all .rej files.", show_default=False) ) -> None: """Applies changes from a specified .rej file to its corresponding original file. From 5e69a3d939a3ac4f039831f591acb7de8f765aea Mon Sep 17 00:00:00 2001 From: Stephan Loor Date: Thu, 18 Jul 2024 22:31:09 +0200 Subject: [PATCH 06/12] updated test for testing fix_all --- tests/test_rejx.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_rejx.py b/tests/test_rejx.py index 8984422..7be171f 100644 --- a/tests/test_rejx.py +++ b/tests/test_rejx.py @@ -86,7 +86,7 @@ def test_fix(sample_rej_file: str): def test_fix_all(): - result = runner.invoke(app, ["fix-all"]) + result = runner.invoke(app, ["fix", "--all"]) assert result.exit_code == 0 From d0174a64e6f7a8d49a12e1a02b83937ba9533d6c Mon Sep 17 00:00:00 2001 From: Stephan Loor Date: Thu, 18 Jul 2024 22:38:33 +0200 Subject: [PATCH 07/12] updated diff to allow for filename --- src/rejx/cli.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/rejx/cli.py b/src/rejx/cli.py index abc57e1..eaa793f 100644 --- a/src/rejx/cli.py +++ b/src/rejx/cli.py @@ -223,7 +223,9 @@ def fix( @app.command() -def diff() -> None: +def diff( + rej_files: List[str] = typer.Argument( default = None ) + ) -> None: """Displays the diff of changes proposed by .rej files against their corresponding original files. Example: @@ -233,10 +235,13 @@ def diff() -> None: rejx diff ``` """ + if rej_files is None: + rej_files = find_rej_files() + console = Console() file_logs = [] - for rej_file in find_rej_files(): + for rej_file in rej_files: try: target_file_path = rej_file.replace(".rej", "") rej_lines = parse_rej_file(rej_file) From a1be68c8171007997017f7b10fd45fe65a65e0c0 Mon Sep 17 00:00:00 2001 From: Stephan Loor Date: Thu, 18 Jul 2024 22:49:38 +0200 Subject: [PATCH 08/12] adapted clean function to allow for all flag --- src/rejx/cli.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/rejx/cli.py b/src/rejx/cli.py index eaa793f..571eb4d 100644 --- a/src/rejx/cli.py +++ b/src/rejx/cli.py @@ -381,11 +381,13 @@ def ls( @app.command() def clean( + rej_files: List[str] = typer.Argument( default = None ), + apply_to_all_files: Optional[bool] = typer.Option(False,"--all", help="Apply changes from all .rej files.", show_default=False), preview: bool = typer.Option( False, help="Preview files before deleting", ), -) -> None: + ) -> None: """Deletes all .rej files in the current directory and subdirectories. Optional preview before deletion. @@ -405,7 +407,12 @@ def clean( rejx clean --preview ``` """ - rej_files = find_rej_files() + if apply_to_all_files: + rej_files = find_rej_files() + + elif rej_files is None: + logging.error("No filename specified.") + console = Console() if not rej_files: From 67fa7c61fa2e6596f38e543e39d8510e8bb0799e Mon Sep 17 00:00:00 2001 From: Stephan Loor Date: Thu, 18 Jul 2024 22:51:05 +0200 Subject: [PATCH 09/12] updated tests for clean function --- tests/test_rejx.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_rejx.py b/tests/test_rejx.py index 7be171f..bf6ba0a 100644 --- a/tests/test_rejx.py +++ b/tests/test_rejx.py @@ -106,12 +106,12 @@ def test_ls_tree(): def test_clean(): - result = runner.invoke(app, ["clean"]) + result = runner.invoke(app, ["clean","--all"]) assert result.exit_code == 0 def test_clean_with_preview(): - result = runner.invoke(app, ["clean", "--preview"], input="y\n") + result = runner.invoke(app, ["clean","all", "--preview"], input="y\n") assert result.exit_code == 0 From 67e0c878c1cd3cd4947adeb4137f95a83c77780f Mon Sep 17 00:00:00 2001 From: Stephan Loor Date: Thu, 18 Jul 2024 22:54:49 +0200 Subject: [PATCH 10/12] updated descriptions of different command --- src/rejx/cli.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/rejx/cli.py b/src/rejx/cli.py index 571eb4d..2ec1296 100644 --- a/src/rejx/cli.py +++ b/src/rejx/cli.py @@ -226,7 +226,8 @@ def fix( def diff( rej_files: List[str] = typer.Argument( default = None ) ) -> None: - """Displays the diff of changes proposed by .rej files against their corresponding original files. + """Displays the diff of changes proposed by .rej files against their corresponding original files. + Displays the diff for all .rej files If no file names are specified. Example: ------- @@ -388,23 +389,29 @@ def clean( help="Preview files before deleting", ), ) -> None: - """Deletes all .rej files in the current directory and subdirectories. + """Deletes one or all .rej files in the current directory and subdirectories. Optional preview before deletion. Args: ---- + rej_files (Optional, List[str]): a list of names of files to be deleted. + apply_to_all_files (Optional, bool): determines if all files should be removed. Defaults to "False". preview (bool): If True, previews the files before deleting. Defaults to False. Example: ------- + - To delete a file file.txt.rej without preview, run: + ```bash + rejx clean file.txt.rej + ``` - To delete all .rej files without preview, run: ```bash - rejx clean + rejx clean --all ``` - To preview files before deletion, run: ```bash - rejx clean --preview + rejx clean --all --preview ``` """ if apply_to_all_files: From 77d644658a164b0d7897401ce58bb5260e33f7b0 Mon Sep 17 00:00:00 2001 From: Stephan Loor Date: Thu, 18 Jul 2024 22:59:28 +0200 Subject: [PATCH 11/12] updated README.md --- README.md | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e090c18..658e4d8 100644 --- a/README.md +++ b/README.md @@ -28,24 +28,22 @@ Your Python Typer application, rejx, provides a command line interface (CLI) for ### `fix` -Purpose: Applies the changes from a specified .rej file to its corresponding original file. +Purpose: Applies the changes from one or more specified .rej file to their corresponding original file. Usage: -`rejx fix path/to/file.rej` +`rejx fix path/to/file1.rej path/to/file2.rej ...` -### `fix_all` - -Purpose: Searches for all .rej files in the current directory and subdirectories, then applies their changes to the corresponding original files. -Usage: - -`rejx fix_all` +Passing the optional flag `--all` applies the changes from all .rej files to their corresponding original files. Usage: +`rejx fix --all` ### `diff` -Purpose: Displays the differences between the current files and the changes proposed in the .rej files. +Purpose: Displays the differences between the current file(s) and the changes proposed in the corresponding .rej file(s). Usage: -`rejx diff` +`rejx diff ...` + +If no file name is passed, this displays the difference for all .rej files. Note: This command uses a pager for output. Use arrow keys or Vim bindings to navigate, and q to quit. @@ -67,13 +65,20 @@ For list view (default): ### `clean` -Purpose: Deletes all .rej files in the current directory and subdirectories. It has an optional preview feature. +Purpose: Deletes specified .rej files. It has an optional preview feature. Usage: -`rejx clean` +`rejx clean path/to/file1.rej path/to/file2.rej ...` With preview: -`rejx clean --preview` +`rejx clean path/to/file1.rej path/to/file2.rej ... --preview` + +By passing the optional `--all` flag, this command deletes all the .rej files in the current directory and subdirectories. +Usage: +`rejx clean --all` +This can be combined with the `--preview` option. +Usage: +`rejx clean --all --preview` ______________________________________________________________________ From e3142cf70747f4dc4eca82eb6ed178cfff563a50 Mon Sep 17 00:00:00 2001 From: Stephan Loor Date: Thu, 18 Jul 2024 23:24:14 +0200 Subject: [PATCH 12/12] applied linting --- README.md | 9 +++++---- src/rejx/cli.py | 45 +++++++++++++++++++++++---------------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 658e4d8..5c80cfd 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Usage: `rejx fix path/to/file1.rej path/to/file2.rej ...` -Passing the optional flag `--all` applies the changes from all .rej files to their corresponding original files. Usage: +Passing the optional flag `--all` applies the changes from all .rej files to their corresponding original files. Usage: `rejx fix --all` ### `diff` @@ -43,7 +43,7 @@ Usage: `rejx diff ...` -If no file name is passed, this displays the difference for all .rej files. +If no file name is passed, this displays the difference for all .rej files. Note: This command uses a pager for output. Use arrow keys or Vim bindings to navigate, and q to quit. @@ -75,10 +75,11 @@ With preview: `rejx clean path/to/file1.rej path/to/file2.rej ... --preview` By passing the optional `--all` flag, this command deletes all the .rej files in the current directory and subdirectories. -Usage: +Usage: `rejx clean --all` -This can be combined with the `--preview` option. +This can be combined with the `--preview` option. Usage: `rejx clean --all --preview` + ______________________________________________________________________ diff --git a/src/rejx/cli.py b/src/rejx/cli.py index 2ec1296..59fb077 100644 --- a/src/rejx/cli.py +++ b/src/rejx/cli.py @@ -8,10 +8,9 @@ import pathlib import re from difflib import unified_diff -from typing import Optional, List +from typing import Optional import typer - from rich.console import Console from rich.logging import RichHandler from rich.prompt import Confirm @@ -186,17 +185,18 @@ def process_rej_file(rej_file_path: str) -> bool: return success + @app.command() def fix( - rej_files: List[str] = typer.Argument( default = None ), - apply_to_all_files: Optional[bool] = typer.Option(False,"--all", help="Apply changes from all .rej files.", show_default=False) - ) -> None: + rej_files: list[str] = typer.Argument(default=None), # noqa: B008 + apply_to_all_files: Optional[bool] = typer.Option(False, "--all", help="Apply changes from all .rej files.", show_default=False), +) -> None: """Applies changes from a specified .rej file to its corresponding original file. Args: ---- rej_file (str): The path to the .rej file to be processed. - apply_to_all_files (Optional[bool]): Determines whether all files should be fixed. Default: False. + apply_to_all_files (Optional[bool]): Determines whether all files should be fixed. Default: False. Example: ------- @@ -205,18 +205,18 @@ def fix( rejx fix path/to/file.rej ``` - To fix all files, run: + To fix all files, run: ```bash - rejx fix --all + rejx fix --all ``` """ if apply_to_all_files: for rej_file in find_rej_files(): process_rej_file(rej_file) - + elif rej_files is None: - logging.error(f"No file name specified") - + logging.error("No file name specified") + else: for rej_file in rej_files: process_rej_file(rej_file) @@ -224,9 +224,10 @@ def fix( @app.command() def diff( - rej_files: List[str] = typer.Argument( default = None ) - ) -> None: - """Displays the diff of changes proposed by .rej files against their corresponding original files. + rej_files: list[str] = typer.Argument(default=None), # noqa: B008 +) -> None: + """Displays the diff of changes proposed by .rej files against their corresponding original files. + Displays the diff for all .rej files If no file names are specified. Example: @@ -382,21 +383,21 @@ def ls( @app.command() def clean( - rej_files: List[str] = typer.Argument( default = None ), - apply_to_all_files: Optional[bool] = typer.Option(False,"--all", help="Apply changes from all .rej files.", show_default=False), + rej_files: list[str] = typer.Argument(default=None), # noqa: B008 + apply_to_all_files: Optional[bool] = typer.Option(False, "--all", help="Apply changes from all .rej files.", show_default=False), preview: bool = typer.Option( False, help="Preview files before deleting", ), - ) -> None: +) -> None: """Deletes one or all .rej files in the current directory and subdirectories. Optional preview before deletion. Args: ---- - rej_files (Optional, List[str]): a list of names of files to be deleted. - apply_to_all_files (Optional, bool): determines if all files should be removed. Defaults to "False". + rej_files (Optional, List[str]): a list of names of files to be deleted. + apply_to_all_files (Optional, bool): determines if all files should be removed. Defaults to "False". preview (bool): If True, previews the files before deleting. Defaults to False. Example: @@ -414,12 +415,12 @@ def clean( rejx clean --all --preview ``` """ - if apply_to_all_files: + if apply_to_all_files: rej_files = find_rej_files() - + elif rej_files is None: logging.error("No filename specified.") - + console = Console() if not rej_files: