Add a directory tree visualizer (#243)

This commit is contained in:
Harpreet Singh Saluja 2021-10-28 11:26:23 +05:30 committed by GitHub
parent 592c5a1d26
commit f364b4719e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 190 additions and 0 deletions

View File

@ -0,0 +1,116 @@
# Directory Tree Visualizer
This project is useful for visualizing the relationship between files and directories and making their positioning easy to comprehend.
## Libraries Used
* Docopt
* Argparse
* os
* walkdir
## Usage
Directory Tree Generator depends on third party libraries and you will first need to install the application's dependencies:
```bash
pip install walkdir
```
## Run Locally
Clone the project
```bash
git clone https://github.com/hastagAB/Awesome-Python-Scripts.git
```
Go to the project directory
```bash
cd Awesome-Python-Scripts\Directory_Tree_Generator
```
Run ```directory_tree_generator.py```. You will have to provide the absolute path of the directory you want to visualize
```python
python directory_tree_generator.py "path\to\directory"
```
A full visualizer would be displayed along with the levels.
```bash
1 - D:\\Cheatsheet Template
2 - CPP
3 - Beginnig Level Programs
4 - index.html
4 - prism.css
4 - prism.js
4 - style.css
3 - New Category 2
4 - index.html
4 - prism.css
4 - prism.js
4 - style.css
3 - New Category 3
4 - index.html
4 - prism.css
4 - prism.js
4 - style.css
3 - New Category4
4 - index.html
4 - prism.css
4 - prism.js
4 - style.css
2 - Python
3 - Arrays
4 - index.html
4 - prism.css
4 - prism.js
4 - style.css
3 - Easy String prog
4 - index.html
4 - prism.css
4 - prism.js
4 - style.css
3 - File Handling Programs
4 - index.html
4 - prism.css
4 - prism.js
4 - style.css
3 - Good Programs to Practise
4 - index.html
4 - prism.css
4 - prism.js
4 - style.css
3 - Miscellaneous
4 - index.html
4 - prism.css
4 - prism.js
4 - style.css
3 - New Category 3 (another copy)
4 - index.html
4 - prism.css
4 - prism.js
4 - style.css
3 - New Category 3 (copy)
4 - index.html
4 - prism.css
4 - prism.js
4 - style.css
3 - Regex Programs
4 - index.html
4 - prism.css
4 - prism.js
3 - Searching Algorithm
4 - index.html
4 - prism.css
4 - prism.js
4 - style.css
3 - Sorting Algortihm
4 - index.html
4 - prism.css
4 - prism.js
4 - style.css
```

View File

@ -0,0 +1,72 @@
""" A Python directory tree generator tool for your command line.
The application will take a directory path as an argument at the
command line and display a directory tree diagram on your screen. """
import argparse
import os
from walkdir import filtered_walk
parser = argparse.ArgumentParser(
description='Print the directory-tree code for the LaTeX dirtree package.')
parser.add_argument(dest='path', type=str, help="Root directory of the tree")
parser.add_argument('-d', '--maxDepth', dest='maxDepth',
type=int, help="Max depth for tree expansion")
parser.add_argument('-H', '--includeHidden', dest='includeHidden',
action='store_true', help='Include hidden files')
parser.add_argument('-S', '--includeSystem', dest='includeSystem',
action='store_true', help='Include system files')
system_file_names = [".DS_Store"]
def delete_trailing_slash(path_name):
""" Delete trailing slash which might otherwise lead to errors """
while path_name.endswith('/'):
path_name = path_name[:-1]
return path_name
def get_relative_depth(dir_path, level_offset):
""" Count how many levels deep is the directory with respect to dirRoot """
return dir_path.count(os.path.sep) - level_offset
def escape_illegal(name):
""" Escape illegal symbols for LaTeX """
illegal_char_array = ['\\', '&', '%', '$', '#', '_', '{', '}', '~', '^']
for char in illegal_char_array:
name = name.replace(char, "\\" + char)
return name
rootDir = delete_trailing_slash(parser.parse_args().path)
includeHidden = parser.parse_args().includeHidden
includeSystem = parser.parse_args().includeSystem
maxDepth = parser.parse_args().maxDepth
# Check if the directory exists
if os.path.isdir(rootDir) and os.path.exists(rootDir):
INDENT_CHAR = " "
# Depth of the root (i.e. number of "/")
levelOffset = rootDir.count(os.path.sep) - 1
# Create filter
excluded_filter = []
if not includeHidden:
excluded_filter.append(".*")
if not includeSystem:
excluded_filter += system_file_names
for dirName, subdirList, fileList in sorted(filtered_walk(
rootDir, depth=maxDepth, excluded_dirs=excluded_filter,
excluded_files=excluded_filter)):
level = get_relative_depth(dirName, levelOffset)
baseName = os.path.basename(dirName)
if level == 1: # for the first level only print the whole path
print(INDENT_CHAR + str(level) + " - " + escape_illegal(dirName))
else:
print((INDENT_CHAR * level + str(level) +
" - " + escape_illegal((os.path.basename(dirName)))))
level += 1
for fileName in sorted(fileList):
print(INDENT_CHAR * level + str(level) + " - "
+ escape_illegal(fileName))
else:
print("Error: root directory not found")

View File

@ -0,0 +1 @@
walkdir==0.4.1

View File

@ -185,6 +185,7 @@ So far, the following projects have been integrated to this repo:
|[csv_to_json](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/csv_to_json)|[MoiZ](https://github.com/TechBoyy6)| |[csv_to_json](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/csv_to_json)|[MoiZ](https://github.com/TechBoyy6)|
|[Battery_notification](https://github.com/hastagABAwesome-Python-Scripts/Battery_notification/)|[Krishna Sharma](https://github.com/krishnasharma1386)| |[Battery_notification](https://github.com/hastagABAwesome-Python-Scripts/Battery_notification/)|[Krishna Sharma](https://github.com/krishnasharma1386)|
|[Steg_Tool](https://github.com/hastagABAwesome-Python-Scripts/Steg_Tool/)|[Shankar JP](https://github.com/shankarjp)| |[Steg_Tool](https://github.com/hastagABAwesome-Python-Scripts/Steg_Tool/)|[Shankar JP](https://github.com/shankarjp)|
| [Directory Tree Visualizer](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Directory_Tree_Generator) | [Harpreet Singh Saluja](https://github.com/hssaluja25/) |
|[Pressure_Converter](https://github.com/E-wave112/Awesome-Python-Scripts/tree/master/Pressure_Converter)|[E-Wave](https://github.com/E-wave112)| |[Pressure_Converter](https://github.com/E-wave112/Awesome-Python-Scripts/tree/master/Pressure_Converter)|[E-Wave](https://github.com/E-wave112)|
## How to use : ## How to use :