diff --git a/DirectoryTree/README.md b/DirectoryTree/README.md new file mode 100644 index 0000000..f750eaa --- /dev/null +++ b/DirectoryTree/README.md @@ -0,0 +1,14 @@ + + +- tree.py provides an entry-point script for you to run the application. + +- Then you have the rptree/ directory that holds a Python package with three modules: + +rptree.py provides the application’s main functionalities. +__init__.py enables rptree/ as a Python package. +cli.py provides the command-line interface for the application. + +- Your directory tree generator tool will run on the command line. It’ll take arguments, process them, and display a directory tree diagram on the terminal window. It can also save the output diagram to a file in markdown format. + +- To run the first step, you need to provide a way for your application to take a directory path at the command line. To do this, you’ll use Python’s argparse module from the standard library. + - To complete the second and third steps, you’ll use pathlib. This module provides several tools to manage and represent file system paths. Finally, you’ll use a regular Python list to store the list of entries in the directory structure. diff --git a/DirectoryTree/dir_generator.py b/DirectoryTree/dir_generator.py new file mode 100644 index 0000000..e69de29 diff --git a/DirectoryTree/rptree/__init__py.py b/DirectoryTree/rptree/__init__py.py new file mode 100644 index 0000000..0abaf00 --- /dev/null +++ b/DirectoryTree/rptree/__init__py.py @@ -0,0 +1,5 @@ +# __init__.py + +"""Top-level package for RP Tree.""" + +__version__ = "0.1.0" \ No newline at end of file diff --git a/DirectoryTree/rptree/cli.py b/DirectoryTree/rptree/cli.py new file mode 100644 index 0000000..6532520 --- /dev/null +++ b/DirectoryTree/rptree/cli.py @@ -0,0 +1,36 @@ +"""This module provides the RP Tree CLI.""" +# cli.py + +import argparse +import pathlib +import sys + +from . import __version__ +from .rptree import DirectoryTree + +def main(): + args = parse_cmd_line_arguments() + root_dir = pathlib.Path(args.root_dir) + if not root_dir.is_dir(): + print("The specified root directory doesn't exist") + sys.exit() + tree = DirectoryTree(root_dir) + tree.generate() + + +def parse_cmd_line_arguments(): + parser = argparse.ArgumentParser( + prog="tree", + description="RP Tree, a directory tree generator", + epilog="Thanks for using RP Tree!", + ) + parser.version = f"RP Tree v{__version__}" + parser.add_argument("-v", "--version", action="version") + parser.add_argument( + "root_dir", + metavar="ROOT_DIR", + nargs="?", + default=".", + help="Generate a full directory tree starting at ROOT_DIR", + ) + return parser.parse_args() \ No newline at end of file diff --git a/DirectoryTree/rptree/rptree.py b/DirectoryTree/rptree/rptree.py new file mode 100644 index 0000000..4ccd738 --- /dev/null +++ b/DirectoryTree/rptree/rptree.py @@ -0,0 +1,69 @@ +# rptree.py + +"""This module provides RP Tree main module.""" + +import os +import pathlib + +PIPE = "│" +ELBOW = "└──" +TEE = "├──" +PIPE_PREFIX = "│ " +SPACE_PREFIX = " " + + +class DirectoryTree: + def __init__(self, root_dir): + self._generator = _TreeGenerator(root_dir) + + def generate(self): + tree = self._generator.build_tree() + for entry in tree: + print(entry) + + +class _TreeGenerator: + def __init__(self, root_dir): + self._root_dir = pathlib.Path(root_dir) + self._tree = [] + + def build_tree(self): + self._tree_head() + self._tree_body(self._root_dir) + return self._tree + + def _tree_head(self): + self._tree.append(f"{self._root_dir}{os.sep}") + self._tree.append(PIPE) + + def _tree_body(self, directory, prefix=""): + entries = directory.iterdir() + entries = sorted(entries, key=lambda entry: entry.is_file()) + entries_count = len(entries) + for index, entry in enumerate(entries): + connector = ELBOW if index == entries_count - 1 else TEE + if entry.is_dir(): + self._add_directory( + entry, index, entries_count, prefix, connector + ) + else: + self._add_file(entry, prefix, connector) + + def _add_directory( + self, directory, index, entries_count, prefix, connector + ): + self._tree.append(f"{prefix}{connector} {directory.name}{os.sep}") + if index != entries_count - 1: + prefix += PIPE_PREFIX + else: + prefix += SPACE_PREFIX + self._tree_body( + directory=directory, + prefix=prefix, + ) + self._tree.append(prefix.rstrip()) + + def _add_file(self, file, prefix, connector): + self._tree.append(f"{prefix}{connector} {file.name}") + + diff --git a/DirectoryTree/show_dirs.py b/DirectoryTree/show_dirs.py deleted file mode 100644 index abb73c6..0000000 --- a/DirectoryTree/show_dirs.py +++ /dev/null @@ -1,52 +0,0 @@ - -import os -import sys - -path = os.getcwd() - -i = 0 -folders = {} -main_folder = "" - -for subdir, dirs, files in os.walk(path): - dir_name = str(subdir).split("/") - dir_name = dir_name[len(dir_name)-1] - - if len(main_folder) < 1: - main_folder = dir_name - folders_and_files = [] - - for dir_ in dirs: - folders_and_files.append(dir_) - for file_ in files: - folders_and_files.append(file_) - - folders[dir_name] = folders_and_files - -final_list = [] - -def recursive_tree(folder, num): - files = "" - num = num + "-" - try: - files = folders[folder] - return go_through_list(files, len(files)-1, num) - except: - Exception() - -def go_through_list(list_, index, num): - if len(num) == 2: - final_list.append(str(list_[index])) - else: - final_list.append(num + " " + str(list_[index])) - if index == 0: - recursive_tree(list_[index], num) - return - else: - recursive_tree(list_[index], num) - return go_through_list(list_, index - 1, num) - -recursive_tree(main_folder, " ") - -for item in final_list: - print(item) diff --git a/DirectoryTree/tree.py b/DirectoryTree/tree.py new file mode 100644 index 0000000..26e23ad --- /dev/null +++ b/DirectoryTree/tree.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python3 +# tree.py + +"""This module provides RP Tree entry point script.""" + +from rptree.cli import main + +if __name__ == "__main__": + main() +