Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 49 additions & 42 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# step 1
# Prepare command parser: identify command and arguments
#directory/file related commands must be implemented as a separate module
#Thus, to add a new command into the shell, create a python module with the same name as
# the command in a specific folder.
# directory/file related commands must be implemented as a separate module
# Thus, to add a new command into the shell, create a python module with the same name as
# the command in a specific folder.
# This module should have run() function that would contain the command implementation.
# step 2
# log commands in history data base
Expand All @@ -15,78 +15,85 @@

import os
import sys
from modules import file_operations,dir_operations,misc
from modules import file_operations, dir_operations, misc
from utils import *

base_path = os.getcwd()
debug = 1

def print_command(cmd,opts,args):
print("command : ",cmd)

def print_command(cmd, opts, args):
print("command : ", cmd)
print("Options : ", opts)
print("Arguments : ",args)
print("Arguments : ", args)


def py():
print("pyshell>>", end=" ")
print("pyshell>>", end=" ")


def imports():
module_path = os.path.join(base_path,'modules')
modules = [f[:-3] for f in os.listdir(module_path) if os.path.isfile(os.path.join(module_path,f))]
module_path = os.path.join(base_path, 'modules')
modules = [f[:-3] for f in os.listdir(module_path) if os.path.isfile(os.path.join(module_path, f))]
return modules


def str_to_class(str):
return getattr(sys.modules[__name__],str)
return getattr(sys.modules[__name__], str)


def get_function(cmd):
modules = imports()
for module in modules:
if(debug):
print("Searching in :",module)
try:
function = getattr(str_to_class(module),cmd)
if(debug):
print("Found function : ",function," in :",module)
return function
break
except AttributeError:
pass

def run_function(function,opts,args):
if debug:
print("Searching in :", module)
try:
function = getattr(str_to_class(module), cmd)
if debug:
print("Found function : ", function, " in :", module)
return function
break
except AttributeError:
pass


def run_function(function, opts, args):
try:
if(debug):
print("Running : ",function)
function(opts,args)
del(function)
if debug:
print("Running : ", function)

function(opts, args)
del function
return True
except TypeError:
print("Command not found")
return


if __name__ == "__main__":
#CORE EVENT LOOP
# CORE EVENT LOOP
version = "v0.001"
hf_path,count = initalize_history()
#print(modules)
print("Welcome to pyshell",version)
hf_path, count = initalize_history()
# print(modules)
print("Welcome to pyshell", version)
try:
while(1):
while 1:
py()
ori_cmd = input()
if(not len(ori_cmd)):
if not len(ori_cmd):
continue
opts,args = [],[]
opts, args = [], []

cmd, opts, args = pre_process_cmd(ori_cmd)
if(debug):
print_command(cmd,opts,args)

if debug:
print_command(cmd, opts, args)

function = get_function(cmd)

log_it = run_function(function,opts,args)
log_it = run_function(function, opts, args)
if log_it:
count = log_cmd(ori_cmd,hf_path,count)
except (KeyboardInterrupt,EOFError):
print("\nThanks for using pyshell",version)
print("Closing...")
count = log_cmd(ori_cmd, hf_path, count)
except (KeyboardInterrupt, EOFError):
print("\nThanks for using pyshell", version)
print("Closing...")
73 changes: 38 additions & 35 deletions modules/dir_operations.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,73 @@
'''
"""
ls (with support for -R or --recursive switch for listing contents of directory recursively)
mkdir
pwd
rmdir
cd
'''
"""
import os
import shutil
from utils import inv_opt


def ls(opts,args):
def ls(opts, args):
for opt in opts:
if not ((opt == 'R') or (opt == 'recursive')):
inv_opt(ls.__name__,opt)
inv_opt(ls.__name__, opt)
return

for arg in args:
path = os.path.join(os.getcwd(),str(arg))
path = os.path.join(os.getcwd(), str(arg))
if not (os.path.isfile(path) or os.path.isdir(path)):
print("ls: cannot access \'",arg,"\': No such file or directory")
print("ls: cannot access \'", arg, "\': No such file or directory")
return
if ('R' in opts) or ('recursive' in opts):
queue = []
queue.append(arg)
while( len(queue) > 0):
while len(queue) > 0:
parent = queue.pop(0)
print(('.'+parent+":\n"))
files = [f for f in os.listdir(os.path.join(os.getcwd(),parent)) if f[0] != '.']
print(('.' + parent + ":\n"))
files = [f for f in os.listdir(os.path.join(os.getcwd(), parent)) if f[0] != '.']
for file in files:
if os.path.isdir(os.path.join(parent,file)):
queue.append(os.path.join(parent,file))
print(file," ",end ="")
if os.path.isdir(os.path.join(parent, file)):
queue.append(os.path.join(parent, file))
print(file, " ", end="")
print('\n')
else:
if(len(args) > 1):
print(arg," :\n")
files = [f for f in os.listdir(os.path.join(os.getcwd(),arg)) if f[0] != '.']
if len(args) > 1:
print(arg, " :\n")
files = [f for f in os.listdir(os.path.join(os.getcwd(), arg)) if f[0] != '.']
files.sort()
for file in files:
print(file," ",end ="")
print(file, " ", end="")
print('\n')

def cd(opts,args):
if(len(args)>1):
print(cd.__name__,": too many arguments")

def cd(opts, args):
if len(args) > 1:
print(cd.__name__, ": too many arguments")
return

if( len(opts)>0 ):
inv_opt(cd.__name__,opts[0])
if len(opts) > 0:
inv_opt(cd.__name__, opts[0])
return

if os.path.isdir(str(args[0])):
os.chdir(str(args[0]))
else:
print(cd.__name__,": ",args[0],": No such file or directory")
print(cd.__name__, ": ", args[0], ": No such file or directory")


def pwd(opts,args):
if( len(opts)>0 ):
inv_opt(pwd.__name__,opts[0])
def pwd(opts, args):
if len(opts) > 0:
inv_opt(pwd.__name__, opts[0])
return
print(os.getcwd())

def mkdir(opts,args):
if( len(opts)>0 ):
inv_opt(mkdir.__name__,opts[0])

def mkdir(opts, args):
if len(opts) > 0:
inv_opt(mkdir.__name__, opts[0])
return

rnw = ""
Expand All @@ -74,24 +76,25 @@ def mkdir(opts,args):
rnw = str(arg)
os.mkdir(rnw)
except FileExistsError:
print('\'',rnw,'\'',"already exists")
print('\'', rnw, '\'', "already exists")

def rmdir(opts,args):

def rmdir(opts, args):
rnw = "str"
try:
for arg in args:
rnw = str(arg)
os.rmdir(rnw)

except OSError:
if( len(opts) == 0 or ('r' not in opts)):
if len(opts) == 0 or ('r' not in opts):
print("Directory not empty")
return

for opt in opts:
if opt != 'r':
inv_opt(rmdir.__name__,opt)
inv_opt(rmdir.__name__, opt)
return
for arg in args:
rnw = str(arg)
shutil.rmtree(rnw)
shutil.rmtree(rnw)
Loading