annotate cmd2.py @ 32:ebefe2d57e3b

multiline stops after line 2
author devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
date Fri, 21 Dec 2007 15:31:17 -0500
parents 5e2f6ec2e383
children 061f40299ed5
rev   line source
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
1 """Variant on standard library's cmd with extra features:
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
2
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
3 Searchable command history
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
4 Multi-line commands
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
5 Case-insensitive commands
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
6 Special-character shortcut commands
25
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
7 Load commands from file
27
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
8 Settable environment parameters
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
9
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
10 still to do:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
11 edit
30
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
12 run
27
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
13 >
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
14 """
31
5e2f6ec2e383 load so close!
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 30
diff changeset
15 import cmd, re, os, sys
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
17 class Cmd(cmd.Cmd):
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
18 caseInsensitive = True
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
19 multilineCommands = []
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
20 continuationPrompt = '> '
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
21 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'}
22
221095f3a4af all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 21
diff changeset
22 excludeFromHistory = '''run r list l history hi ed li eof'''.split()
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
23 defaultExtension = 'txt'
27
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
24 defaultFileName = 'command.txt'
31
5e2f6ec2e383 load so close!
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 30
diff changeset
25 editor = os.environ.get('EDITOR')
5e2f6ec2e383 load so close!
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 30
diff changeset
26 if not editor:
5e2f6ec2e383 load so close!
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 30
diff changeset
27 if sys.platform[:3] == 'win':
5e2f6ec2e383 load so close!
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 30
diff changeset
28 editor = 'notepad'
30
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
29 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive']
26
bb3fb24b6f5f settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 25
diff changeset
30 terminators = ';\n'
25
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
31 def do_cmdenvironment(self, args):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
32 self.stdout.write("""
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
33 Commands are %(casesensitive)scase-sensitive.
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
34 Commands may be terminated with: %(terminators)s
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
35 Settable parameters: %(settable)s
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
36 """ %
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
37 { 'casesensitive': 'not ' if self.caseInsensitive else '',
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
38 'terminators': ' '.join(self.terminators),
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
39 'settable': ' '.join(self.settable)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
40 })
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
41
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
42 def __init__(self, *args, **kwargs):
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
43 cmd.Cmd.__init__(self, *args, **kwargs)
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
44 self.history = History()
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
45
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
46 def precmd(self, line):
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
47 """Hook method executed just before the command line is
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
48 interpreted, but after the input prompt is generated and issued.
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
49
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
50 Makes commands case-insensitive (but unfortunately does not alter command completion).
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
51 """
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
52 try:
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
53 (command, args) = line.split(None,1)
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
54 except ValueError:
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
55 (command, args) = line, ''
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
56 if self.caseInsensitive:
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
57 command = command.lower()
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
58 statement = ' '.join([command, args])
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
59 if (not self.multilineCommands) or (command not in self.multilineCommands):
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
60 return statement
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
61 return self.finishStatement(statement)
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
62
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
63 def postcmd(self, stop, line):
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
64 """Hook method executed just after a command dispatch is finished."""
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
65 try:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
66 command = line.split(None,1)[0].lower()
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
67 if command not in self.excludeFromHistory:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
68 self.history.append(line)
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
69 finally:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
70 return stop
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
71
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
72 def finishStatement(self, firstline):
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
73 statement = firstline
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
74 while not self.statementHasEnded(statement):
30
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
75 inp = self.pseudo_raw_input(self.continuationPrompt)
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
76 statement = '%s\n%s' % (statement, inp)
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
77 return statement
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
78 # assembling a list of lines and joining them at the end would be faster,
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
79 # but statementHasEnded needs a string arg; anyway, we're getting
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
80 # user input and users are slow.
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
81
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
82 def pseudo_raw_input(self, prompt):
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
83 """copied from cmd's cmdloop; like raw_input, but accounts for changed stdin, stdout"""
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
84
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
85 if self.use_rawinput:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
86 try:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
87 line = raw_input(prompt)
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
88 except EOFError:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
89 line = 'EOF'
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
90 else:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
91 self.stdout.write(prompt)
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
92 self.stdout.flush()
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
93 line = self.stdin.readline()
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
94 if not len(line):
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
95 line = 'EOF'
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
96 else:
32
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
97 if line[-1] == '\n': # this was always true in Cmd
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
98 line = line[:-1]
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
99 return line
32
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
100
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
101 def cmdloop(self, intro=None):
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
102 """Repeatedly issue a prompt, accept input, parse an initial prefix
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
103 off the received input, and dispatch to action methods, passing them
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
104 the remainder of the line as argument.
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
105 """
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
106
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
107 # An almost perfect copy from Cmd; however, the pseudo_raw_input portion
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
108 # has been split out so that it can be called separately
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
109
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
110 self.preloop()
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
111 if self.use_rawinput and self.completekey:
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
112 try:
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
113 import readline
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
114 self.old_completer = readline.get_completer()
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
115 readline.set_completer(self.complete)
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
116 readline.parse_and_bind(self.completekey+": complete")
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
117 except ImportError:
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
118 pass
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
119 try:
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
120 if intro is not None:
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
121 self.intro = intro
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
122 if self.intro:
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
123 self.stdout.write(str(self.intro)+"\n")
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
124 stop = None
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
125 while not stop:
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
126 if self.cmdqueue:
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
127 line = self.cmdqueue.pop(0)
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
128 else:
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
129 line = self.pseudo_raw_input(self.prompt)
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
130 line = self.precmd(line)
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
131 stop = self.onecmd(line)
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
132 stop = self.postcmd(stop, line)
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
133 self.postloop()
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
134 finally:
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
135 if self.use_rawinput and self.completekey:
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
136 try:
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
137 import readline
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
138 readline.set_completer(self.old_completer)
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
139 except ImportError:
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
140 pass
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
141
ebefe2d57e3b multiline stops after line 2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 31
diff changeset
142
22
221095f3a4af all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 21
diff changeset
143 def do_EOF(self, arg):
221095f3a4af all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 21
diff changeset
144 return True
221095f3a4af all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 21
diff changeset
145 do_eof = do_EOF
221095f3a4af all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 21
diff changeset
146
30
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
147 statementEndPattern = re.compile(r'([%s]\s*)|(EOF)$' % terminators)
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
148 def statementHasEnded(self, lines):
19
25f22e742a68 statement ends fixed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 18
diff changeset
149 return bool(self.statementEndPattern.search(lines))
25f22e742a68 statement ends fixed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 18
diff changeset
150
25
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
151 def clean(self, s):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
152 """cleans up a string"""
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
153 if self.caseInsensitive:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
154 return s.strip().lower()
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
155 return s.strip()
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
156
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
157 def parseline(self, line):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
158 """Parse the line into a command name and a string containing
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
159 the arguments. Returns a tuple containing (command, args, line).
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
160 'command' and 'args' may be None if the line couldn't be parsed.
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
161 """
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
162 line = line.strip()
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
163 if not line:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
164 return None, None, line
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
165 shortcut = self.shortcuts.get(line[0])
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
166 if shortcut and hasattr(self, 'do_%s' % shortcut):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
167 line = '%s %s' % (shortcut, line[1:])
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
168 i, n = 0, len(line)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
169 while i < n and line[i] in self.identchars: i = i+1
26
bb3fb24b6f5f settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 25
diff changeset
170 cmd, arg = line[:i], line[i:].strip().strip(self.terminators)
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
171 return cmd, arg, line
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
172
25
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
173 def showParam(self, param):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
174 param = self.clean(param)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
175 if param in self.settable:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
176 val = getattr(self, param)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
177 self.stdout.write('%s: %s\n' % (param, str(getattr(self, param))))
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
178
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
179 def do_show(self, arg):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
180 'Shows value of a parameter'
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
181 if arg.strip():
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
182 self.showParam(arg)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
183 else:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
184 for param in self.settable:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
185 self.showParam(param)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
186
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
187 def do_set(self, arg):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
188 'Sets a parameter'
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
189 try:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
190 paramName, val = arg.split(None, 1)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
191 paramName = self.clean(paramName)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
192 if paramName not in self.settable:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
193 raise NotSettableError
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
194 currentVal = getattr(self, paramName)
26
bb3fb24b6f5f settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 25
diff changeset
195 val = cast(currentVal, val.strip(self.terminators))
25
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
196 setattr(self, paramName, val)
26
bb3fb24b6f5f settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 25
diff changeset
197 self.stdout.write('%s - was: %s\nnow: %s\n' % (paramName, currentVal, val))
30
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
198 except (ValueError, AttributeError, NotSettableError), e:
25
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
199 self.do_show(arg)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
200
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
201 def do_shell(self, arg):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
202 'execute a command as if at the OS prompt.'
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
203 os.system(arg)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
204
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
205 def do_history(self, arg):
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
206 """history [arg]: lists past commands issued
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
207
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
208 no arg -> list all
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
209 arg is integer -> list one history item, by index
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
210 arg is string -> string search
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
211 arg is /enclosed in forward-slashes/ -> regular expression search
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
212 """
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
213 if arg:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
214 history = self.history.get(arg)
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
215 else:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
216 history = self.history
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
217 for hi in history:
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
218 self.stdout.write(hi.pr())
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
219 def last_matching(self, arg):
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
220 try:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
221 if arg:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
222 return self.history.get(arg)[-1]
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
223 else:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
224 return self.history[-1]
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
225 except:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
226 return None
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
227 def do_list(self, arg):
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
228 """list [arg]: lists last command issued
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
229
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
230 no arg -> list absolute last
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
231 arg is integer -> list one history item, by index
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
232 - arg, arg - (integer) -> list up to or after #arg
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
233 arg is string -> list last command matching string search
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
234 arg is /enclosed in forward-slashes/ -> regular expression search
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
235 """
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
236 try:
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
237 self.stdout.write(self.last_matching(arg).pr())
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
238 except:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
239 pass
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
240 do_hi = do_history
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
241 do_l = do_list
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
242 do_li = do_list
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
243
27
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
244 def do_ed(self, arg):
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
245 'ed [N]: brings up SQL from N commands ago in text editor, and puts result in SQL buffer.'
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
246 if not self.editor:
30
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
247 print "please use 'set editor' to specify your text editing program of choice."
27
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
248 return
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
249 buffer = self.last_matching(arg)
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
250 f = open(self.defaultFileName, 'w')
31
5e2f6ec2e383 load so close!
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 30
diff changeset
251 f.write(buffer or '')
27
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
252 f.close()
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
253 os.system('%s %s' % (self.editor, self.defaultFileName))
30
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
254 self.do_load(self.defaultFileName)
27
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
255 do_edit = do_ed
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
256
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
257 def do_save(self, fname=None):
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
258 """Saves most recent command to a file."""
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
259
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
260 if fname is None:
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
261 fname = self.defaultFileName
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
262 try:
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
263 f = open(fname, 'w')
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
264 f.write(self.history[-1])
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
265 f.close()
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
266 except Exception, e:
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
267 print 'Error saving %s: %s' % (fname, str(e))
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
268
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
269 def do_load(self, fname=None):
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
270 """Runs command(s) from a file."""
27
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
271 if fname is None:
ca6f34be3397 working on ed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 26
diff changeset
272 fname = self.defaultFileName
24
7a89805a47b1 load cleaned up
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 23
diff changeset
273 keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt','continuationPrompt'))
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
274 try:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
275 self.stdin = open(fname, 'r')
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
276 except IOError, e:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
277 try:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
278 self.stdin = open('%s.%s' % (fname, self.defaultExtension), 'r')
29
a6f68b25ae16 load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 27
diff changeset
279 except IOError:
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
280 print 'Problem opening file %s: \n%s' % (fname, e)
29
a6f68b25ae16 load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 27
diff changeset
281 keepstate.restore()
a6f68b25ae16 load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 27
diff changeset
282 return
a6f68b25ae16 load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 27
diff changeset
283 self.use_rawinput = False
a6f68b25ae16 load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 27
diff changeset
284 self.prompt = self.continuationPrompt = ''
a6f68b25ae16 load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 27
diff changeset
285 self.cmdloop()
a6f68b25ae16 load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 27
diff changeset
286 self.stdin.close()
a6f68b25ae16 load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 27
diff changeset
287 keepstate.restore()
a6f68b25ae16 load is perfect
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 27
diff changeset
288 self.lastcmd = ''
24
7a89805a47b1 load cleaned up
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 23
diff changeset
289
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
290 class HistoryItem(str):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
291 def __init__(self, instr):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
292 str.__init__(self, instr)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
293 self.lowercase = self.lower()
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
294 self.idx = None
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
295 def pr(self):
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
296 return '-------------------------[%d]\n%s\n' % (self.idx, str(self))
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
297
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
298 class History(list):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
299 rangeFrom = re.compile(r'^([\d])+\s*\-$')
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
300 def append(self, new):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
301 new = HistoryItem(new)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
302 list.append(self, new)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
303 new.idx = len(self)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
304 def extend(self, new):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
305 for n in new:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
306 self.append(n)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
307 def get(self, getme):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
308 try:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
309 getme = int(getme)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
310 if getme < 0:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
311 return self[:(-1 * getme)]
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
312 else:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
313 return [self[getme-1]]
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
314 except IndexError:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
315 return []
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
316 except (ValueError, TypeError):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
317 getme = getme.strip()
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
318 mtch = self.rangeFrom.search(getme)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
319 if mtch:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
320 return self[(int(mtch.group(1))-1):]
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
321 if getme.startswith(r'/') and getme.endswith(r'/'):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
322 finder = re.compile(getme[1:-1], re.DOTALL | re.MULTILINE | re.IGNORECASE)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
323 def isin(hi):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
324 return finder.search(hi)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
325 else:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
326 def isin(hi):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
327 return (getme.lower() in hi.lowercase)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
328 return [itm for itm in self if isin(itm)]
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
329
25
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
330 class NotSettableError(Exception):
30
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
331 pass
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
332
26
bb3fb24b6f5f settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 25
diff changeset
333 def cast(current, new):
bb3fb24b6f5f settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 25
diff changeset
334 """Tries to force a new value into the same type as the current."""
bb3fb24b6f5f settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 25
diff changeset
335 typ = type(current)
bb3fb24b6f5f settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 25
diff changeset
336 if typ == bool:
30
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
337 try:
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
338 return bool(int(new))
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
339 except ValueError, TypeError:
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
340 pass
26
bb3fb24b6f5f settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 25
diff changeset
341 try:
30
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
342 new = new.lower()
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
343 except:
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
344 pass
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
345 if (new=='on') or (new[0] in ('y','t')):
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
346 return True
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
347 if (new=='off') or (new[0] in ('n','f')):
26
bb3fb24b6f5f settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 25
diff changeset
348 return False
30
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
349 else:
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
350 try:
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
351 return typ(new)
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
352 except:
26
bb3fb24b6f5f settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 25
diff changeset
353 pass
30
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
354 print "Problem setting parameter (now %s) to %s; incorrect type?" % (current, new)
2739250177ed load fixed?
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 29
diff changeset
355 return current
26
bb3fb24b6f5f settables ironed out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 25
diff changeset
356
25
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
357 class Statekeeper(object):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
358 def __init__(self, obj, attribs):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
359 self.obj = obj
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
360 self.attribs = attribs
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
361 self.save()
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
362 def save(self):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
363 for attrib in self.attribs:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
364 setattr(self, attrib, getattr(self.obj, attrib))
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
365 def restore(self):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
366 for attrib in self.attribs:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
367 setattr(self.obj, attrib, getattr(self, attrib))