Last active
July 26, 2024 07:09
-
-
Save tarekbadrsh/e6aa6cde277cf1af9031e245b0b82d58 to your computer and use it in GitHub Desktop.
Multi-File Commenter and Combiner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import fnmatch | |
def get_comment_prefix(filename): | |
extension_to_comment = { | |
'.asm': (';', ';'), | |
'.awk': ('#', '#'), | |
'.c': ('//', '//'), | |
'.clj': (';;', ';;'), | |
'.cpp': ('//', '//'), | |
'.css': ('/*', '*/'), | |
'.cs': ('//', '//'), | |
'.dart': ('//', '//'), | |
'.dockerfile': ('#', '#'), | |
'.ex': ('#', '#'), | |
'.erl': ('%', '%'), | |
'.fs': ('//', '//'), | |
'.f90': ('!', '!'), | |
'.go': ('//', '//'), | |
'.groovy': ('//', '//'), | |
'.hs': ('--', '--'), | |
'.html': ('<!--', '-->'), | |
'.java': ('//', '//'), | |
'.js': ('//', '//'), | |
'.jl': ('#', '#'), | |
'.kt': ('//', '//'), | |
'.latex': ('%', '%'), | |
'.lisp': (';', ';'), | |
'.lua': ('--', '--'), | |
'.mk': ('#', '#'), | |
'.md': ('<!--', '-->'), | |
'.m': ('%', '%'), | |
'.mm': ('//', '//'), | |
'.ml': ('//', '//'), | |
'.pas': ('//', '//'), | |
'.pl': ('#', '#'), | |
'.php': ('//', '//'), | |
'.plain': ('#', '#'), | |
'.ps1': ('#', '#'), | |
'.py': ('##', '##'), | |
'.r': ('#', '#'), | |
'.rb': ('#', '#'), | |
'.rs': ('//', '//'), | |
'.scala': ('//', '//'), | |
'.scm': (';', ';'), | |
'.sed': ('#', '#'), | |
'.sh': ('#', '#'), | |
'.st': ('"', '"'), | |
'.sql': ('--', '--'), | |
'.swift': ('//', '//'), | |
'.ts': ('//', '//'), | |
'.tsx': ('//', '//'), | |
'.vb': ("'", "'"), | |
'.xml': ('<!--', '-->'), | |
'.yaml': ('#', '#'), | |
} | |
_, ext = os.path.splitext(filename) | |
return extension_to_comment.get(ext, ('#', '#')) | |
def read_gitignore(directory): | |
result = [".git", ".gitignore", ".dockerignore"] | |
gitignore_path = os.path.join(directory, '.gitignore') | |
if os.path.exists(gitignore_path): | |
with open(gitignore_path, 'r') as gitignore_file: | |
result.extend([line.strip() for line in gitignore_file if line.strip( | |
) and not line.startswith('#')]) | |
return result | |
def should_ignore(path, ignore_patterns): | |
for pattern in ignore_patterns: | |
if fnmatch.fnmatch(path, pattern) or fnmatch.fnmatch(os.path.basename(path), pattern): | |
return True | |
return False | |
def read_and_save_files(directory, output_file, ignore_patterns): | |
with open(output_file, 'w') as outfile: | |
for root, dirs, files in os.walk(directory): | |
# Remove directories that match ignore patterns | |
dirs[:] = [d for d in dirs if not should_ignore( | |
os.path.join(root, d), ignore_patterns)] | |
for filename in files: | |
full_path = os.path.join(root, filename) | |
if should_ignore(full_path, ignore_patterns): | |
continue | |
comment_prefix = get_comment_prefix(filename) | |
try: | |
with open(full_path, 'r') as infile: | |
outfile.write( | |
f"{comment_prefix[0]}---FILE_PATH---{full_path}---FILE_PATH---{comment_prefix[1]}\n") | |
outfile.write(infile.read()) | |
outfile.write("\n") | |
except Exception as e: | |
print(f"Error reading file {full_path}: {e}") | |
if __name__ == "__main__": | |
directory = input("Enter the directory path: ") | |
output_file = os.path.join( | |
directory, f"{os.path.basename(directory)}_output.txt") | |
# Read patterns from .gitignore if it exists | |
ignore_patterns = read_gitignore(directory) | |
read_and_save_files(directory, output_file, ignore_patterns) | |
print(f"Output saved to {output_file}") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank @EmadAnwer, for your suggestions on using
.gitignore
in the script.I have made some updates to your code and hope they are helpful for everyone.
Cheers 🍻