~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shell.py

  • Committer: Aaron Bentley
  • Date: 2006-03-11 15:42:15 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20060311154215-cd9599aa4e996ccf
Initial nested progressbar work.  (Need console fix)

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
#    You should have received a copy of the GNU General Public License
15
15
#    along with this program; if not, write to the Free Software
16
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
 
18
17
import cmd
19
 
from itertools import chain
 
18
import sys
20
19
import os
 
20
import terminal
21
21
import readline
22
 
import shlex
23
22
import string
24
 
import sys
25
 
 
 
23
from itertools import chain
 
24
from bzrlib.errors import BzrError
 
25
from bzrlib.commands import get_cmd_object, get_all_cmds
26
26
from bzrlib.branch import Branch
27
 
from bzrlib.commands import get_cmd_object, get_all_cmds, get_alias
28
 
from bzrlib.errors import BzrError
29
 
 
30
 
import terminal
31
 
 
32
27
 
33
28
SHELL_BLACKLIST = set(['rm', 'ls'])
34
29
COMPLETION_BLACKLIST = set(['shell'])
35
30
 
36
 
 
37
31
class BlackListedCommand(BzrError):
38
32
    def __init__(self, command):
39
33
        BzrError.__init__(self, "The command %s is blacklisted for shell use" %
40
34
                          command)
41
35
 
42
 
 
43
36
class CompletionContext(object):
44
37
    def __init__(self, text, command=None, prev_opt=None, arg_pos=None):
45
38
        self.text = text
160
153
        self.default("help "+line)
161
154
 
162
155
    def default(self, line):
163
 
        args = shlex.split(line)
164
 
        alias_args = get_alias(args[0])
165
 
        if alias_args is not None:
166
 
            args[0] = alias_args.pop(0)
167
 
            
 
156
        args = line.split()
168
157
        commandname = args.pop(0)
169
158
        for char in ('|', '<', '>'):
170
159
            commandname = commandname.split(char)[0]
181
170
            if too_complicated(line):
182
171
                return os.system("bzr "+line)
183
172
            else:
184
 
                return (cmd_obj.run_argv_aliases(args, alias_args) or 0)
 
173
                return (cmd_obj.run_argv(args) or 0)
185
174
        except BzrError, e:
186
175
            print e
187
176
        except KeyboardInterrupt, e:
211
200
            cmd = None
212
201
        return CompletionContext(text, command=cmd).get_completions()
213
202
 
214
 
 
215
203
def run_shell():
216
204
    try:
217
205
        prompt = PromptCmd()
222
210
    except StopIteration:
223
211
        pass
224
212
 
225
 
 
226
213
def iter_opt_completions(command_obj):
227
214
    for option_name, option in command_obj.options().items():
228
215
        yield "--" + option_name
230
217
        if short_name:
231
218
            yield "-" + short_name
232
219
 
233
 
 
234
220
def iter_file_completions(arg, only_dirs = False):
235
221
    """Generate an iterator that iterates through filename completions.
236
222
 
270
256
    """
271
257
    return iter_file_completions(arg, True)
272
258
 
273
 
 
274
259
def iter_command_names(hidden=False):
275
260
    for real_cmd_name, cmd_class in get_all_cmds():
276
261
        if not hidden and cmd_class.hidden:
280
265
            if name == real_cmd_name or not real_cmd_name.startswith(name):
281
266
                yield name
282
267
 
283
 
 
284
268
def filter_completions(iter, arg):
285
269
    return (c for c in iter if c.startswith(arg))
286
270
 
287
 
 
288
271
def iter_munged_completions(iter, arg, text):
289
272
    for completion in iter:
290
273
        completion = str(completion)
291
274
        if completion.startswith(arg):
292
275
            yield completion[len(arg)-len(text):]+" "
293
276
 
294
 
 
295
277
def too_complicated(line):
296
 
    for char in '|<>*?':
 
278
    for char in '|<>"\"*?':
297
279
        if char in line:
298
280
            return True
299
281
    return False