65
def __init__(self, module = ''):
65
def __init__(self, module='default', self_obj=None):
69
71
self.id = _SHELL_NBR
71
73
self.status = 'raw_input'
74
self.module_stack = []
75
self.module_stack = [module]
76
self.self_stack = [self_obj]
75
77
# function pointers
76
78
self.func_dict = {}
80
self._shell_cmd_module_stack('shell', self)
78
81
self.shell_cmd_add_default()
81
84
return "<Shell (id %d)>" % (self.id)
84
# ------------------------------------------------------------------
86
# Jump into/Come back from module
87
def shell_module_stack(self, new_module):
88
self.module_stack.append(self.module)
89
self.module = new_module
91
def shell_module_pop(self):
92
self.module = self.module_stack.pop()
86
def shell_print(self, string):
87
print '> %s' % (string)
94
89
# Command management
95
90
# ------------------------------------------------------------------
97
def shell_cmd_exit(self):
98
print '--> exiting shell'
101
def _shell_cmd_print(self):
103
str += 'exit: %s\n' % (self.func_descr['exit'])
104
str += 'module\nmodule_stack: %s, module_set: %s, module_pop: %s, module_print: %s\n' % (self.func_descr['module_stack'], self.func_descr['module_set'], self.fct_descr['module_pop'], self.fct_descr['module_print'])
105
str += 'commands\ncmd_print: %s' % (self.func_descr['cmd_print'])
108
def _shell_cmd_module_stack(self):
109
# new_module = prs_arg_lst.pop(0)
110
# print '--> setting module to %s' % (new_module)
111
# self.zShell_module_stack(new_module)
114
def _shell_cmd_module_pop(self):
115
# self.zShell_module_pop()
116
# print '--> setting module to old %s' % (self.module)
119
def _shell_cmd_module_print(self):
120
print '--> module: %s' % (self.module)
122
92
# Add cmd to the fct pointers dict
123
93
def shell_cmd_add(self, func, command_name, command_descr):
124
if self.func_dict.has_key(command_name):
94
if self.func_dict.get(command_name):
125
95
_logger.warning('command %s already existing' % (command_name))
126
self.func_dict[command_name] = (func, command_descr)
97
self.func_dict[command_name] = ShellCommand(command_name, func, None, command_descr)
128
99
def shell_cmd_add_default(self):
129
self.shell_cmd_add(shell_cmd_exit, 'exit', 'exit shell')
130
self.shell_cmd_add(_shell_cmd_print, 'help', 'print commands')
131
self.shell_cmd_add(_shell_cmd_module_stack, 'module_stack', 'module_stack')
132
self.shell_cmd_add(_shell_cmd_module_pop, 'module_pop', 'module_pop')
133
self.shell_cmd_add(_shell_cmd_module_print, 'module_print', 'module_print')
100
self.shell_cmd_add(Shell._shell_cmd_exit, 'exit', 'exit shell')
101
self.shell_cmd_add(Shell._shell_cmd_print, 'help', 'help and commands')
102
self.shell_cmd_add(Shell._shell_cmd_module_stack, 'module_stack', 'module_stack')
103
self.shell_cmd_add(Shell._shell_cmd_module_pop, 'module_pop', 'module_pop')
104
self.shell_cmd_add(Shell._shell_cmd_module_print, 'module_print', 'module_print')
106
def _shell_cmd_exit(self, **kwargs):
107
self.shell_print('--> exiting shell')
110
def _shell_cmd_print(self, **kwargs):
111
string = 'available commands:\n'
112
for command_name, shell_command in self.func_dict.items():
113
string += '%s: %s\n' % (command_name, shell_command.description)
114
self.shell_print(string)
116
def _shell_cmd_module_stack(self, module_name, self_obj, **kwargs):
117
self.shell_print('--> setting module to %s' % (module_name))
118
self.module_stack.append(module_name)
119
self.self_stack.append(self_obj)
121
def _shell_cmd_module_pop(self, **kwargs):
122
self.module_stack.pop()
123
self.self_stack.pop()
124
self.shell_print('--> setting module to %s' % (self.module_stack[-1]))
126
def _shell_cmd_module_print(self, **kwargs):
127
self.shell_print('--> module: %s' % (self.module_stack[-1]))
136
130
# ------------------------------------------------------------------
151
145
self.shell_exec_cmd(cmd, mod_cmd, prs_arg_lst, prs_arg_dict)
153
def shell_exec_cmd(self, cmd, mod_cmd, prs_arg_lst, prs_arg_dict):
154
if self.func_dict.has_key(mod_cmd):
155
print '--> exec cmd: %s' % (mod_cmd)
156
self.func_dict[mod_cmd](*prs_arg_lst, **prs_arg_dict)
147
def shell_exec_cmd(self, command, command_w_mod, arg_list, arg_dict):
148
""" Execute a command
150
:param command: name of the command, such as 'exit'
151
:param command_w_mod: name of the command prefixed by the module
152
name, such as 'main__exit'
156
if self.func_dict.get(command):
157
print '--> exec command: %s' % (command)
158
self.func_dict[command].func(*arg_list, **arg_dict)
159
elif self.func_dict.get(command_w_mod):
160
print '--> exec command: %s' % (command_w_mod)
161
self.func_dict[command_w_mod].func(*arg_list, **arg_dict)
158
print '--> cmd not found: %s or %s' % (cmd, mod_cmd)
163
print '--> command not found: %s or %s' % (command, command_w_mod)
162
167
# ----------------------------------------------------------------------
164
169
def shell_cmd_parse(self, user_input):
167
# --------------------------------------------------
173
171
prs_meta_status = 'out'
174
172
prs_meta_arg = ''