|
| 1 | +import argparse |
| 2 | +from pathlib import Path |
| 3 | +from tabulate import tabulate |
| 4 | + |
| 5 | +from extliner.main import LineCounter |
| 6 | + |
| 7 | +def main(): |
| 8 | + parser = argparse.ArgumentParser(description="Count lines in files by extension.") |
| 9 | + parser.add_argument( |
| 10 | + "-d", "--directory", type=Path, required=True, |
| 11 | + help="Directory to count lines in." |
| 12 | + ) |
| 13 | + parser.add_argument( |
| 14 | + "--ignore", nargs="*", default=[], |
| 15 | + help="List of file extensions to ignore (e.g., .log .json)" |
| 16 | + ) |
| 17 | + |
| 18 | + args = parser.parse_args() |
| 19 | + |
| 20 | + if not args.directory.is_dir(): |
| 21 | + print(f"Error: {args.directory} is not a valid directory") |
| 22 | + return |
| 23 | + |
| 24 | + counter = LineCounter(ignore_extensions=args.ignore) |
| 25 | + result = counter.count_lines(args.directory) |
| 26 | + |
| 27 | + # Remove extensions with 0 lines |
| 28 | + result = { |
| 29 | + ext: counts for ext, counts in result.items() |
| 30 | + if counts["with_spaces"] > 0 or counts["without_spaces"] > 0 |
| 31 | + } |
| 32 | + |
| 33 | + # Sort result by extension |
| 34 | + result = dict(sorted(result.items())) |
| 35 | + |
| 36 | + total_with_spaces = sum(counts["with_spaces"] for counts in result.values()) |
| 37 | + |
| 38 | + # sort the result by the number of lines with spaces |
| 39 | + result = dict(sorted(result.items(), key=lambda item: item[1]["with_spaces"], reverse=True)) |
| 40 | + |
| 41 | + table = [] |
| 42 | + for ext, counts in result.items(): |
| 43 | + with_spaces = counts["with_spaces"] |
| 44 | + without_spaces = counts["without_spaces"] |
| 45 | + percent = (with_spaces / total_with_spaces * 100) if total_with_spaces else 0 |
| 46 | + table.append([ext, with_spaces, without_spaces, f"{percent:.2f}%"]) |
| 47 | + |
| 48 | + print(tabulate( |
| 49 | + table, |
| 50 | + headers=["Extension", "With Spaces", "Without Spaces", "% of Total"], |
| 51 | + tablefmt="grid" |
| 52 | + )) |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + main() |
0 commit comments