From cb6b8a5a18ca2537164ca81cb5bc27f068894a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20H=C3=B8gh=20Kyhse-Andersen?= Date: Tue, 14 Jan 2025 22:19:27 +0100 Subject: [PATCH] cleaned some stuff up and added simple use of internal data instead of using deprecaped package module in Python. --- copcon/cli.py | 14 ++++++++++---- copcon/core/data/.copconignore | 26 ++++++++++++++++++++++++++ copcon/core/data/__init__.py | 10 ++++++++++ copcon/core/file_filter.py | 27 +++++++++------------------ 4 files changed, 55 insertions(+), 22 deletions(-) create mode 100644 copcon/core/data/.copconignore create mode 100644 copcon/core/data/__init__.py diff --git a/copcon/cli.py b/copcon/cli.py index 3002d3c..a3faffa 100644 --- a/copcon/cli.py +++ b/copcon/cli.py @@ -15,7 +15,8 @@ from copcon.utils.logger import logger -app = typer.Typer() +app = typer.Typer(name="copcon", help="Test") + @app.command(no_args_is_help=True) def main( @@ -53,7 +54,7 @@ def main( file_filter = FileFilter( additional_dirs=ignore_dirs, additional_files=ignore_files, - user_ignore_path=copconignore + user_ignore_path=copconignore, ) # Generate directory tree @@ -84,7 +85,9 @@ def main( else: extension = "(no extension)" - extension_token_map[extension] = extension_token_map.get(extension, 0) + tokens_for_file + extension_token_map[extension] = ( + extension_token_map.get(extension, 0) + tokens_for_file + ) # Write or copy the textual report if output_file: @@ -99,7 +102,9 @@ def main( total_tokens=total_tokens, extension_token_map=extension_token_map, output_file=str(output_file) if output_file else None, - copconignore_path=str(used_copconignore_path) if used_copconignore_path else None, + copconignore_path=( + str(used_copconignore_path) if used_copconignore_path else None + ), ) typer.echo(success_msg) @@ -113,5 +118,6 @@ def main( logger.exception("An unexpected error occurred.") raise typer.Exit(code=1) + if __name__ == "__main__": app() diff --git a/copcon/core/data/.copconignore b/copcon/core/data/.copconignore new file mode 100644 index 0000000..34050b1 --- /dev/null +++ b/copcon/core/data/.copconignore @@ -0,0 +1,26 @@ + +# Default directories to ignore +__pycache__/ +.venv/ +node_modules/ +.git/ +.idea/ +.vscode/ +build/ +dist/ +target/ +.vs/ +bin/ +obj/ +publish/ + +# Default files to ignore +poetry.lock +package-lock.json +Cargo.lock +.DS_Store +yarn.lock +copcon_output.txt + +# Additional ignore patterns +*.log diff --git a/copcon/core/data/__init__.py b/copcon/core/data/__init__.py new file mode 100644 index 0000000..ee8b2bd --- /dev/null +++ b/copcon/core/data/__init__.py @@ -0,0 +1,10 @@ +from pathlib import Path + +default_copconignore_path = Path(__file__).parent / ".copconignore" + + +def read_copconignore_patterns(path: Path) -> list[str]: + with path.open() as file: + return [ + line.strip() for line in file if line.strip() and not line.startswith("#") + ] diff --git a/copcon/core/file_filter.py b/copcon/core/file_filter.py index 5c0f809..07ec4fe 100644 --- a/copcon/core/file_filter.py +++ b/copcon/core/file_filter.py @@ -4,12 +4,13 @@ specified in `.copconignore` files and additional user-defined patterns. """ -from pathlib import Path -from typing import List, Set, Optional, Tuple import pathspec + +from pathlib import Path +from typing import List, Optional from copcon.exceptions import FileReadError from copcon.utils.logger import logger -import importlib.resources as pkg_resources +from copcon.core.data import read_copconignore_patterns, default_copconignore_path class FileFilter: @@ -38,20 +39,19 @@ def __init__( """ # Load internal patterns self.ignore_spec = self._load_internal_copconignore() - self.user_defined = False # Flag to indicate if user-defined .copconignore was loaded # Load user-specified patterns if any if user_ignore_path and user_ignore_path.exists(): try: - with user_ignore_path.open() as f: - user_patterns = [line.strip() for line in f if line.strip() and not line.startswith("#")] + user_patterns = read_copconignore_patterns(user_ignore_path) user_spec = pathspec.PathSpec.from_lines("gitwildmatch", user_patterns) self.ignore_spec = self.ignore_spec + user_spec # Merge user patterns - self.user_defined = True # Set flag as user-defined .copconignore is loaded logger.debug(f"Loaded user ignore patterns from {user_ignore_path}") except Exception as e: logger.error(f"Error reading user ignore file {user_ignore_path}: {e}") - raise FileReadError(f"Error reading user ignore file {user_ignore_path}: {e}") + raise FileReadError( + f"Error reading user ignore file {user_ignore_path}: {e}" + ) # Add additional directories and files to ignore if additional_dirs: @@ -75,8 +75,7 @@ def _load_internal_copconignore(self) -> pathspec.PathSpec: """ try: - with pkg_resources.open_text('copcon.core', '.copconignore') as f: - patterns = [line.strip() for line in f if line.strip() and not line.startswith("#")] + patterns = read_copconignore_patterns(default_copconignore_path) spec = pathspec.PathSpec.from_lines("gitwildmatch", patterns) logger.debug("Loaded internal .copconignore patterns.") return spec @@ -106,11 +105,3 @@ def should_ignore(self, path: Path) -> bool: if path.is_file() and path.name in self.ignore_files: return True return False - - def has_user_defined_ignore(self) -> bool: - """Check if a user-defined .copconignore was loaded. - - Returns: - bool: True if a user-defined .copconignore was loaded, False otherwise. - """ - return self.user_defined