Created
August 30, 2020 14:16
-
-
Save search5/3157e1aa36eb60546cfc19bc1248e6af to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from pathlib import Path | |
class DisplayablePath(object): | |
display_filename_prefix_middle = '├──' | |
display_filename_prefix_last = '└──' | |
display_parent_prefix_middle = ' ' | |
display_parent_prefix_last = '│ ' | |
def __init__(self, path, parent_path, is_last): | |
self.path = Path(str(path)) | |
self.parent = parent_path | |
self.is_last = is_last | |
if self.parent: | |
self.depth = self.parent.depth + 1 | |
else: | |
self.depth = 0 | |
@property | |
def displayname(self): | |
if self.path.is_dir(): | |
return self.path.name + '/' | |
return self.path.name | |
@classmethod | |
def make_tree(cls, root, parent=None, is_last=False, criteria=None): | |
root = Path(str(root)) | |
criteria = criteria or cls._default_criteria | |
displayable_root = cls(root, parent, is_last) | |
yield displayable_root | |
children = sorted(list(path | |
for path in root.iterdir() | |
if criteria(path)), | |
key=lambda s: str(s).lower()) | |
count = 1 | |
for path in children: | |
is_last = count == len(children) | |
if path.is_dir(): | |
yield from cls.make_tree(path, | |
parent=displayable_root, | |
is_last=is_last, | |
criteria=criteria) | |
else: | |
yield cls(path, displayable_root, is_last) | |
count += 1 | |
@classmethod | |
def _default_criteria(cls, path): | |
return True | |
@property | |
def displayname(self): | |
if self.path.is_dir(): | |
return self.path.name + '/' | |
return self.path.name | |
def displayable(self): | |
if self.parent is None: | |
return self.displayname | |
_filename_prefix = (self.display_filename_prefix_last | |
if self.is_last | |
else self.display_filename_prefix_middle) | |
parts = ['{!s} {!s}'.format(_filename_prefix, | |
self.displayname)] | |
parent = self.parent | |
while parent and parent.parent is not None: | |
parts.append(self.display_parent_prefix_middle | |
if parent.is_last | |
else self.display_parent_prefix_last) | |
parent = parent.parent | |
return ''.join(reversed(parts)) | |
if __name__ == "__main__": | |
paths = DisplayablePath.make_tree(Path('player_src')) | |
for path in paths: | |
print(path.displayable()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment