mirror of
				https://github.com/kevinveenbirkenbach/directory-content-scanner.git
				synced 2025-11-04 03:48:22 +00:00 
			
		
		
		
	added compress function
This commit is contained in:
		
							
								
								
									
										29
									
								
								scan.py
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								scan.py
									
									
									
									
									
								
							@@ -1,6 +1,7 @@
 | 
				
			|||||||
import os
 | 
					import os
 | 
				
			||||||
import argparse
 | 
					import argparse
 | 
				
			||||||
import re
 | 
					import re
 | 
				
			||||||
 | 
					import zlib
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def remove_comments(content, file_type):
 | 
					def remove_comments(content, file_type):
 | 
				
			||||||
    """Remove comments from the content based on file type."""
 | 
					    """Remove comments from the content based on file type."""
 | 
				
			||||||
@@ -17,6 +18,10 @@ def remove_comments(content, file_type):
 | 
				
			|||||||
    # Add more file types and their comment styles as needed
 | 
					    # Add more file types and their comment styles as needed
 | 
				
			||||||
    return content.strip()
 | 
					    return content.strip()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def compress_code(content):
 | 
				
			||||||
 | 
					    """Compress code using zlib."""
 | 
				
			||||||
 | 
					    return zlib.compress(content.encode())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def filter_directories(dirs, ignore_strings, ignore_hidden):
 | 
					def filter_directories(dirs, ignore_strings, ignore_hidden):
 | 
				
			||||||
    """Filter out directories based on ignore criteria."""
 | 
					    """Filter out directories based on ignore criteria."""
 | 
				
			||||||
    if ignore_hidden:
 | 
					    if ignore_hidden:
 | 
				
			||||||
