-
Notifications
You must be signed in to change notification settings - Fork 490
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
kaustav202
wants to merge
4
commits into
HarshCasper:master
Choose a base branch
from
kaustav202:dir_tree
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Directory Tree Generator #1377
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ | ||
# file-system_structure-generator |
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 = "| " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}") | ||
|
||
|
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 @@ | ||
|
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 @@ | ||
from dstree.cli import main | ||
|
||
if __name__ == "__main__": | ||
main() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
|
, namelyU+2502