Skip to content

Commit

Permalink
Fix some bugs on 'ls' command, create the base of 'mkdir' command and…
Browse files Browse the repository at this point in the history
… fix the input parser
  • Loading branch information
thepabloaguilar committed Oct 7, 2018
1 parent f05055f commit 0a74802
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ wheels/
.installed.cfg
*.egg
MANIFEST
*.pyc

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down
3 changes: 2 additions & 1 deletion commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
from cp import cp
from cat import cat
from clear import clear
from grep import grep
from grep import grep
from mkdir import mkdir
Binary file removed commands/__init__.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions commands/exit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
class exit():

def execute(self, cls):
print("Exiting...\nBye!")
sys.exit(0)
print("Exiting...\nBye!")
sys.exit(0)
Binary file removed commands/history.pyc
Binary file not shown.
59 changes: 33 additions & 26 deletions commands/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@

class ls():
def __init__(self):
self.files = []
self.show_hidden = False
self.option_dictionary = self.get_option_dictionary()
# print "in self"

def get_files_from_current_path(self):
self.files = FileDetailsHelper.get_files_from_path('.')
def __get_files_from_current_path(self):
return FileDetailsHelper.get_files_from_path('.')

def __get_files_from_path(self, path):
return FileDetailsHelper.get_files_from_path(path)

def get_files(self, *args):
if args:
return self.__get_files_from_path(*args)
return self.__get_files_from_current_path()

def get_option_dictionary(self):
option_dictionary = {
Expand All @@ -25,18 +31,17 @@ def get_option_dictionary(self):

return option_dictionary

def get_complete_details(self, show_hidden_files=True):
def get_complete_details(self, show_hidden_files=True, *args):
detailed_files = []
self.get_files_from_current_path()
files = self.files
files = self.get_files(args)
if not show_hidden_files:
files = FileDetailsHelper.filter_hidden_files(files)

for filename in files:
try:
lstat_info = os.lstat(filename)
except:
print "No such file " + filename
print("No such file {}".format(filename))
continue

perms, color, link = FileDetailsHelper.get_mode_info(lstat_info.st_mode)
Expand All @@ -52,31 +57,30 @@ def get_complete_details(self, show_hidden_files=True):
detailed_files = sorted(detailed_files, key=lambda x: datetime.datetime.strptime(x[5], '%b %d %Y'))
for info_tuple in reversed(detailed_files):
for info in info_tuple:
print info,
print
print(info)
print()

def get_only_visible_files(self):
self.get_files_from_current_path()
files = FileDetailsHelper.filter_hidden_files(self.files)
def get_only_visible_files(self, *args):
files = self.get_files(*args)
files = FileDetailsHelper.filter_hidden_files(files)
self.print_to_screen(files)

def get_files_in_revrese_order(self):
self.get_files_from_current_path()
files = FileDetailsHelper.reverse_file_ordering(self.files)
def get_files_in_revrese_order(self, *args):
files = self.get_files(*args)
files = FileDetailsHelper.reverse_file_ordering(files)
self.print_to_screen(files)

def print_to_screen(self, files):
for filename in files:
print filename,
print
print(filename)

def get_all_files(self):
self.get_files_from_current_path()
self.print_to_screen(self.files)
def get_all_files(self, *args):
files = self.get_files(*args)
self.print_to_screen(files)

def get_folders_with_backslash(self):
self.get_files_from_current_path()
files = FileDetailsHelper.filter_hidden_files(self.files)
def get_folders_with_backslash(self, *args):
files = self.get_files(*args)
files = FileDetailsHelper.filter_hidden_files(files)
for i, filename in enumerate(files):

if FileDetailsHelper.is_a_folder(os.lstat(filename).st_mode):
Expand All @@ -95,5 +99,8 @@ def execute(self, cls):
option = 'basic'

methods = self.option_dictionary
method_to_execute = getattr(ls, methods[option])
method_to_execute(self)
if option in methods.keys():
method_to_execute = getattr(ls, methods[option])
method_to_execute(self, *cls.data)
else:
print('Invalid option')
Binary file removed commands/ls.pyc
Binary file not shown.
15 changes: 15 additions & 0 deletions commands/mkdir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os

class mkdir():
def __create_folder(self, path):
try:
os.mkdir(path, )
except OSError:
print('The folder already exist')

def execute(self, terminal):
path_to_create = terminal.data
if path_to_create:
self.__create_folder(path_to_create[0])
else:
print('Syntax error')
Binary file removed helpers/__init__.pyc
Binary file not shown.
5 changes: 2 additions & 3 deletions helpers/file_details_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ def get_files_from_path(path):
try:
files = os.listdir(path)
except:
print "Invalid path"
return

print("Invalid path")
return False
return files

@staticmethod
Expand Down
Binary file removed helpers/file_details_helper.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion index.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_command():


if __name__ == "__main__":
print "Welcome to pylinux"
print("Welcome to pylinux")

readline.set_completer(completer)
readline.parse_and_bind("tab: complete")
Expand Down
11 changes: 8 additions & 3 deletions terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_command(self):
show_path = show_path.split('/')
show_path = show_path[-2:]
show_path = "/".join(show_path)
command = raw_input(show_path+'$ ')
command = raw_input('{}$ '.format(show_path))
return command

def play(self):
Expand All @@ -42,9 +42,14 @@ def play(self):
log.info(self.command)
self.command, self.data, self.options = InputParser.parse_input(self.command)
executer = getattr(commands, self.command)

except KeyboardInterrupt:
print("Ctrl+C")
executer = getattr(commands, 'exit')
except EOFError:
print("Ctrl+D")
executer = getattr(commands, 'exit')
except:
print ("The command is either invalid or not supported yet")
print("The command is either invalid or not supported yet")
executer().execute(self)
self.set_last_command()
self.push_to_history()
Binary file removed terminal.pyc
Binary file not shown.
Binary file removed utils/__init__.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion utils/input_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ def parse_input(command):
data.append(option)
else:
main_command = command
return main_command, data, split_commands
return main_command, data, options
Binary file removed utils/input_parser.pyc
Binary file not shown.

0 comments on commit 0a74802

Please sign in to comment.