~tde-openerp/deadlyshoping/trunk

« back to all changes in this revision

Viewing changes to src/base/shell.py

  • Committer: Thibault Delavallée
  • Date: 2012-06-29 14:37:14 UTC
  • Revision ID: tde@openerp.com-20120629143714-awem7b8vz8r88oke
[IMP] base.shell: cleaned and update old code, now basically working and a bit more consistent.

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
        """     ShellCommand class.
49
49
        """
50
50
 
51
 
        def __init__(self, name, call, description, func):
 
51
        def __init__(self, name, func, call, description):
52
52
                self.name = name
53
53
                self.call = call
54
54
                self.description = description
62
62
        """     Shell class.
63
63
        """
64
64
 
65
 
        def __init__(self, module = ''):
 
65
        def __init__(self, module='default', self_obj=None):
66
66
                global _SHELL_NBR
67
67
                _SHELL_NBR += 1
 
68
                if not self_obj:
 
69
                        self_obj = self
68
70
                # ID
69
71
                self.id = _SHELL_NBR
70
72
                # status
71
73
                self.status = 'raw_input'
72
74
                # modules
73
 
                self.module = module
74
 
                self.module_stack = []
 
75
                self.module_stack = [module]
 
76
                self.self_stack = [self_obj]
75
77
                # function pointers
76
78
                self.func_dict = {}
77
79
                # init shell cmds
 
80
                self._shell_cmd_module_stack('shell', self)
78
81
                self.shell_cmd_add_default()
79
82
 
80
83
        def __str__(self):
81
84
                return "<Shell (id %d)>" % (self.id)
82
85
 
83
 
        # Modules
84
 
        # ------------------------------------------------------------------
85
 
        
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
90
 
 
91
 
        def shell_module_pop(self):
92
 
                self.module = self.module_stack.pop()
 
86
        def shell_print(self, string):
 
87
                print '> %s' % (string)
93
88
 
94
89
        # Command management
95
90
        # ------------------------------------------------------------------
96
91
 
97
 
        def shell_cmd_exit(self):
98
 
                print '--> exiting shell'
99
 
                self.status = 'exit'
100
 
 
101
 
        def _shell_cmd_print(self):
102
 
                str = ''
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'])
106
 
                print str
107
 
 
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)
112
 
                pass
113
 
 
114
 
        def _shell_cmd_module_pop(self):
115
 
                # self.zShell_module_pop()
116
 
                # print '--> setting module to old %s' % (self.module)
117
 
                pass
118
 
 
119
 
        def _shell_cmd_module_print(self):
120
 
                print '--> module: %s' % (self.module)
121
 
 
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)
 
96
                else:
 
97
                        self.func_dict[command_name] = ShellCommand(command_name, func, None, command_descr)
127
98
 
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')
 
105
 
 
106
        def _shell_cmd_exit(self, **kwargs):
 
107
                self.shell_print('--> exiting shell')
 
108
                self.status = 'exit'
 
109
 
 
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)
 
115
 
 
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)
 
120
 
 
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]))
 
125
 
 
126
        def _shell_cmd_module_print(self, **kwargs):
 
127
                self.shell_print('--> module: %s' % (self.module_stack[-1]))
134
128
 
135
129
        # Main cmds
136
130
        # ------------------------------------------------------------------
150
144
                # exec cmd
151
145
                self.shell_exec_cmd(cmd, mod_cmd, prs_arg_lst, prs_arg_dict)
152
146
 
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
 
149
 
 
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'
 
153
                        :param arg_list:
 
154
                        :param arg_dict:
 
155
                """
 
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)
157
162
                else:
158
 
                        print '--> cmd not found: %s or %s' % (cmd, mod_cmd)
 
163
                        print '--> command not found: %s or %s' % (command, command_w_mod)
159
164
        
160
165
        
161
166
        # Parser
162
167
        # ----------------------------------------------------------------------
163
168
        
164
169
        def shell_cmd_parse(self, user_input):
165
 
                
166
 
                # vars
167
 
                # --------------------------------------------------
168
 
                
169
 
                # global
170
 
                global status
171
 
                
172
170
                # parser vars
173
171
                prs_meta_status = 'out'
174
172
                prs_meta_arg = ''
176
174
                prs_arg_dict = {}
177
175
                prs_arg_lst = []
178
176
                prs_cur_word = ''
179
 
                
180
 
                
 
177
 
181
178
                # sanitize cmd
182
 
                # --------------------------------------------------
183
179
                user_input = user_input.strip(' ').strip('\n')
184
 
                
185
 
                
 
180
 
186
181
                # parse
187
 
                # --------------------------------------------------
188
182
                for i in user_input:
189
183
                        if prs_status == 'arg':
190
184
                                if i == ' ':
245
239
                
246
240
                # extract cmd
247
241
                cmd = prs_arg_lst.pop(0)
248
 
                mod_cmd = self.module + '__' + cmd
 
242
                mod_cmd = self.module_stack[-1] + '__' + cmd
249
243
                mod_cmd = mod_cmd.lstrip('_')
250
 
                
251
 
                # add zShell
252
 
                prs_arg_dict['z_shell'] = self
253
 
                
 
244
                # add shell
 
245
                prs_arg_dict['shell'] = self
 
246
                # add self as first parameter
 
247
                prs_arg_lst.insert(0, self.self_stack[-1])
254
248
                # return parsed data
255
249
                return (cmd, mod_cmd, prs_arg_lst, prs_arg_dict)