2
# -*- coding: utf-8 -*-
4
# ----------------------------------------------------------------------
6
# This file is part of ShoppingMortel.
7
# Copyright (C) 2012-today zAmis Association
9
# ShoppingMortel is free software: you can redistribute it and/or
10
# modify it under the terms of the GNU Affero General Public License
11
# as published by the Free Software Foundation, either version 3 of
12
# the License, or (at your option) any later version
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU Affero General Public License for more details
19
# You should have received a copy of the GNU Affero General Public
20
# License along with this program. If not, see
21
# <http://www.gnu.org/licenses/>
23
# ----------------------------------------------------------------------
26
# ----------------------------------------------------------------------
33
from misc.configuration import *
34
from misc.general_functions import *
37
# ----------------------------------------------------------------------
42
_logger = logging.getLogger(__name__)
45
# ----------------------------------------------------------------------
48
""" ShellCommand class.
51
def __init__(self, name, call, description, func):
54
self.description = description
58
return "<ShellCommand> (%s)" % (self.name)
65
def __init__(self, module = ''):
71
self.status = 'raw_input'
74
self.module_stack = []
78
self.shell_cmd_add_default()
81
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()
95
# ------------------------------------------------------------------
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
# Add cmd to the fct pointers dict
123
def shell_cmd_add(self, func, command_name, command_descr):
124
if self.func_dict.has_key(command_name):
125
_logger.warning('command %s already existing' % (command_name))
126
self.func_dict[command_name] = (func, command_descr)
128
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')
136
# ------------------------------------------------------------------
141
while(status != 'exit'):
143
user_input = raw_input('> ')
145
self.shell_exec_cmd_str(user_input)
147
def shell_exec_cmd_str(self, cmd_str):
149
(cmd, mod_cmd, prs_arg_lst, prs_arg_dict) = self.shell_cmd_parse(cmd_str)
151
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)
158
print '--> cmd not found: %s or %s' % (cmd, mod_cmd)
162
# ----------------------------------------------------------------------
164
def shell_cmd_parse(self, user_input):
167
# --------------------------------------------------
173
prs_meta_status = 'out'
182
# --------------------------------------------------
183
user_input = user_input.strip(' ').strip('\n')
187
# --------------------------------------------------
189
if prs_status == 'arg':
191
prs_status = 'end_arg'
196
elif prs_status == 'opt1':
201
prs_status = 'end_arg'
206
elif prs_status == 'opt2':
208
prs_status = 'end_opt2'
211
elif prs_status == 'in_guill':
213
prs_status = 'end_guill'
217
if i not in [' ', '"', '-']:
223
prs_status = 'in_guill'
225
if prs_status == 'end_arg' or prs_status == 'end_guill':
226
if prs_meta_status == 'in_opt':
227
prs_arg_dict[prs_meta_arg] = prs_cur_word
228
prs_meta_status = 'out'
230
prs_arg_lst.append(prs_cur_word)
233
elif prs_status == 'end_opt2':
234
prs_meta_arg = prs_cur_word
235
prs_meta_status = 'in_opt'
239
if prs_status == 'arg':
240
if prs_meta_status == 'in_opt':
241
prs_arg_dict[prs_meta_arg] = prs_cur_word
242
prs_meta_status = 'out'
244
prs_arg_lst.append(prs_cur_word)
247
cmd = prs_arg_lst.pop(0)
248
mod_cmd = self.module + '__' + cmd
249
mod_cmd = mod_cmd.lstrip('_')
252
prs_arg_dict['z_shell'] = self
255
return (cmd, mod_cmd, prs_arg_lst, prs_arg_dict)