87 lines
4.0 KiB
Python
87 lines
4.0 KiB
Python
import pathlib
|
|
|
|
def is_item_ignored(item_name, ignored_directories_list, ignored_extensions_list):
|
|
if item_name in ignored_directories_list:
|
|
return True
|
|
for extension_string in ignored_extensions_list:
|
|
if item_name.endswith(extension_string):
|
|
return True
|
|
return False
|
|
|
|
def format_tree_line(entry_name, prefix_string, is_last_element):
|
|
connector_string = '└── ' if is_last_element else '├── '
|
|
return prefix_string + connector_string + entry_name
|
|
|
|
def format_file_section(relative_path_string, file_content_string):
|
|
header_string = '\n--- FILE START: ' + relative_path_string + ' ---\n'
|
|
footer_string = '\n--- FILE END: ' + relative_path_string + ' ---\n'
|
|
return header_string + file_content_string + footer_string
|
|
|
|
def build_final_output_string(tree_lines_list, code_contents_list):
|
|
tree_string = '\n'.join(tree_lines_list)
|
|
code_string = ''.join(code_contents_list)
|
|
return 'PROJECT STRUCTURE:\n' + tree_string + '\n\n' + code_string
|
|
|
|
def collect_file_system_nodes(root_path, ignored_directories_list, ignored_extensions_list):
|
|
valid_nodes_list = []
|
|
try:
|
|
directory_items_list = list(root_path.iterdir())
|
|
except PermissionError:
|
|
return valid_nodes_list
|
|
|
|
for item_path in directory_items_list:
|
|
if not is_item_ignored(item_path.name, ignored_directories_list, ignored_extensions_list):
|
|
valid_nodes_list.append(item_path)
|
|
|
|
valid_nodes_list.sort(key=lambda path_object: (not path_object.is_dir(), path_object.name.lower()))
|
|
return valid_nodes_list
|
|
|
|
def read_file_content_safely(file_path_object):
|
|
try:
|
|
return file_path_object.read_text(encoding='utf-8')
|
|
except UnicodeDecodeError:
|
|
return '[BINARY_OR_UNSUPPORTED_ENCODING]'
|
|
except PermissionError:
|
|
return '[PERMISSION_DENIED]'
|
|
|
|
def write_output_file(output_file_path, content_string):
|
|
output_file_path.write_text(content_string, encoding='utf-8')
|
|
|
|
def process_directory_recursively(current_path, base_directory_path, prefix_string, ignored_directories_list, ignored_extensions_list, tree_lines_list, code_contents_list):
|
|
nodes_list = collect_file_system_nodes(current_path, ignored_directories_list, ignored_extensions_list)
|
|
total_nodes_count = len(nodes_list)
|
|
|
|
for index_number, node_path in enumerate(nodes_list):
|
|
is_last_node = index_number == total_nodes_count - 1
|
|
formatted_tree_line = format_tree_line(node_path.name, prefix_string, is_last_node)
|
|
tree_lines_list.append(formatted_tree_line)
|
|
|
|
if node_path.is_dir():
|
|
new_prefix_string = prefix_string + (' ' if is_last_node else '│ ')
|
|
process_directory_recursively(node_path, base_directory_path, new_prefix_string, ignored_directories_list, ignored_extensions_list, tree_lines_list, code_contents_list)
|
|
else:
|
|
relative_file_path_string = str(node_path.relative_to(base_directory_path))
|
|
file_content_string = read_file_content_safely(node_path)
|
|
formatted_file_section = format_file_section(relative_file_path_string, file_content_string)
|
|
code_contents_list.append(formatted_file_section)
|
|
|
|
def execute_bundler_script():
|
|
target_output_file_name_string = 'output_fuer_ki.txt'
|
|
ignored_directories_list = ['.git', '__pycache__', 'venv', 'env', '.venv', 'node_modules', '.idea', '.vscode', 'instance']
|
|
ignored_extensions_list = ['.pyc', '.sqlite', '.db', '.png', '.jpg', '.jpeg', '.svg', '.ico', '.exe', '.pdf', '.zip', '.tar.gz', '.mp3', '.mp4', target_output_file_name_string]
|
|
|
|
current_working_directory_path = pathlib.Path.cwd()
|
|
output_file_path = current_working_directory_path / target_output_file_name_string
|
|
|
|
tree_lines_list = [current_working_directory_path.name]
|
|
code_contents_list = []
|
|
|
|
process_directory_recursively(current_working_directory_path, current_working_directory_path, '', ignored_directories_list, ignored_extensions_list, tree_lines_list, code_contents_list)
|
|
|
|
final_output_string = build_final_output_string(tree_lines_list, code_contents_list)
|
|
write_output_file(output_file_path, final_output_string)
|
|
|
|
print('Erfolg! Die Datei ' + target_output_file_name_string + ' wurde generiert.')
|
|
|
|
if __name__ == '__main__':
|
|
execute_bundler_script() |