Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directory Tree Generator #1377

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions Python/Directory-Tree-Generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# file-system_structure-generator
5 changes: 5 additions & 0 deletions Python/Directory-Tree-Generator/dstree/__init__.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"
35 changes: 35 additions & 0 deletions Python/Directory-Tree-Generator/dstree/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pathlib
import argparse
import sys

from . import __version__
from .dstree import DirTree

def main():
args = parse_args()
root_dir = pathlib.Path(args.root_dir)
if not root_dir.is_dir():
print("Entered directory is invalid!! ")
sys.exit()
tree = DirTree(root_dir)
tree.generate()


def parse_args():
parser = argparse.ArgumentParser(
prog = "tree",
description = "DS Tree, a directory tree generator.",
epilog = "Thank you for using dstree!!"
)

parser.version = f"ds tree v{__version__}"
parser.add_argument("-v","--version", action = "version")
parser.add_argument(
"root_dir",
metavar = "ROOT_DIR",
nargs = "?",
default = ".",
help = "Generate directory tree starting at ROOT_DIR"
)
return parser.parse_args()

58 changes: 58 additions & 0 deletions Python/Directory-Tree-Generator/dstree/dstree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import os
import pathlib

PIPE = "|"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a unicode character which would be better suitable instead of a simple |, namely U+2502

ELBOW = "└──"
TEE = "├──"
PIPE_PREFIX = "| "
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

SPACE_PREFIX = " "

class DirTree:
def __init__(self,root_dir):
self._generator = _TreeGenerator(root_dir) #Composition

def generate(self):
tree = self._generator.build_tree()
for item in tree:
print(item)


class _TreeGenerator():
def __init__(self,root_dir):
self._tree = []
self._root_dir = pathlib.Path(root_dir)

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 x : x.is_file())
entry_count = len(entries)
for index,entry in enumerate(entries):
connector = ELBOW if index == entry_count-1 else TEE
if entry.is_dir():
self._add_directory(entry,index,entry_count,prefix,connector)
else:
self._add_file(entry,prefix,connector)


def _add_directory(self,directory,index,entry_count,prefix,connector):
self._tree.append(f"{prefix}{connector}{directory.name}{os.sep}")
if entry_count != index - 1:
prefix += PIPE_PREFIX
else:
prefix += SPACE_PREFIX
self._tree_body(directory=directory , prefix=prefix) #overwrite default prefix
self._tree.append(prefix.rstrip())

def _add_file(self,file,prefix,connector):
self._tree.append(f"{prefix}{connector}{file.name}")


1 change: 1 addition & 0 deletions Python/Directory-Tree-Generator/dstree/temp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

5 changes: 5 additions & 0 deletions Python/Directory-Tree-Generator/tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from dstree.cli import main

if __name__ == "__main__":
main()