annotate cmd2.py @ 21:8b55aaa52ce9

working on load, and preserving stdin/out
author devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
date Wed, 19 Dec 2007 13:40:39 -0500
parents d6d64c2e3b98
children 221095f3a4af
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
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
7
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
8 still to do:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
9 environment (maxrows, etc.)
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
10 edit
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
11
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
12 """
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
13 import cmd, re, os
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
14
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
15 class Cmd(cmd.Cmd):
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
16 caseInsensitive = True
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
17 multilineCommands = []
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
18 continuationPrompt = '> '
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
19 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'}
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
20 excludeFromHistory = '''run r list l history hi ed li'''.split()
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
21 defaultExtension = 'txt'
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
22 def __init__(self, *args, **kwargs):
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
23 cmd.Cmd.__init__(self, *args, **kwargs)
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
24 self.history = History()
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
25
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
26 def precmd(self, line):
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
27 """Hook method executed just before the command line is
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
28 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
29
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
30 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
31 """
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
32 try:
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
33 (command, args) = line.split(None,1)
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
34 except ValueError:
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
35 (command, args) = line, ''
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
36 if self.caseInsensitive:
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
37 command = command.lower()
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
38 statement = ' '.join([command, args])
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
39 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
40 return statement
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
41 return self.finishStatement(statement)
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
42
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
43 def postcmd(self, stop, line):
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
44 """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
45 try:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
46 command = line.split(None,1)[0].lower()
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
47 if command not in self.excludeFromHistory:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
48 self.history.append(line)
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
49 finally:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
50 return stop
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
51
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
52 def finishStatement(self, firstline):
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
53 statement = firstline
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
54 while not self.statementHasEnded(statement):
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
55 statement = '%s\n%s' % (statement, self.pseudo_raw_input(self.continuationPrompt))
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
56 return statement
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
57 # 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
58 # 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
59 # user input and users are slow.
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
60
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
61 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
62 """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
63
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
64 if self.use_rawinput:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
65 try:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
66 line = raw_input(prompt)
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
67 except EOFError:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
68 line = 'EOF'
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
69 else:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
70 self.stdout.write(prompt)
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
71 self.stdout.flush()
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
72 line = self.stdin.readline()
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
73 if not len(line):
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
74 line = 'EOF'
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
75 else:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
76 line = line[:-1] # chop \n
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
77 return line
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
78
19
25f22e742a68 statement ends fixed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 18
diff changeset
79 statementEndPattern = re.compile(r'[\n;]\s*$')
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
80 def statementHasEnded(self, lines):
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
81 """This version lets statements end with ; or with a blank line.
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
82 Override for your own needs."""
19
25f22e742a68 statement ends fixed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 18
diff changeset
83 return bool(self.statementEndPattern.search(lines))
25f22e742a68 statement ends fixed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 18
diff changeset
84
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
85 def parseline(self, line):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
86 """Parse the line into a command name and a string containing
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
87 the arguments. Returns a tuple containing (command, args, line).
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
88 '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
89 """
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
90 line = line.strip()
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
91 if not line:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
92 return None, None, line
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
93 shortcut = self.shortcuts.get(line[0])
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
94 if shortcut and hasattr(self, 'do_%s' % shortcut):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
95 line = '%s %s' % (shortcut, line[1:])
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
96 i, n = 0, len(line)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
97 while i < n and line[i] in self.identchars: i = i+1
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
98 cmd, arg = line[:i], line[i:].strip()
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
99 return cmd, arg, line
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
100
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
101 def do_shell(self, arg):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
102 'execute a command as if at the OS prompt.'
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
103 os.system(arg)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
104
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
105 def do_history(self, arg):
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
106 """history [arg]: lists past commands issued
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
107
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
108 no arg -> list all
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
109 arg is integer -> list one history item, by index
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
110 arg is string -> string search
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
111 arg is /enclosed in forward-slashes/ -> regular expression search
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
112 """
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
113 if arg:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
114 history = self.history.get(arg)
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
115 else:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
116 history = self.history
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
117 for hi in history:
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
118 self.stdout.write(hi.pr())
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
119 def last_matching(self, arg):
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
120 try:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
121 if arg:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
122 return self.history.get(arg)[-1]
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
123 else:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
124 return self.history[-1]
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
125 except:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
126 return None
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
127 def do_list(self, arg):
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
128 """list [arg]: lists last command issued
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
129
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
130 no arg -> list absolute last
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
131 arg is integer -> list one history item, by index
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
132 - arg, arg - (integer) -> list up to or after #arg
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
133 arg is string -> list last command matching string search
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
134 arg is /enclosed in forward-slashes/ -> regular expression search
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
135 """
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
136 try:
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
137 self.stdout.write(self.last_matching(arg).pr())
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
138 except:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
139 pass
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
140 do_hi = do_history
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
141 do_l = do_list
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
142 do_li = do_list
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
143
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
144 def breakupStatements(self, txt):
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
145 """takes text that may include multiple statements and
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
146 breaks it into a list of individual statements."""
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
147 result = ['']
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
148 for line in txt.splitlines():
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
149 result[-1] += line
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
150 if self.statementHasEnded(result[-1]):
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
151 result.append('')
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
152
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
153 def do_load(self, fname):
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
154 """Runs command(s) from a file."""
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
155 stdin = self.stdin
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
156 try:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
157 self.stdin = open(fname, 'r')
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
158 except IOError, e:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
159 try:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
160 self.stdin = open('%s.%s' % (fname, self.defaultExtension), 'r')
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
161 except:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
162 print 'Problem opening file %s: \n%s' % (fname, e)
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
163 self.stdin = stdin
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
164 return
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
165 use_rawinput = self.use_rawinput
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
166 self.use_rawinput = False
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
167 print 'stdin = ' + str(self.stdin)
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
168 self.cmdloop()
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
169 self.stdin.close()
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
170 self.stdin = stdin
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
171 self.use_rawinput = use_rawinput
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
172
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
173 class HistoryItem(str):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
174 def __init__(self, instr):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
175 str.__init__(self, instr)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
176 self.lowercase = self.lower()
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
177 self.idx = None
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
178 def pr(self):
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
179 return '-------------------------[%d]\n%s\n' % (self.idx, str(self))
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
180
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
181 class History(list):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
182 rangeFrom = re.compile(r'^([\d])+\s*\-$')
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
183 def append(self, new):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
184 new = HistoryItem(new)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
185 list.append(self, new)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
186 new.idx = len(self)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
187 def extend(self, new):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
188 for n in new:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
189 self.append(n)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
190 def get(self, getme):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
191 try:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
192 getme = int(getme)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
193 if getme < 0:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
194 return self[:(-1 * getme)]
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
195 else:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
196 return [self[getme-1]]
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
197 except IndexError:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
198 return []
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
199 except (ValueError, TypeError):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
200 getme = getme.strip()
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
201 mtch = self.rangeFrom.search(getme)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
202 if mtch:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
203 return self[(int(mtch.group(1))-1):]
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
204 if getme.startswith(r'/') and getme.endswith(r'/'):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
205 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
206 def isin(hi):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
207 return finder.search(hi)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
208 else:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
209 def isin(hi):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
210 return (getme.lower() in hi.lowercase)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
211 return [itm for itm in self if isin(itm)]
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
212
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
213
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
214