@@ -33,52 +38,56 @@ def should_print_file(file, file_filters, ignore_strings, ignore_hidden):
 | 
				
			|||||||
        return False
 | 
					        return False
 | 
				
			||||||
    return True
 | 
					    return True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def print_file_content(file_path, no_comments):
 | 
					def print_file_content(file_path, no_comments, compress):
 | 
				
			||||||
    """Print the content of a file."""
 | 
					    """Print the content of a file."""
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        with open(file_path, 'r') as f:
 | 
					        with open(file_path, 'r') as f:
 | 
				
			||||||
            content = f.read()
 | 
					            content = f.read()
 | 
				
			||||||
        if no_comments:
 | 
					        if no_comments:
 | 
				
			||||||
            file_type = os.path.splitext(file_path)[1]
 | 
					            file_type = os.path.splitext(file_path)[1]
 | 
				
			||||||
            print(file_type)
 | 
					 | 
				
			||||||
            content = remove_comments(content, file_type)
 | 
					            content = remove_comments(content, file_type)
 | 
				
			||||||
        print(f"======== File Path: {file_path} ========")
 | 
					        print(f"======== File Path: {file_path} ========")
 | 
				
			||||||
 | 
					        if compress:
 | 
				
			||||||
 | 
					            compressed_content = compress_code(content)
 | 
				
			||||||
 | 
					            print(f"======== Compressed Code ========")
 | 
				
			||||||
 | 
					            print(compressed_content)
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
            print(content)
 | 
					            print(content)
 | 
				
			||||||
        print("==================================\n")
 | 
					        print("==================================\n")
 | 
				
			||||||
    except UnicodeDecodeError:
 | 
					    except UnicodeDecodeError:
 | 
				
			||||||
        print(f"Warning: Could not read file due to encoding issues: {file_path}")
 | 
					        print(f"Warning: Could not read file due to encoding issues: {file_path}")
 | 
				
			||||||
        exit(1)
 | 
					        exit(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def handle_directory(directory, file_filters, ignore_strings, ignore_hidden, verbose, no_comments):
 | 
					def handle_directory(directory, file_filters, ignore_strings, ignore_hidden, verbose, no_comments, compress):
 | 
				
			||||||
    """Handle scanning and printing for directories."""
 | 
					    """Handle scanning and printing for directories."""
 | 
				
			||||||
    for root, dirs, files in os.walk(directory):
 | 
					    for root, dirs, files in os.walk(directory):
 | 
				
			||||||
        filter_directories(dirs, ignore_strings, ignore_hidden)
 | 
					        filter_directories(dirs, ignore_strings, ignore_hidden)
 | 
				
			||||||
        for file in files:
 | 
					        for file in files:
 | 
				
			||||||
            if should_print_file(file, file_filters, ignore_strings, ignore_hidden):
 | 
					            if should_print_file(file, file_filters, ignore_strings, ignore_hidden):
 | 
				
			||||||
                print_file_content(os.path.join(root, file), no_comments)
 | 
					                print_file_content(os.path.join(root, file), no_comments, compress)
 | 
				
			||||||
            elif verbose:
 | 
					            elif verbose:
 | 
				
			||||||
                print(f"Skipped file: {file}")
 | 
					                print(f"Skipped file: {file}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def handle_file(file_path, file_filters, ignore_strings, ignore_hidden):
 | 
					def handle_file(file_path, file_filters, ignore_strings, ignore_hidden, no_comments, compress):
 | 
				
			||||||
    """Handle scanning and printing for individual files."""
 | 
					    """Handle scanning and printing for individual files."""
 | 
				
			||||||
    if should_print_file(os.path.basename(file_path), file_filters, ignore_strings, ignore_hidden):
 | 
					    print_file_content(file_path, no_comments, compress)
 | 
				
			||||||
        print_file_content(file_path, no_comments)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
def main():
 | 
					def main():
 | 
				
			||||||
    parser = argparse.ArgumentParser(description="Scan directories and print file contents.")
 | 
					    parser = argparse.ArgumentParser(description="Scan directories and print/compile file contents.")
 | 
				
			||||||
    parser.add_argument("paths", nargs='+', help="List of files or directories to scan.")
 | 
					    parser.add_argument("paths", nargs='+', help="List of files or directories to scan.")
 | 
				
			||||||
    parser.add_argument("-f", "--filetype", nargs='+', default=[], help="Filter by file types (e.g., .txt .log).")
 | 
					    parser.add_argument("-f", "--filetype", nargs='+', default=[], help="Filter by file types (e.g., .txt .log).")
 | 
				
			||||||
    parser.add_argument("-i", "--ignore", nargs='+', default=[], help="Ignore files and folders containing these strings.")
 | 
					    parser.add_argument("-i", "--ignore", nargs='+', default=[], help="Ignore files and folders containing these strings.")
 | 
				
			||||||
    parser.add_argument("--ignore-hidden", action='store_true', help="Ignore hidden directories and files.")
 | 
					    parser.add_argument("--ignore-hidden", action='store_true', help="Ignore hidden directories and files.")
 | 
				
			||||||
    parser.add_argument("-v", "--verbose", action='store_true', help="Enable verbose mode.")
 | 
					    parser.add_argument("-v", "--verbose", action='store_true', help="Enable verbose mode.")
 | 
				
			||||||
    parser.add_argument("--no-comments", action='store_true', help="Remove comments from the displayed content based on file type.")
 | 
					    parser.add_argument("--no-comments", action='store_true', help="Remove comments from the displayed content based on file type.")
 | 
				
			||||||
 | 
					    parser.add_argument("--compress", action='store_true', help="Compress code (for Python files).")
 | 
				
			||||||
    args = parser.parse_args()
 | 
					    args = parser.parse_args()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for path in args.paths:
 | 
					    for path in args.paths:
 | 
				
			||||||
        if os.path.isdir(path):
 | 
					        if os.path.isdir(path):
 | 
				
			||||||
            handle_directory(path, args.filetype, args.ignore, args.ignore_hidden, args.verbose, args.no_comments)
 | 
					            handle_directory(path, args.filetype, args.ignore, args.ignore_hidden, args.verbose, args.no_comments, args.compress)
 | 
				
			||||||
        elif os.path.isfile(path):
 | 
					        elif os.path.isfile(path):
 | 
				
			||||||
            handle_file(path, args.filetype, args.ignore, args.ignore_hidden, args.no_comments)
 | 
					            handle_file(path, args.filetype, args.ignore, args.ignore_hidden, args.no_comments, args.compress)
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            print(f"Error: {path} is neither a valid file nor a directory.")
 | 
					            print(f"Error: {path} is neither a valid file nor a directory.")
 | 
				
			||||||
            exit(1)
 | 
					            exit(1)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user