mirror of
https://github.com/kevinveenbirkenbach/directory-content-scanner.git
synced 2024-11-22 04:51:03 +01:00
Refactored to class
This commit is contained in:
parent
ab609af3bd
commit
1fbf43bfeb
166
scan.py
166
scan.py
@ -3,76 +3,110 @@ import argparse
|
|||||||
import re
|
import re
|
||||||
import zlib
|
import zlib
|
||||||
|
|
||||||
def remove_comments(content, file_type):
|
class CodeProcessor:
|
||||||
"""Remove comments from the content based on file type."""
|
PYTHON = ".py"
|
||||||
if file_type == ".py":
|
JS = ".js"
|
||||||
# Remove Python single line comments
|
C = ".c"
|
||||||
content = re.sub(r'^\s*#.*\n?', '', content, flags=re.MULTILINE)
|
CPP = ".cpp"
|
||||||
# Remove triple double-quote and triple single-quote docstrings
|
H = ".h"
|
||||||
content = re.sub(r'\"\"\"(.*?)\"\"\"', '', content, flags=re.DOTALL)
|
|
||||||
content = re.sub(r"\'\'\'(.*?)\'\'\'", '', content, flags=re.DOTALL)
|
|
||||||
elif file_type == ".js" or file_type == ".c" or file_type == ".cpp" or file_type == ".h":
|
|
||||||
# Remove C-style comments (// and /* */)
|
|
||||||
content = re.sub(r'\s*//.*', '', content)
|
|
||||||
content = re.sub(r'/\*.*?\*/', '', content, flags=re.DOTALL)
|
|
||||||
# Add more file types and their comment styles as needed
|
|
||||||
return content.strip()
|
|
||||||
|
|
||||||
def compress_code(content):
|
@staticmethod
|
||||||
"""Compress code using zlib."""
|
def remove_comments(content, file_type):
|
||||||
return zlib.compress(content.encode())
|
"""Remove comments based on file type."""
|
||||||
|
comment_patterns = {
|
||||||
|
CodeProcessor.PYTHON: [
|
||||||
|
(r'^\s*#.*\n?', ''),
|
||||||
|
(r'\"\"\"(.*?)\"\"\"', ''),
|
||||||
|
(r"\'\'\'(.*?)\'\'\'", '')
|
||||||
|
],
|
||||||
|
CodeProcessor.JS: [
|
||||||
|
(r'\s*//.*', ''),
|
||||||
|
(r'/\*.*?\*/', '')
|
||||||
|
],
|
||||||
|
CodeProcessor.C: [
|
||||||
|
(r'\s*//.*', ''),
|
||||||
|
(r'/\*.*?\*/', '')
|
||||||
|
],
|
||||||
|
CodeProcessor.CPP: [
|
||||||
|
(r'\s*//.*', ''),
|
||||||
|
(r'/\*.*?\*/', '')
|
||||||
|
],
|
||||||
|
CodeProcessor.H: [
|
||||||
|
(r'\s*//.*', ''),
|
||||||
|
(r'/\*.*?\*/', '')
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
def filter_directories(dirs, ignore_strings, ignore_hidden):
|
patterns = comment_patterns.get(file_type, [])
|
||||||
"""Filter out directories based on ignore criteria."""
|
for pattern, repl in patterns:
|
||||||
if ignore_hidden:
|
content = re.sub(pattern, repl, content, flags=re.DOTALL)
|
||||||
dirs[:] = [d for d in dirs if not d.startswith('.')]
|
return content.strip()
|
||||||
dirs[:] = [d for d in dirs if not any(ig in d for ig in ignore_strings)]
|
|
||||||
|
|
||||||
def should_print_file(file_path, file_filters, ignore_strings, ignore_hidden, include_strings):
|
@staticmethod
|
||||||
"""Determine if a file should be printed based on filters."""
|
def compress(content):
|
||||||
if ignore_hidden and os.path.basename(file_path).startswith('.'):
|
"""Compress code using zlib."""
|
||||||
return False
|
return zlib.compress(content.encode())
|
||||||
if file_filters and not any(file_path.endswith(file_type) for file_type in file_filters):
|
|
||||||
return False
|
|
||||||
if any(ignore_str in file_path for ignore_str in ignore_strings):
|
|
||||||
return False
|
|
||||||
if include_strings and not any(include_str in file_path for include_str in include_strings):
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
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 = remove_comments(content, file_type)
|
|
||||||
print(f"======== File Path: {file_path} ========")
|
|
||||||
if compress:
|
|
||||||
compressed_content = compress_code(content)
|
|
||||||
print(f"======== Compressed Code ========")
|
|
||||||
print(compressed_content)
|
|
||||||
else:
|
|
||||||
print(content)
|
|
||||||
print("==================================\n")
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
print(f"Warning: Could not read file due to encoding issues: {file_path}")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
def handle_directory(directory, file_filters, ignore_strings, ignore_hidden, verbose, no_comments, compress, strings):
|
class DirectoryHandler:
|
||||||
"""Handle scanning and printing for directories."""
|
|
||||||
for root, dirs, files in os.walk(directory):
|
@staticmethod
|
||||||
filter_directories(dirs, ignore_strings, ignore_hidden)
|
def filter_directories(dirs, ignore_strings, ignore_hidden):
|
||||||
for file in files:
|
"""Filter out directories based on ignore criteria."""
|
||||||
if should_print_file(os.path.join(root, file), file_filters, ignore_strings, ignore_hidden, strings):
|
if ignore_hidden:
|
||||||
print_file_content(os.path.join(root, file), no_comments, compress)
|
dirs[:] = [d for d in dirs if not d.startswith('.')]
|
||||||
elif verbose:
|
dirs[:] = [d for d in dirs if not any(ig in d for ig in ignore_strings)]
|
||||||
print(f"Skipped file: {file}")
|
|
||||||
|
@staticmethod
|
||||||
|
def should_print_file(file_path, file_filters, ignore_strings, ignore_hidden, include_strings):
|
||||||
|
"""Determine if a file should be printed."""
|
||||||
|
if ignore_hidden and os.path.basename(file_path).startswith('.'):
|
||||||
|
return False
|
||||||
|
if file_filters and not any(file_path.endswith(file_type) for file_type in file_filters):
|
||||||
|
return False
|
||||||
|
if any(ignore_str in file_path for ignore_str in ignore_strings):
|
||||||
|
return False
|
||||||
|
if include_strings and not any(include_str in file_path for include_str in include_strings):
|
||||||
|
return False
|
||||||
|
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"======== File Path: {file_path} ========")
|
||||||
|
if compress:
|
||||||
|
compressed_content = CodeProcessor.compress(content)
|
||||||
|
print(f"======== Compressed Code ========")
|
||||||
|
print(compressed_content)
|
||||||
|
else:
|
||||||
|
print(content)
|
||||||
|
print("==================================\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."""
|
||||||
|
for root, dirs, files in os.walk(directory):
|
||||||
|
DirectoryHandler.filter_directories(dirs, kwargs['ignore_strings'], kwargs['ignore_hidden'])
|
||||||
|
for file in files:
|
||||||
|
if DirectoryHandler.should_print_file(os.path.join(root, file), kwargs['file_filters'], kwargs['ignore_strings'], kwargs['ignore_hidden'], kwargs['strings']):
|
||||||
|
DirectoryHandler.print_file_content(os.path.join(root, file), kwargs['no_comments'], kwargs['compress'])
|
||||||
|
elif kwargs['verbose']:
|
||||||
|
print(f"Skipped file: {file}")
|
||||||
|
|
||||||
|
@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'])
|
||||||
|
|
||||||
def handle_file(file_path, file_filters, ignore_strings, ignore_hidden, no_comments, compress):
|
|
||||||
"""Handle scanning and printing for individual files."""
|
|
||||||
print_file_content(file_path, no_comments, compress)
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description="Scan directories and print/compile file contents.")
|
parser = argparse.ArgumentParser(description="Scan directories and print/compile file contents.")
|
||||||
@ -88,9 +122,9 @@ def main():
|
|||||||
|
|
||||||
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, args.compress, args.strings)
|
DirectoryHandler.handle_directory(path, file_filters=args.filetype, ignore_strings=args.ignore, ignore_hidden=args.ignore_hidden, verbose=args.verbose, no_comments=args.no_comments, compress=args.compress, strings=args.strings)
|
||||||
elif os.path.isfile(path):
|
elif os.path.isfile(path):
|
||||||
handle_file(path, args.filetype, args.ignore, args.ignore_hidden, args.no_comments, args.compress)
|
DirectoryHandler.handle_file(path, file_filters=args.filetype, ignore_strings=args.ignore, ignore_hidden=args.ignore_hidden, no_comments=args.no_comments, compress=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)
|
||||||
|
Loading…
Reference in New Issue
Block a user