def _decorate_dir_entry(cls, entry) -> tuple: """ Decorates given entry for a directory. Decorate means that creates a colored representation of a name of the entry, grabs the date it was last modified and size in bytes and decorates. collects everything and returns as a list. """ # Gives orange color to the string & truncate to 32 chars current = [stylize("■ " + entry.name[:33] + "/", fg(202))] # Get date and convert in to a human readable format date = os.stat(entry).st_mtime current.append( DecoratedData(date, normalize_date('%h %d %Y %H:%M', date))) # recursively calculates the total size of a folder b = DirectoryFiles().get_dir_size(entry) current.append(DecoratedData(b, bytes_to_human_readable(b))) current.append('-') # add directory type identifier return tuple(current)
def _decorate_file_entry(cls, entry) -> tuple: """ Decorates given entry for a file. By decorate it means that creates a colored representation of a name of the entry, grabs the date it was last modified and size in bytes and decorates, determines file type. collects everything and returns as a list. """ # Gives yellow color to the string & truncate to 32 chars current = [stylize("» " + entry.name[:33], fg(226))] # Convert last modified time (which is in nanoseconds) date = os.stat(entry).st_mtime current.append( DecoratedData(date, normalize_date('%h %d %Y %H:%M', date))) b = os.stat(entry).st_size current.append(DecoratedData(b, bytes_to_human_readable(b))) # Evaluate the file type current.append(magic.from_file(entry.path, mime=True)) return tuple(current)
def get_files(path: str, hidden: bool = False, level: int = None) -> BinarySearchTree: """Collects all the files and returns as a BST""" bst = BinarySearchTree() for dirpath, dirname, filenames in os.walk(path): for f in filenames: entry = os.path.join(dirpath, f) if os.path.isfile(entry): if f.startswith('.') and not hidden: continue else: b = os.stat(entry).st_size file_node = DecoratedData(f, b) bst.add(file_node) return bst