Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions DirectoryTree/README.md
Original file line number Diff line number Diff line change
@@ -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.
Empty file added DirectoryTree/dir_generator.py
Empty file.
5 changes: 5 additions & 0 deletions DirectoryTree/rptree/__init__py.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# __init__.py

"""Top-level package for RP Tree."""

__version__ = "0.1.0"
36 changes: 36 additions & 0 deletions DirectoryTree/rptree/cli.py
Original file line number Diff line number Diff line change
@@ -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()
69 changes: 69 additions & 0 deletions DirectoryTree/rptree/rptree.py
Original file line number Diff line number Diff line change
@@ -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}")


52 changes: 0 additions & 52 deletions DirectoryTree/show_dirs.py

This file was deleted.

10 changes: 10 additions & 0 deletions DirectoryTree/tree.py
Original file line number Diff line number Diff line change
@@ -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()