Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions copcon/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from copcon.utils.logger import logger


app = typer.Typer()
app = typer.Typer(name="copcon", help="Test")
Copy link
Copy Markdown
Owner

@kasperjunge kasperjunge Jan 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

help="Test" should be removed 👍



@app.command(no_args_is_help=True)
def main(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)

Expand All @@ -113,5 +118,6 @@ def main(
logger.exception("An unexpected error occurred.")
raise typer.Exit(code=1)


if __name__ == "__main__":
app()
26 changes: 26 additions & 0 deletions copcon/core/data/.copconignore
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions copcon/core/data/__init__.py
Original file line number Diff line number Diff line change
@@ -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("#")
]
27 changes: 9 additions & 18 deletions copcon/core/file_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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