From bf2f548a1f85d8c8d2e2fa52d006d3fe36492371 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Tue, 15 Apr 2025 21:52:42 +0200 Subject: [PATCH] Refactored --- __pycache__/cli.cpython-313.pyc | Bin 0 -> 1839 bytes __pycache__/code_processor.cpython-313.pyc | Bin 0 -> 2041 bytes __pycache__/directory_handler.cpython-313.pyc | Bin 0 -> 8121 bytes cli.py | 18 ++ code_processor.py | 54 ++++ directory_handler.py | 126 +++++++++ main.py | 242 +++--------------- 7 files changed, 231 insertions(+), 209 deletions(-) create mode 100644 __pycache__/cli.cpython-313.pyc create mode 100644 __pycache__/code_processor.cpython-313.pyc create mode 100644 __pycache__/directory_handler.cpython-313.pyc create mode 100644 cli.py create mode 100644 code_processor.py create mode 100644 directory_handler.py diff --git a/__pycache__/cli.cpython-313.pyc b/__pycache__/cli.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a72ab86dcc8e849b3924b47891a56b92b99b55cd GIT binary patch literal 1839 zcmaJ>&u<$=6rNqLozy?cFA_>90}3K5vTL;xLXl9RiBmu=Rh1PIsFlikcWke+o?XqX z4c=4#1rA6#att>gbL^k6g+r=^0}>~0seDI$GhW+G)OaM1_r3SM@4ffU?2amxB7*hH z>HqD$JVO5pW3trjVYCbnzak4+%00x8rCvu&X)Ai}KLL2)WH9-+0DBI8qZ38sJetN1 zCu~y>LLUO7rS2*eeTNRA`sHQiEX~SWh20#!3b9~mb!07C*LJllF-3_f&Bc_hidD60 zb1J=MEdgd3FjrSv0mSNDmNh`UJr}VqyWN=E?K={4eJi<-3hv(=M&r#n-kE8(1@G5l9pV;9p;6R7tk~x+EIt6 z$|vzh_dOorpd}MH4%h|WD8L+QnYx-iC3xH%Y? zm0r9}%(l6M&FJR{n|{#NO+ClNZ0UkaT6WKm*dkc*nqhct8Za`Zq%-SF z4A=^MmoP4;vONkY9CZjMI7>0(+LWr}xh|n;k%K_wNyK_2-pUBap$lV5c9;i*QqzL% z2#vH)QzAZ%viZr~C#t{4Ul3{bu+#Ex$tl5Wr!>W>|`#e%MA z6>9R(w+FN|C3w392lQ|db+Qbu%b~?-LJo~U z79M}r7?;VU`>6w+V|n1REMTA@5odl$zGYEZ-k2HL7Z=I0B0~E&abVyEQjbjrG~=oi zr|m`KLymVE0j5EOVZ0FTFE*#4tY5b$E3X`Q}+jXBn zRQl4Q1PPnNZ7)cRarm+!oc>tv1YJ@;B7Kkc3877o9l>rpo%$gO1MZDqqdK(G=`~GI z|BYCi+Wvrh++f6Z2cVHl>W=T3;UKL|tIKXU6W?L+apeZI@D%-BSv#w2{ZZL^vHI1^ zoLVhBy?b6j`O=xT{#;u>UighCH@{DW@xu$Pn0c!w&greB(UpEW^(PzkA%C`a4&QTS zPle!1g!239YjJ(JlN=n$G=J(o){c*a|B>`Rp80FTGH^AN#)Ru&#&ujid7OOd$P{;) zl5FV1mD46-&5{S;@`d=-yUuz4JK*%Kc%I!(&qEh=AWe- ziFRWIsv%uvW%!da{*Ks+cwT}nUHly0xt>pMvo$cq@$w36=Q%}D{zA2pR#V=W@K;J9 B@4Ns2 literal 0 HcmV?d00001 diff --git a/__pycache__/code_processor.cpython-313.pyc b/__pycache__/code_processor.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..7642b5f4f221ffa3535bc781658b5a8b6d6cd438 GIT binary patch literal 2041 zcmah}&1)M+6rbIdRx5cW%W+~7*B=|(A(2{ZOHvmMp~Z3B7}vFpCnhNoStNNZZ6ocf zvnzuIraF}*Js1k1?y04x1PaZue?w0erdW)72=tVj5(RoIeX}bW*$&Mt`}WOyzc=&V zzK>Qk8bPpr`TgJgpFxEF=9|t6w3*fwV4fh1u<$ve{#T&Fc{GNwIEb(`CHhDz=7mJy zCEqONe34JvdemBh{S%Z%RKSReSfCOXX#h(!hyzr{K^np`Rd9%gu|gy19vmJ-Q|*(e z3j0XcK8ky2EUzY_EIMnilND;`h~wDwC3n0krOI1CzH+K!Dpw7pa^*5`1rRxfYA}_} zIE7rtFMdM|UJ2gT9|HIB9+!D*wHwuzVF8O+y4U$bXjN`2x;`IC7$SQYMwhy7Qo77L zHwCe*i(MEmU&nFyBt|{1=etuF9`A{D3KKs8fGrkuq05^yZ%2F>vcxrU_1tOgnxLaj zy;#zk)Q{%hL#P6OzC35iU4LufwCxzWJ9k&cq3M8yz8FTR2We;xtzI~d?9|KzN+{Lw zRZ_AyiI%fVC1Sab2DvBenr&$tW|3&_R+*#{QdLen$?;T@L1Dsy)xPu?f3BpGpC){k zhJ5+uG(5iCW5G5Awz{55dJxYZgke3M1nYEC?-C0vxKYgHokW!K>S6NAm$&DamzYQi zlW#8Ho>^RE;sR#EER$weR+uo)0yk#xJPYFaInYUY;xfspWLeO0saZ~hSSV*(E(FHH zJRXC`!{Y5=jdI3y3AG%i9C4vG#{!g;i9* zE9k4FY&)iFQh?gid&B9Oj43r-)#|F&h>tb;F8xe@z53hqv+2F~)bU+26c~@aiFoj# zx;oT|k2m6%nn7ta<{h%L{Al?|HsZ?;)Zw~1d?eNW4>?j-N5F-qBwmcYk-ck1>*}a4 z8jCfr4Gl&1l=IE&=-&CDpT!~ zp=E^Xg;~2)rZ7#RY1WBWaZD?(Rf}d88ifTo!xYGv2VpX?I4U7f-tP`SN2Acxh+YK3 z2977RV|I;W2Z2B)WfVWVv;BB`_s&oEf4sjxJh|VS+EY?=1k}&aQlghBhGAtuRI zFiQ4%1q$6@s$qOx$rRg~Uc=ZhspA$+i&(Z{&@h-&J_{)?8_Fjh?d62eHQEP+sg9d* z&0LAN1$&(iak=O8Im7TZ2J{a#vlW*(aN-09aIgIMcmljO=8Url?=_W>YlL0IsYPlbV8zh w>{E>s@6rj*9XZ03>;6CaguV|7o&ksFqA3Z2aEQW(sOO)+RY8A^fOuyA0T2PUtN;K2 literal 0 HcmV?d00001 diff --git a/__pycache__/directory_handler.cpython-313.pyc b/__pycache__/directory_handler.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..556431d0c5cbbb7ab2340577993da6b4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xe!2x5(Vq)aH5TXmCT?Ald6vby+FNLt+&W} zQoo13HbiRjG+mXOQ_GFL9ki-5*yI`1BM71#|M1=SCTqoNnsK~*0H?Ewqys0(Sxx$5+RwdaCjmma0`{e=>!C+ zV{EhDE#~WD@fp~0j%wf#DH4cUDA5KGa?-^P!!I~YoJ(V(4Gj8I5t3LbK0*>=ZJ2Ok zyq6^8)8ZkLT!h4Dk*u1)^O9-$od}K#N_@~j0_3<8I!Q*d7DV33OW~TteN+@w1BYcw z5L%B7%}bsg+~@f&VVFf|14qw=nm0--e!TGh!jeB*>Jv+So1T}lp3|ZSKxfS}nz1)L zBJt**RV~+KJ)NT6eRW{TxM{D?+S^5Y`-&m+>WFwGm2YZG1mr^RMh z`C|mhlQLA%lyw~yT}M}~ciP0jSmw>QGLdQV&E$R8%tl4shi`uH=Ev4-Max=6ixS5^ zwtOH{d*r_B=#xfN)9|$sc@HV9Egv3<8_59L4Vi2CH^E&P+8Bh7R2Pz+GY3|2211h^ z-)i6qK9;zWh|NWk^06)WbJjTwK|R?JW!s`R)6fr1dYz zPzbPsfjluGYfaX2K(riKKD|049zAnsG7}1m!*AWQL}Zo7ahzdaiDU_f3AGQ0C0jT= zlZwqj#5^q7!{N64A&nlq{*)~;G@tCzZ<0!(HSm1m5M2jB>Ri(L;WB^&S~%j zDRPiV6=4Vyk#S-8;Rq)t=F*%H4$G1#GLfLVq7tGP@-O@XlD|e9M#p2i)ac!2kg0r& z1&iwo2qWgYg2TR+8+NU9K(BdqQrDmFr54E`3IqI%BzB2f3|gxnGU( zWwjsVwvNbkv~faNWt%2hWdlTH2;j(9ClBPM5)#*e8>ISsw@kj29Ug$@+^!`{x1#$V l%tHgQR{>") + if compress: + compressed_content = CodeProcessor.compress(content) + print("COMPRESSED CODE: ") + print(compressed_content) + else: + print(content) + print("<< END >>\n") + except UnicodeDecodeError: + print(f"Warning: Could not read file due to encoding issues: {file_path}") + exit(1) + + @staticmethod + def handle_directory(directory, **kwargs): + """Handle scanning and printing for directories.""" + gitignore_patterns = [] + if not kwargs.get('no_gitignore'): + gitignore_patterns = DirectoryHandler.load_gitignore_patterns(directory) + + for root, dirs, files in os.walk(directory): + DirectoryHandler.filter_directories(dirs, kwargs['ignore_file_strings'], kwargs['ignore_hidden']) + for file in files: + file_path = os.path.join(root, file) + if gitignore_patterns and DirectoryHandler.is_gitignored(file_path, gitignore_patterns): + if kwargs.get('verbose'): + print(f"Skipped (gitignored): {file_path}") + continue + + if DirectoryHandler.should_print_file( + file_path, + kwargs['file_types'], + kwargs['ignore_file_strings'], + kwargs['ignore_hidden'], + kwargs['path_contains'], + kwargs['content_contains'] + ): + DirectoryHandler.print_file_content(file_path, kwargs['no_comments'], kwargs['compress']) + elif kwargs.get('verbose'): + print(f"Skipped file: {file_path}") + + @staticmethod + def handle_file(file_path, **kwargs): + """Handle scanning and printing for individual files.""" + DirectoryHandler.print_file_content(file_path, kwargs['no_comments'], kwargs['compress']) diff --git a/main.py b/main.py index 591e375..641338b 100755 --- a/main.py +++ b/main.py @@ -1,222 +1,46 @@ #!/usr/bin/env python3 - import os -import argparse -import re -import zlib -import fnmatch - -class CodeProcessor: - PYTHON = ".py" - JS = ".js" - C = ".c" - CPP = ".cpp" - H = ".h" - BASH = ".sh" - SHELL = ".bash" - - @staticmethod - def remove_comments(content, file_type): - """Remove comments based on file type.""" - comment_patterns = { - CodeProcessor.PYTHON: [ - (r'\s*#.*', '',0), - (r'\"\"\"(.*?)\"\"\"', '', re.DOTALL), - (r"\'\'\'(.*?)\'\'\'", '', re.DOTALL) - ], - CodeProcessor.JS: [ - (r'\s*//.*', '',0), - (r'/\*.*?\*/', '',0) - ], - CodeProcessor.C: [ - (r'\s*//.*', '',0), - (r'/\*.*?\*/', '',0) - ], - CodeProcessor.CPP: [ - (r'\s*//.*', '',0), - (r'/\*.*?\*/', '',0) - ], - CodeProcessor.H: [ - (r'\s*//.*', '',0), - (r'/\*.*?\*/', '',0) - ], - CodeProcessor.BASH: [ - (r'\s*#.*', '', 0) - ], - CodeProcessor.SHELL: [ - (r'\s*#.*', '', 0) - ] - } - - patterns = comment_patterns.get(file_type, []) - for pattern, repl, flags in patterns: - content = re.sub(pattern, repl, content, flags=flags) - return content.strip() - - @staticmethod - def compress(content): - """Compress code using zlib.""" - return zlib.compress(content.encode()) - - -class DirectoryHandler: - - @staticmethod - def load_gitignore_patterns(root_path): - """Collect .gitignore patterns from root_path and all subdirectories.""" - gitignore_patterns = [] - - for dirpath, dirnames, filenames in os.walk(root_path): - if '.gitignore' in filenames: - gitignore_path = os.path.join(dirpath, '.gitignore') - with open(gitignore_path, 'r') as f: - for line in f: - line = line.strip() - if line and not line.startswith('#'): - gitignore_patterns.append(os.path.join(dirpath, line)) - return gitignore_patterns - - @staticmethod - def is_gitignored(file_path, gitignore_patterns): - """Check if file_path matches any .gitignore pattern.""" - for pattern in gitignore_patterns: - if fnmatch.fnmatch(file_path, pattern): - return True - return False - - @staticmethod - def filter_directories(dirs, ignore_file_strings, ignore_hidden): - """Filter out directories based on ignore criteria.""" - if ignore_hidden: - dirs[:] = [d for d in dirs if not d.startswith('.')] - dirs[:] = [d for d in dirs if not any(ig in d for ig in ignore_file_strings)] - - @staticmethod - def path_or_content_contains(file_path, path_contains, content_contains): - # Check if the file name contains specific strings (whitelist) - if path_contains and any(whitelist_str in file_path for whitelist_str in path_contains): - return True - - # Check file content for specific strings (if specified) - if content_contains: - try: - with open(file_path, 'r') as f: - content = f.read() - # Return True if any of the content_contains strings are found in the content - if any(whitelist_str in content for whitelist_str in content_contains): - return True - except UnicodeDecodeError: - # Return False if there's a Unicode decode error (file can't be read) - return False - return False - - @staticmethod - def should_print_file(file_path, file_types, ignore_file_strings, ignore_hidden, path_contains, content_contains): - """ - Determine if a file should be printed based on various criteria. - - Args: - file_path (str): The path of the file to be checked. - file_types (list): List of allowed file extensions. - ignore_file_strings (list): List of strings; if any are found in the file path, the file is ignored. - ignore_hidden (bool): If True, hidden files (starting with '.') are ignored. - path_contains (list): List of strings; the file is processed only if its path contains one of these strings. - content_contains (list): List of strings; the file is processed only if its content contains one of these strings. - - Returns: - bool: True if the file should be printed, False otherwise. - """ - - # Check for hidden files if ignore_hidden is enabled - if ignore_hidden and os.path.basename(file_path).startswith('.'): - return False - - # Check if the file type is in the allowed list (if specified) - if file_types and not any(file_path.endswith(file_type) for file_type in file_types): - return False - - # Check if the file should be ignored based on the presence of specific strings in its path - if any(ignore_str in file_path for ignore_str in ignore_file_strings): - return False - - if path_contains or content_contains: - return DirectoryHandler.path_or_content_contains(file_path, path_contains, content_contains) - return True - - @staticmethod - def print_file_content(file_path, no_comments, compress): - """Print the content of a file.""" - try: - with open(file_path, 'r') as f: - content = f.read() - if no_comments: - file_type = os.path.splitext(file_path)[1] - content = CodeProcessor.remove_comments(content, file_type) - print(f"<< START: {file_path} >>") - if compress: - compressed_content = CodeProcessor.compress(content) - print(f"COMPRESSED CODE: ") - print(compressed_content) - else: - print(content) - print("<< END >>\n") - except UnicodeDecodeError: - print(f"Warning: Could not read file due to encoding issues: {file_path}") - exit(1) - - @staticmethod - def handle_directory(directory, **kwargs): - """Handle scanning and printing for directories.""" - gitignore_patterns = [] - if not kwargs.get('no_gitignore'): - gitignore_patterns = DirectoryHandler.load_gitignore_patterns(directory) - - for root, dirs, files in os.walk(directory): - DirectoryHandler.filter_directories(dirs, kwargs['ignore_file_strings'], kwargs['ignore_hidden']) - for file in files: - file_path = os.path.join(root, file) - - if gitignore_patterns and DirectoryHandler.is_gitignored(file_path, gitignore_patterns): - if kwargs['verbose']: - print(f"Skipped (gitignored): {file_path}") - continue - - if DirectoryHandler.should_print_file(file_path, kwargs['file_types'], kwargs['ignore_file_strings'], kwargs['ignore_hidden'], kwargs['path_contains'], kwargs['content_contains']): - DirectoryHandler.print_file_content(file_path, kwargs['no_comments'], kwargs['compress']) - elif kwargs['verbose']: - print(f"Skipped file: {file_path}") - - - @staticmethod - def handle_file(file_path, **kwargs): - """Handle scanning and printing for individual files.""" - DirectoryHandler.print_file_content(file_path, kwargs['no_comments'], kwargs['compress']) - +import sys +from cli import parse_arguments +from directory_handler import DirectoryHandler def main(): - 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("--file-types", nargs='+', default=[], help="Filter by file types (e.g., .txt .log).") - parser.add_argument("--ignore-file-strings", 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("-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("--compress", action='store_true', help="Compress code (for Python files).") - parser.add_argument("--path-contains", nargs='+', default=[], help="Display files whose paths contain one of these strings.") - parser.add_argument("--content-contains", nargs='+', default=[], help="Display files containing one of these strings in their content.") - parser.add_argument("--no-gitignore", action='store_true', help="Do not respect .gitignore files during scan.") - - args = parser.parse_args() + args = parse_arguments() for path in args.paths: if os.path.isdir(path): - DirectoryHandler.handle_directory(path, file_types=args.file_types, ignore_file_strings=args.ignore_file_strings, ignore_hidden=args.ignore_hidden, verbose=args.verbose, no_comments=args.no_comments, compress=args.compress, path_contains=args.path_contains, content_contains=args.content_contains) + DirectoryHandler.handle_directory( + path, + file_types=args.file_types, + ignore_file_strings=args.ignore_file_strings, + ignore_hidden=args.ignore_hidden, + verbose=args.verbose, + no_comments=args.no_comments, + compress=args.compress, + path_contains=args.path_contains, + content_contains=args.content_contains, + no_gitignore=args.no_gitignore + ) elif os.path.isfile(path): - if DirectoryHandler.should_print_file(path, file_types=args.file_types, ignore_file_strings=args.ignore_file_strings, ignore_hidden=args.ignore_hidden, path_contains=args.path_contains, content_contains=args.content_contains): - DirectoryHandler.handle_file(path, file_types=args.file_types, ignore_file_strings=args.ignore_file_strings, ignore_hidden=args.ignore_hidden, no_comments=args.no_comments, compress=args.compress) + if DirectoryHandler.should_print_file( + path, + file_types=args.file_types, + ignore_file_strings=args.ignore_file_strings, + ignore_hidden=args.ignore_hidden, + path_contains=args.path_contains, + content_contains=args.content_contains + ): + DirectoryHandler.handle_file( + path, + file_types=args.file_types, + ignore_file_strings=args.ignore_file_strings, + ignore_hidden=args.ignore_hidden, + no_comments=args.no_comments, + compress=args.compress + ) else: print(f"Error: {path} is neither a valid file nor a directory.") - exit(1) + sys.exit(1) if __name__ == "__main__": main() \ No newline at end of file