Skip to content

Commit

Permalink
Added comments to the code
Browse files Browse the repository at this point in the history
  • Loading branch information
mana-sg committed Oct 31, 2023
1 parent 0b3b1ef commit 8354b0e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
7 changes: 7 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,30 @@
import sys

parser = ArgumentParser()
# Creates a file sort object which containes all the functions to sort
fos = FileSort()

# Creates basic sort argument
parser.add_argument('-b', '--basic',
help="Sorts your files into basic folders namely Photos, AudioVideo, Documents, CodingFiles, Folders, Others",
action="store_true",
)

# Creates deep sort argument
parser. add_argument('-d', '--deep',
help="Further sorts the folders into sub categories based on extensions",
action='store_true',
)

# args is a variable of type NameSpace which stores in all the arguments created
args: Namespace = parser.parse_args()

if args.deep:
# Calls the deep sorting algorithm
fos.deep_sort("Doing a deep sort for yout files...\n")

elif args.basic:
# Calls the basic sorting algorithm created by us
fos.basic_sort("Doing a basic sort for your files...\n")

else:
Expand Down
20 changes: 18 additions & 2 deletions modules/fos.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
import json
from progressBar import ProgressBar

# A class with all kinds of sorts


class FileSort:
def __init__(self):
'''Initialises the important parameters required to sort the files.'''

with open("extensions.json", "r") as data:
data = json.load(data)['EXTENSIONS']
self.IMAGE_EXT = data['IMAGE_EXT']
Expand All @@ -19,13 +23,19 @@ def __init__(self):
map(lambda x: f"{self.PATH}/{x}", self.ALL_FOLDERS_CREATED))

def createDirectory(self, folderName: str, file: str):
'''A function to create directories if not created yet and add it to the list of directories. This function
also moves the files to its respective directory after creating the directory if it doesn't already exist'''

if folderName not in self.files_in_directory:
os.mkdir(f"{self.PATH}/{folderName}")
self.files_in_directory.append(f"{folderName}")

os.rename(f"{self.PATH}/{file}", f"{self.PATH}/{folderName}/{file}")

def basic_sort(self, desc=''):
'''Basic sorting, sorts the files into documents, photos, audiovideo, codingfiles, folders and others based
on the extensions of the files.'''

for file in self.files_in_directory:
root, ext = os.path.splitext(f"{self.PATH}/{file}")

Expand All @@ -50,17 +60,23 @@ def basic_sort(self, desc=''):
else:
self.createDirectory(folderName="Others", file=file)

# Creates a progress bar using the class object ProgressBar and prints the final statement after sorting it.
pgBar = ProgressBar(desc=desc)
pgBar.createProgressBar()
print("Your files have been sorted!")

def deep_sort(self, desc=''):
'''This function does deep sorting. It calls the basic sorting algorithm and then continues to deep sort the documents
based on the extensions into PDF files, word files, ppt files, spreadsheet files, zip files and otehr files'''

self.basic_sort(desc=desc)
self.PATH = self.PATH + "/Documents"
self.PATH = self.PATH + "/Documents" # Adding documents to the path.
# Getting the directories in the documents directory that was created in basic sort
self.files_in_directory = os.listdir(self.PATH)
for file in self.files_in_directory:
root, ext = os.path.splitext(f"{self.PATH}/{file}")
_, ext = os.path.splitext(f"{self.PATH}/{file}")

# Creates folders and sorts the files into these.
if ext == '':
continue

Expand Down
5 changes: 4 additions & 1 deletion modules/progressBar.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from tqdm import tqdm
from time import sleep

# A class to create a terminal based progress bar using tqdm


class ProgressBar:
def __init__(self, desc):
'''Initialises the description to be displayed while the progress of the operation.'''
self.desc = desc

def createProgressBar(self):
from tqdm import tqdm
'''Creates a progress bar in the terminal to depict the progress of the files.'''
for _ in tqdm(range(100), desc=self.desc):
sleep(.01)

0 comments on commit 8354b0e

Please sign in to comment.