Mercurial > sqlpython
annotate cmd2.py @ 22:221095f3a4af
all load done but a bad chop
author | devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil |
---|---|
date | Wed, 19 Dec 2007 14:21:30 -0500 |
parents | 8b55aaa52ce9 |
children | 07aae987b754 |
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'} |
22
221095f3a4af
all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
21
diff
changeset
|
20 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
|
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 |
22
221095f3a4af
all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
21
diff
changeset
|
79 def do_EOF(self, arg): |
221095f3a4af
all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
21
diff
changeset
|
80 return True |
221095f3a4af
all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
21
diff
changeset
|
81 do_eof = do_EOF |
221095f3a4af
all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
21
diff
changeset
|
82 |
19
25f22e742a68
statement ends fixed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
18
diff
changeset
|
83 statementEndPattern = re.compile(r'[\n;]\s*$') |
17
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
84 def statementHasEnded(self, lines): |
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
85 """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
|
86 Override for your own needs.""" |
19
25f22e742a68
statement ends fixed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
18
diff
changeset
|
87 return bool(self.statementEndPattern.search(lines)) |
25f22e742a68
statement ends fixed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
18
diff
changeset
|
88 |
20
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
89 def parseline(self, line): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
90 """Parse the line into a command name and a string containing |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
91 the arguments. Returns a tuple containing (command, args, line). |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
92 '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
|
93 """ |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
94 line = line.strip() |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
95 if not line: |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
96 return None, None, line |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
97 shortcut = self.shortcuts.get(line[0]) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
98 if shortcut and hasattr(self, 'do_%s' % shortcut): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
99 line = '%s %s' % (shortcut, line[1:]) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
100 i, n = 0, len(line) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
101 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
|
102 cmd, arg = line[:i], line[i:].strip() |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
103 return cmd, arg, line |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
104 |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
105 def do_shell(self, arg): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
106 'execute a command as if at the OS prompt.' |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
107 os.system(arg) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
108 |
16
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
109 def do_history(self, arg): |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
110 """history [arg]: lists past commands issued |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
111 |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
112 no arg -> list all |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
113 arg is integer -> list one history item, by index |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
114 arg is string -> string search |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
115 arg is /enclosed in forward-slashes/ -> regular expression search |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
116 """ |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
117 if arg: |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
118 history = self.history.get(arg) |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
119 else: |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
120 history = self.history |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
121 for hi in history: |
21
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
122 self.stdout.write(hi.pr()) |
16
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
123 def last_matching(self, arg): |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
124 try: |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
125 if arg: |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
126 return self.history.get(arg)[-1] |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
127 else: |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
128 return self.history[-1] |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
129 except: |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
130 return None |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
131 def do_list(self, arg): |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
132 """list [arg]: lists last command issued |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
133 |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
134 no arg -> list absolute last |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
135 arg is integer -> list one history item, by index |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
136 - arg, arg - (integer) -> list up to or after #arg |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
137 arg is string -> list last command matching string search |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
138 arg is /enclosed in forward-slashes/ -> regular expression search |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
139 """ |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
140 try: |
21
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
141 self.stdout.write(self.last_matching(arg).pr()) |
16
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
142 except: |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
143 pass |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
144 do_hi = do_history |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
145 do_l = do_list |
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
146 do_li = do_list |
21
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
147 |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
148 def breakupStatements(self, txt): |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
149 """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
|
150 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
|
151 result = [''] |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
152 for line in txt.splitlines(): |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
153 result[-1] += line |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
154 if self.statementHasEnded(result[-1]): |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
155 result.append('') |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
156 |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
157 def do_load(self, fname): |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
158 """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
|
159 stdin = self.stdin |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
160 try: |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
161 self.stdin = open(fname, 'r') |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
162 except IOError, e: |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
163 try: |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
164 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
|
165 except: |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
166 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
|
167 self.stdin = stdin |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
168 return |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
169 use_rawinput = self.use_rawinput |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
170 self.use_rawinput = False |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
171 self.cmdloop() |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
172 self.stdin.close() |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
173 self.stdin = stdin |
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
174 self.use_rawinput = use_rawinput |
20
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
175 |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
176 class HistoryItem(str): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
177 def __init__(self, instr): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
178 str.__init__(self, instr) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
179 self.lowercase = self.lower() |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
180 self.idx = None |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
181 def pr(self): |
21
8b55aaa52ce9
working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
20
diff
changeset
|
182 return '-------------------------[%d]\n%s\n' % (self.idx, str(self)) |
20
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
183 |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
184 class History(list): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
185 rangeFrom = re.compile(r'^([\d])+\s*\-$') |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
186 def append(self, new): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
187 new = HistoryItem(new) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
188 list.append(self, new) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
189 new.idx = len(self) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
190 def extend(self, new): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
191 for n in new: |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
192 self.append(n) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
193 def get(self, getme): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
194 try: |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
195 getme = int(getme) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
196 if getme < 0: |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
197 return self[:(-1 * getme)] |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
198 else: |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
199 return [self[getme-1]] |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
200 except IndexError: |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
201 return [] |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
202 except (ValueError, TypeError): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
203 getme = getme.strip() |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
204 mtch = self.rangeFrom.search(getme) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
205 if mtch: |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
206 return self[(int(mtch.group(1))-1):] |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
207 if getme.startswith(r'/') and getme.endswith(r'/'): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
208 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
|
209 def isin(hi): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
210 return finder.search(hi) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
211 else: |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
212 def isin(hi): |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
213 return (getme.lower() in hi.lowercase) |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
214 return [itm for itm in self if isin(itm)] |
d6d64c2e3b98
shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
19
diff
changeset
|
215 |
17
25e908abf199
multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
16
diff
changeset
|
216 |
16
2776755a3a7e
beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff
changeset
|
217 |