-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpysh_builtins.py
137 lines (109 loc) · 4.25 KB
/
pysh_builtins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#: pysh_builtins.py
"""This class is built to run built-in command shell"""
import os
import sys
from abc import ABC, abstractmethod
# pylint: disable=too-few-public-methods
class BuiltinStrategy(ABC):
"""Abstract base class for built in commands"""
@abstractmethod
def run(self, arguments):
"""abstract run method"""
class CdBuiltin(BuiltinStrategy):
"""CdBuiltin: change the current working directory"""
def run(self, arguments):
"""run cd command"""
if len(arguments) == 2:
os.chdir(arguments[1])
return 0
return 1
class EchoBuiltin(BuiltinStrategy):
"""EchoBuiltin: print a message to the terminal"""
def run(self, arguments):
"""run echo command"""
print(' '.join(arguments[1:]))
class AliasBuiltin(BuiltinStrategy):
"""AliasBuiltin: create a shortcut for a longer command"""
def run(self, arguments):
"""run alias command"""
raise NotImplementedError('alias')
class HistoryBuiltin(BuiltinStrategy):
"""HistoryBuiltin: display a list of previously executed commands"""
def run(self, arguments):
"""run history command"""
raise NotImplementedError('history')
class ExitBuiltin:
"""ExitBuiltin: exit the current shell"""
def run(self, arguments):
"""run exit command"""
if len(arguments) > 1:
exit_value = int(arguments[1])
else:
exit_value = 0
sys.exit(exit_value)
class ExportBuiltin(BuiltinStrategy):
"""ExportBuiltin: set an environment variable"""
def run(self, arguments):
"""run export command"""
raise NotImplementedError('export')
class UnsetBuiltin(BuiltinStrategy):
"""UnsetBuiltin: remove an environment variable"""
def run(self, arguments):
"""run unset command"""
raise NotImplementedError('unset')
class SourceBuiltin(BuiltinStrategy):
"""SourceBuiltin: execute commands from a file in the current shell"""
def run(self, arguments):
"""run source command"""
raise NotImplementedError('source')
class PwdBuiltin(BuiltinStrategy):
"""PwdBuiltin: print the current working directory"""
def run(self, arguments):
"""run pwd command"""
print(os.getcwd())
class TypeBuiltin(BuiltinStrategy):
"""determine whether a command is a shell built-in or an external executable"""
def run(self, arguments):
"""run check type of command"""
raise NotImplementedError('type')
class BuiltinLauncher:
"""
cd: change the current working directory, module CdBuiltin
echo: print a message to the terminal, module EchoBuiltin
alias: create a shortcut for a longer command, module AliasBuiltin
history: display a list of previously executed commands, module HistoryBuiltin
export: set an environment variable, module ExportBuiltin
unset: remove an environment variable, module UnsetBuiltin
source: execute commands from a file in the current shell, module SourceBuiltin
exit: exit the current shell, module ExitBuiltin
pwd: print the current working directory, module PwdBuiltin
type: determine whether a command is a shell built-in or an external executable
module TypeBuiltin
"""
def __init__(self) -> None:
self.builtins_cmds = {
'cd': CdBuiltin(),
'echo': EchoBuiltin(),
'alias': AliasBuiltin(),
'history': HistoryBuiltin(),
'export': ExportBuiltin(),
'unset': UnsetBuiltin(),
'source': SourceBuiltin(),
'exit': ExitBuiltin(),
'pwd': PwdBuiltin(),
'type': TypeBuiltin(),
}
def is_builtin(self, command):
"""check builtin command"""
internal_command = command
if isinstance(command, list):
internal_command = command[0]
return internal_command.strip() in self.builtins_cmds
def launch_builtin(self, arguments):
"""launch builtin command"""
command_name = arguments[0].strip()
if command_name in self.builtins_cmds:
builtin_strategy = self.builtins_cmds[command_name]
builtin_strategy.run(arguments)
else:
raise NotImplementedError(command_name)