-
Notifications
You must be signed in to change notification settings - Fork 490
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a769da
commit a3fe607
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | ||
|
||
|