Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kaustav202 authored and HarshCasper committed May 13, 2022
1 parent 5a769da commit a3fe607
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
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 = "|"
ELBOW = "└──"
TEE = "├──"
PIPE_PREFIX = "| "
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}")


0 comments on commit a3fe607

Please sign in to comment.