From 69849f580c0f359b7c81bfca0607cc5d026e1866 Mon Sep 17 00:00:00 2001 From: kaustav202 <89788120+kaustav202@users.noreply.github.com> Date: Wed, 20 Apr 2022 20:09:04 +0530 Subject: [PATCH 1/4] Create README.md --- Python/Directory-Tree-Generator/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Python/Directory-Tree-Generator/README.md diff --git a/Python/Directory-Tree-Generator/README.md b/Python/Directory-Tree-Generator/README.md new file mode 100644 index 000000000..c9723bc05 --- /dev/null +++ b/Python/Directory-Tree-Generator/README.md @@ -0,0 +1 @@ +## Directory Tree Generator From f60a65c6da9c9f170378f3efc6e2d7d651518cc8 Mon Sep 17 00:00:00 2001 From: kaustav202 <89788120+kaustav202@users.noreply.github.com> Date: Wed, 20 Apr 2022 20:10:18 +0530 Subject: [PATCH 2/4] Add files via upload --- Python/Directory-Tree-Generator/README.md | 2 +- Python/Directory-Tree-Generator/tree.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 Python/Directory-Tree-Generator/tree.py diff --git a/Python/Directory-Tree-Generator/README.md b/Python/Directory-Tree-Generator/README.md index c9723bc05..b525c6493 100644 --- a/Python/Directory-Tree-Generator/README.md +++ b/Python/Directory-Tree-Generator/README.md @@ -1 +1 @@ -## Directory Tree Generator +# file-system_structure-generator diff --git a/Python/Directory-Tree-Generator/tree.py b/Python/Directory-Tree-Generator/tree.py new file mode 100644 index 000000000..5a02f3d40 --- /dev/null +++ b/Python/Directory-Tree-Generator/tree.py @@ -0,0 +1,5 @@ +from dstree.cli import main + +if __name__ == "__main__": + main() + From 5a769da87edd2285604e616997a729d8f2b58ed4 Mon Sep 17 00:00:00 2001 From: kaustav202 <89788120+kaustav202@users.noreply.github.com> Date: Wed, 20 Apr 2022 20:11:29 +0530 Subject: [PATCH 3/4] Create temp.txt --- Python/Directory-Tree-Generator/dstree/temp.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Python/Directory-Tree-Generator/dstree/temp.txt diff --git a/Python/Directory-Tree-Generator/dstree/temp.txt b/Python/Directory-Tree-Generator/dstree/temp.txt new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/Python/Directory-Tree-Generator/dstree/temp.txt @@ -0,0 +1 @@ + From a3fe607d666c62a9048204479cd5c7b210d56477 Mon Sep 17 00:00:00 2001 From: kaustav202 <89788120+kaustav202@users.noreply.github.com> Date: Wed, 20 Apr 2022 20:12:30 +0530 Subject: [PATCH 4/4] Add files via upload --- .../dstree/__init__.py | 5 ++ Python/Directory-Tree-Generator/dstree/cli.py | 35 +++++++++++ .../Directory-Tree-Generator/dstree/dstree.py | 58 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 Python/Directory-Tree-Generator/dstree/__init__.py create mode 100644 Python/Directory-Tree-Generator/dstree/cli.py create mode 100644 Python/Directory-Tree-Generator/dstree/dstree.py diff --git a/Python/Directory-Tree-Generator/dstree/__init__.py b/Python/Directory-Tree-Generator/dstree/__init__.py new file mode 100644 index 000000000..3ad9b4714 --- /dev/null +++ b/Python/Directory-Tree-Generator/dstree/__init__.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/Python/Directory-Tree-Generator/dstree/cli.py b/Python/Directory-Tree-Generator/dstree/cli.py new file mode 100644 index 000000000..b3b39aba2 --- /dev/null +++ b/Python/Directory-Tree-Generator/dstree/cli.py @@ -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() + \ No newline at end of file diff --git a/Python/Directory-Tree-Generator/dstree/dstree.py b/Python/Directory-Tree-Generator/dstree/dstree.py new file mode 100644 index 000000000..7f86f79cb --- /dev/null +++ b/Python/Directory-Tree-Generator/dstree/dstree.py @@ -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}") + + \ No newline at end of file