mirror of
https://github.com/kevinveenbirkenbach/directory-content-scanner.git
synced 2024-11-22 04:51:03 +01:00
implemented string whitelist
This commit is contained in:
parent
36c5b62812
commit
e78834da08
10
README.md
10
README.md
@ -29,10 +29,12 @@ python3 scan.py /path/to/directory [options]
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Options:
|
### Options:
|
||||||
- `-f, --filetype`: Filter by file types (e.g., `.txt`, `.log`).
|
|
||||||
- `-i, --ignore`: Ignore files and folders containing specific strings.
|
To show the help page execute:
|
||||||
- `--ignore-hidden`: Omit hidden directories from the scan.
|
|
||||||
- `-v, --verbose`: Enable verbose mode for detailed output.
|
```bash
|
||||||
|
python3 scan.py /path/to/directory --help
|
||||||
|
```
|
||||||
|
|
||||||
## ⚠️ Caution
|
## ⚠️ Caution
|
||||||
|
|
||||||
|
24
scan.py
24
scan.py
@ -57,17 +57,24 @@ class DirectoryHandler:
|
|||||||
dirs[:] = [d for d in dirs if not d.startswith('.')]
|
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_strings)]
|
dirs[:] = [d for d in dirs if not any(ig in d for ig in ignore_strings)]
|
||||||
|
|
||||||
@staticmethod
|
def should_print_file(file_path, file_filters, ignore_strings, ignore_hidden, whitelist_file, whitelist_content):
|
||||||
def should_print_file(file_path, file_filters, ignore_strings, ignore_hidden, include_strings):
|
"""Determine if a file should be printed based on new criteria."""
|
||||||
"""Determine if a file should be printed."""
|
|
||||||
if ignore_hidden and os.path.basename(file_path).startswith('.'):
|
if ignore_hidden and os.path.basename(file_path).startswith('.'):
|
||||||
return False
|
return False
|
||||||
if file_filters and not any(file_path.endswith(file_type) for file_type in file_filters):
|
if file_filters and not any(file_path.endswith(file_type) for file_type in file_filters):
|
||||||
return False
|
return False
|
||||||
if any(ignore_str in file_path for ignore_str in ignore_strings):
|
if any(ignore_str in file_path for ignore_str in ignore_strings):
|
||||||
return False
|
return False
|
||||||
if include_strings and not any(include_str in file_path for include_str in include_strings):
|
if whitelist_file and not any(whitelist_str in file_path for whitelist_str in whitelist_file):
|
||||||
return False
|
return False
|
||||||
|
if whitelist_content:
|
||||||
|
try:
|
||||||
|
with open(file_path, 'r') as f:
|
||||||
|
content = f.read()
|
||||||
|
if not any(whitelist_str in content for whitelist_str in whitelist_content):
|
||||||
|
return False
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -97,7 +104,7 @@ class DirectoryHandler:
|
|||||||
for root, dirs, files in os.walk(directory):
|
for root, dirs, files in os.walk(directory):
|
||||||
DirectoryHandler.filter_directories(dirs, kwargs['ignore_strings'], kwargs['ignore_hidden'])
|
DirectoryHandler.filter_directories(dirs, kwargs['ignore_strings'], kwargs['ignore_hidden'])
|
||||||
for file in files:
|
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']):
|
if DirectoryHandler.should_print_file(os.path.join(root, file), kwargs['file_filters'], kwargs['ignore_strings'], kwargs['ignore_hidden'], kwargs['whitelist_file'], kwargs['whitelist_content']):
|
||||||
DirectoryHandler.print_file_content(os.path.join(root, file), kwargs['no_comments'], kwargs['compress'])
|
DirectoryHandler.print_file_content(os.path.join(root, file), kwargs['no_comments'], kwargs['compress'])
|
||||||
elif kwargs['verbose']:
|
elif kwargs['verbose']:
|
||||||
print(f"Skipped file: {file}")
|
print(f"Skipped file: {file}")
|
||||||
@ -116,15 +123,16 @@ def main():
|
|||||||
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("-s", "--strings", nargs='+', default=[], help="Only display files whose paths contain one of these strings.")
|
|
||||||
parser.add_argument("--compress", action='store_true', help="Compress code (for Python files).")
|
parser.add_argument("--compress", action='store_true', help="Compress code (for Python files).")
|
||||||
|
parser.add_argument("--whitelist-file", nargs='+', default=[], help="Only display files whose paths contain one of these strings.")
|
||||||
|
parser.add_argument("--whitelist-content", nargs='+', default=[], help="Only display files containing one of these strings in their content.")
|
||||||
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):
|
||||||
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)
|
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, whitelist_file=args.whitelist_file, whitelist_content=args.whitelist_content)
|
||||||
elif os.path.isfile(path):
|
elif os.path.isfile(path):
|
||||||
if DirectoryHandler.should_print_file(path, file_filters=args.filetype, ignore_strings=args.ignore, ignore_hidden=args.ignore_hidden, include_strings=args.strings):
|
if DirectoryHandler.should_print_file(path, file_filters=args.filetype, ignore_strings=args.ignore, ignore_hidden=args.ignore_hidden, whitelist_file=args.whitelist_file, whitelist_content=args.whitelist_content):
|
||||||
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)
|
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.")
|
||||||
|
Loading…
Reference in New Issue
Block a user