|
| 1 | +import os |
| 2 | +import argparse |
| 3 | + |
| 4 | +# Define global constants |
| 5 | +# PROJECT_PATH = 'C:/Users/user/Documents/Python/MyProject' # Replace with the actual path to your project |
| 6 | +PROJECT_PATH = 'E:/my-nodejs-app' |
| 7 | + |
| 8 | +def simple_tree(dir_path): |
| 9 | + print(os.path.basename(dir_path) + '/') |
| 10 | + |
| 11 | + for item in os.listdir(dir_path): |
| 12 | + item_path = os.path.join(dir_path, item) |
| 13 | + if os.path.isdir(item_path): |
| 14 | + simple_tree(item_path) |
| 15 | + else: |
| 16 | + print(item) |
| 17 | + |
| 18 | +# Platform-Independent Path Handling for Absolute Path Tree |
| 19 | +def absolute_path_tree_pi(dir_path): |
| 20 | + print(dir_path) |
| 21 | + for item in os.listdir(dir_path): |
| 22 | + item_path = os.path.join(dir_path, item) |
| 23 | + if os.path.isdir(item_path): |
| 24 | + absolute_path_tree_pi(item_path) |
| 25 | + else: |
| 26 | + print(item_path) |
| 27 | + |
| 28 | +# User-Defined Path Formatting for Absolute Path Tree |
| 29 | +def absolute_path_tree(dir_path, path_style='auto'): |
| 30 | + if path_style == 'auto': |
| 31 | + path_style = os.path.sep # Use the default separator based on the platform |
| 32 | + |
| 33 | + print(dir_path.replace(os.path.sep, path_style)) |
| 34 | + for item in os.listdir(dir_path): |
| 35 | + item_path = os.path.normpath(os.path.join(dir_path, item)) |
| 36 | + if os.path.isdir(item_path): |
| 37 | + absolute_path_tree(item_path, path_style) |
| 38 | + else: |
| 39 | + print(item_path.replace(os.path.sep, path_style)) |
| 40 | + |
| 41 | +# Platform-Independent Path Handling for Relative Path Tree |
| 42 | +def relative_path_tree_pi(dir_path, relative_path=""): |
| 43 | + current_path = os.path.join(relative_path, os.path.basename(dir_path)) |
| 44 | + print(current_path) |
| 45 | + |
| 46 | + for item in os.listdir(dir_path): |
| 47 | + item_path = os.path.join(dir_path, item) |
| 48 | + if os.path.isdir(item_path): |
| 49 | + relative_path_tree_pi(item_path, current_path) |
| 50 | + else: |
| 51 | + print(os.path.join(current_path, item)) |
| 52 | + |
| 53 | +# User-Defined Path Formatting for Relative Path Tree |
| 54 | +def relative_path_tree(dir_path, relative_path="", path_style='auto'): |
| 55 | + if path_style == 'auto': |
| 56 | + path_style = os.path.sep # Use the default separator based on the platform |
| 57 | + |
| 58 | + current_path = os.path.join(relative_path, os.path.basename(dir_path)) |
| 59 | + print(current_path.replace(os.path.sep, path_style) + path_style) |
| 60 | + |
| 61 | + for item in os.listdir(dir_path): |
| 62 | + item_path = os.path.join(dir_path, item) |
| 63 | + if os.path.isdir(item_path): |
| 64 | + relative_path_tree(item_path, current_path, path_style) # Use current_path instead of os.path.join(current_path, item) |
| 65 | + else: |
| 66 | + print(os.path.join(current_path, item).replace(os.path.sep, path_style)) |
| 67 | + |
| 68 | +def directory_tree_structure(dir_path, indent=''): |
| 69 | + print(indent[:-1] + '+-- ' + os.path.basename(dir_path) + '/') |
| 70 | + indent += ' ' |
| 71 | + |
| 72 | + for item in os.listdir(dir_path): |
| 73 | + item_path = os.path.join(dir_path, item) |
| 74 | + if os.path.isdir(item_path): |
| 75 | + directory_tree_structure(item_path, indent) |
| 76 | + else: |
| 77 | + print(indent + '+-- ' + item) |
| 78 | + |
| 79 | +def beautify_tree_structure(dir_path, indent=''): |
| 80 | + print(f"{indent}- {os.path.basename(dir_path)}") |
| 81 | + |
| 82 | + for item in os.listdir(dir_path): |
| 83 | + item_path = os.path.join(dir_path, item) |
| 84 | + if os.path.isdir(item_path): |
| 85 | + beautify_tree_structure(item_path, indent + ' |') |
| 86 | + else: |
| 87 | + print(f"{indent} |- {item}") |
| 88 | + |
| 89 | +def main(): |
| 90 | + parser = argparse.ArgumentParser(description='Directory Tree Visualizer') |
| 91 | + |
| 92 | + # Specify the function to execute (simple, abs_pi, rel_pi, abs, rel, tree, beautify) |
| 93 | + parser.add_argument('function', choices=['simple', 'abs_pi', 'rel_pi', 'abs', 'rel', 'tree', 'beautify'], |
| 94 | + help='Function to execute. Choose one of the following: simple, abs_pi, rel_pi, abs, rel, tree, beautify.') |
| 95 | + |
| 96 | + # Specify the path parameter |
| 97 | + parser.add_argument('path', help='Path parameter. Enter the path to the target directory.') |
| 98 | + |
| 99 | + # Specify an optional argument for path style formatting |
| 100 | + parser.add_argument('--path_style', default='auto', |
| 101 | + help='Path style for formatting. Choose one of the following: auto, /, \\ (default: auto).\n' |
| 102 | + 'Examples:\n' |
| 103 | + ' - For Unix-like paths: --path_style /\n' |
| 104 | + ' - For Windows paths: --path_style \\\n' |
| 105 | + ' - Automatic detection based on the platform: --path_style auto') |
| 106 | + |
| 107 | + args = parser.parse_args() |
| 108 | + |
| 109 | + if not os.path.exists(args.path): |
| 110 | + print("Error: The specified path does not exist.") |
| 111 | + else: |
| 112 | + base_path = os.path.abspath(args.path) |
| 113 | + |
| 114 | + if args.function == 'simple': |
| 115 | + # Simple tree |
| 116 | + simple_tree(base_path) |
| 117 | + elif args.function == 'abs_pi': |
| 118 | + # Platform-Independent Path Handling for absolute path tree |
| 119 | + absolute_path_tree_pi(base_path) |
| 120 | + elif args.function == 'rel_pi': |
| 121 | + # Platform-Independent Path Handling for relative path tree |
| 122 | + relative_path_tree_pi(base_path) |
| 123 | + elif args.function == 'abs': |
| 124 | + # User-Defined Path Formatting for absolute path tree |
| 125 | + absolute_path_tree(base_path, args.path_style) |
| 126 | + elif args.function == 'rel': |
| 127 | + # User-Defined Path Formatting for relative path tree |
| 128 | + relative_path_tree(base_path, path_style=args.path_style) |
| 129 | + elif args.function == 'tree': |
| 130 | + # Directory Tree Structure |
| 131 | + directory_tree_structure(base_path) |
| 132 | + elif args.function == 'beautify': |
| 133 | + # Beautify Tree Structure |
| 134 | + beautify_tree_structure(base_path) |
| 135 | + |
| 136 | +if __name__ == '__main__': |
| 137 | + main() |
0 commit comments