~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 12:04:53 UTC
  • Revision ID: tde@openerp.com-20120629120453-vtjazl4k28e6va0b
[ADD] Added base lib: contains first version of some tools description functions, and the shell.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# ----------------------------------------------------------------------
 
5
#
 
6
#    This file is part of ShoppingMortel.
 
7
#    Copyright (C) 2012-today zAmis Association
 
8
#
 
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
 
13
#
 
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
 
18
#
 
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/>
 
22
#
 
23
# ----------------------------------------------------------------------
 
24
 
 
25
# Imports
 
26
# ----------------------------------------------------------------------
 
27
 
 
28
# Standard libraries
 
29
import argparse
 
30
import logging
 
31
 
 
32
# Misc library
 
33
from misc.configuration                                                                         import *
 
34
from misc.general_functions                                                                     import *
 
35
 
 
36
# Vars
 
37
# ----------------------------------------------------------------------
 
38
 
 
39
global _SHELL_NBR
 
40
_SHELL_NBR = 0
 
41
 
 
42
_logger = logging.getLogger(__name__)
 
43
 
 
44
# Class
 
45
# ----------------------------------------------------------------------
 
46
 
 
47
class ShellCommand():
 
48
        """     ShellCommand class.
 
49
        """
 
50
 
 
51
        def __init__(self, name, call, description, func):
 
52
                self.name = name
 
53
                self.call = call
 
54
                self.description = description
 
55
                self.func = func
 
56
        
 
57
        def __str__(self):
 
58
                return "<ShellCommand> (%s)" % (self.name)
 
59
 
 
60
 
 
61
class Shell():
 
62
        """     Shell class.
 
63
        """
 
64
 
 
65
        def __init__(self, module = ''):
 
66
                global _SHELL_NBR
 
67
                _SHELL_NBR += 1
 
68
                # ID
 
69
                self.id = _SHELL_NBR
 
70
                # status
 
71
                self.status = 'raw_input'
 
72
                # modules
 
73
                self.module = module
 
74
                self.module_stack = []
 
75
                # function pointers
 
76
                self.func_dict = {}
 
77
                # init shell cmds
 
78
                self.shell_cmd_add_default()
 
79
 
 
80
        def __str__(self):
 
81
                return "<Shell (id %d)>" % (self.id)
 
82
 
 
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()
 
93
 
 
94
        # Command management
 
95
        # ------------------------------------------------------------------
 
96
 
 
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
        # 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)
 
127
 
 
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')
 
134
 
 
135
        # Main cmds
 
136
        # ------------------------------------------------------------------
 
137
 
 
138
        def shell_run(self):
 
139
                global status
 
140
                status = 'raw_input'
 
141
                while(status != 'exit'):
 
142
                        # get user input
 
143
                        user_input = raw_input('> ')
 
144
                        # analyse input
 
145
                        self.shell_exec_cmd_str(user_input)
 
146
 
 
147
        def shell_exec_cmd_str(self, cmd_str):
 
148
                # analyse input
 
149
                (cmd, mod_cmd, prs_arg_lst, prs_arg_dict) = self.shell_cmd_parse(cmd_str)
 
150
                # exec cmd
 
151
                self.shell_exec_cmd(cmd, mod_cmd, prs_arg_lst, prs_arg_dict)
 
152
 
 
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)
 
157
                else:
 
158
                        print '--> cmd not found: %s or %s' % (cmd, mod_cmd)
 
159
        
 
160
        
 
161
        # Parser
 
162
        # ----------------------------------------------------------------------
 
163
        
 
164
        def shell_cmd_parse(self, user_input):
 
165
                
 
166
                # vars
 
167
                # --------------------------------------------------
 
168
                
 
169
                # global
 
170
                global status
 
171
                
 
172
                # parser vars
 
173
                prs_meta_status = 'out'
 
174
                prs_meta_arg = ''
 
175
                prs_status = 'out'
 
176
                prs_arg_dict = {}
 
177
                prs_arg_lst = []
 
178
                prs_cur_word = ''
 
179
                
 
180
                
 
181
                # sanitize cmd
 
182
                # --------------------------------------------------
 
183
                user_input = user_input.strip(' ').strip('\n')
 
184
                
 
185
                
 
186
                # parse
 
187
                # --------------------------------------------------
 
188
                for i in user_input:
 
189
                        if prs_status == 'arg':
 
190
                                if i == ' ':
 
191
                                        prs_status = 'end_arg'
 
192
                                elif i == '-':
 
193
                                        prs_status = 'opt1'
 
194
                                else:
 
195
                                        prs_cur_word += i
 
196
                        elif prs_status == 'opt1':
 
197
                                if i == '-':
 
198
                                        prs_status = 'opt2'
 
199
                                elif i == ' ':
 
200
                                        prs_cur_word += '-'
 
201
                                        prs_status = 'end_arg'
 
202
                                else:
 
203
                                        prs_cur_word += '-'
 
204
                                        prs_cur_word += i
 
205
                                        prs_status = 'arg'
 
206
                        elif prs_status == 'opt2':
 
207
                                if i == ' ':
 
208
                                        prs_status = 'end_opt2'
 
209
                                else:
 
210
                                        prs_cur_word += i
 
211
                        elif prs_status == 'in_guill':
 
212
                                if i == '"':
 
213
                                        prs_status = 'end_guill'
 
214
                                else:
 
215
                                        prs_cur_word += i
 
216
                        else:
 
217
                                if i not in [' ', '"', '-']:
 
218
                                        prs_status = 'arg'
 
219
                                        prs_cur_word += i
 
220
                                elif i == '-':
 
221
                                        prs_status = 'opt1'
 
222
                                elif i == '"':
 
223
                                        prs_status = 'in_guill'
 
224
                        
 
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'
 
229
                                else:
 
230
                                        prs_arg_lst.append(prs_cur_word)
 
231
                                prs_cur_word = ''
 
232
                                prs_status = 'out'
 
233
                        elif prs_status == 'end_opt2':
 
234
                                prs_meta_arg = prs_cur_word
 
235
                                prs_meta_status = 'in_opt'
 
236
                                prs_cur_word = ''
 
237
                                prs_status = 'out'
 
238
                        
 
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'
 
243
                        else:
 
244
                                prs_arg_lst.append(prs_cur_word)
 
245
                
 
246
                # extract cmd
 
247
                cmd = prs_arg_lst.pop(0)
 
248
                mod_cmd = self.module + '__' + cmd
 
249
                mod_cmd = mod_cmd.lstrip('_')
 
250
                
 
251
                # add zShell
 
252
                prs_arg_dict['z_shell'] = self
 
253
                
 
254
                # return parsed data
 
255
                return (cmd, mod_cmd, prs_arg_lst, prs_arg_dict)