annotate cmd2.py @ 20:d6d64c2e3b98

shortcuts
author devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
date Wed, 19 Dec 2007 11:49:23 -0500
parents 25f22e742a68
children 8b55aaa52ce9
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
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
7 """
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
8 import cmd, re, os
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
9
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
10 class Cmd(cmd.Cmd):
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
11 excludeFromHistory = '''run r list l history hi ed li'''.split()
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
12 caseInsensitive = True
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
13 multilineCommands = []
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
14 continuationPrompt = '> '
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
15 shortcuts = {'?': 'help', '!': 'shell'}
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
16 def __init__(self, *args, **kwargs):
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
17 cmd.Cmd.__init__(self, *args, **kwargs)
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
18 self.history = History()
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
19
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
20 def precmd(self, line):
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
21 """Hook method executed just before the command line is
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
22 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
23
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
24 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
25 """
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
26 try:
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
27 (command, args) = line.split(None,1)
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
28 except ValueError:
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
29 (command, args) = line, ''
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
30 if self.caseInsensitive:
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
31 command = command.lower()
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
32 statement = ' '.join([command, args])
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
33 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
34 return statement
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
35 return self.finishStatement(statement)
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
36
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
37 def postcmd(self, stop, line):
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
38 """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
39 try:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
40 command = line.split(None,1)[0].lower()
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
41 if command not in self.excludeFromHistory:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
42 self.history.append(line)
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
43 finally:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
44 return stop
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
45
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
46 def finishStatement(self, firstline):
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
47 statement = firstline
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
48 while not self.statementHasEnded(statement):
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
49 statement = '%s\n%s' % (statement, raw_input(self.continuationPrompt))
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
50 return statement
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
51 # 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
52 # 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
53 # user input and users are slow.
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
54
19
25f22e742a68 statement ends fixed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 18
diff changeset
55 statementEndPattern = re.compile(r'[\n;]\s*$')
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
56 def statementHasEnded(self, lines):
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
57 """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
58 Override for your own needs."""
19
25f22e742a68 statement ends fixed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 18
diff changeset
59 return bool(self.statementEndPattern.search(lines))
25f22e742a68 statement ends fixed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 18
diff changeset
60
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
61 def parseline(self, line):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
62 """Parse the line into a command name and a string containing
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
63 the arguments. Returns a tuple containing (command, args, line).
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
64 '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
65 """
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
66 line = line.strip()
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
67 if not line:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
68 return None, None, line
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
69 shortcut = self.shortcuts.get(line[0])
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
70 if shortcut and hasattr(self, 'do_%s' % shortcut):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
71 line = '%s %s' % (shortcut, line[1:])
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
72 i, n = 0, len(line)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
73 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
74 cmd, arg = line[:i], line[i:].strip()
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
75 return cmd, arg, line
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
76
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
77 def do_shell(self, arg):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
78 'execute a command as if at the OS prompt.'
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
79 os.system(arg)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
80
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
81 def do_history(self, arg):
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
82 """history [arg]: lists past commands issued
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
83
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
84 no arg -> list all
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
85 arg is integer -> list one history item, by index
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
86 arg is string -> string search
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
87 arg is /enclosed in forward-slashes/ -> regular expression search
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
88 """
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
89 if arg:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
90 history = self.history.get(arg)
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
91 else:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
92 history = self.history
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
93 for hi in history:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
94 hi.pr()
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
95 def last_matching(self, arg):
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
96 try:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
97 if arg:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
98 return self.history.get(arg)[-1]
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
99 else:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
100 return self.history[-1]
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
101 except:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
102 return None
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
103 def do_list(self, arg):
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
104 """list [arg]: lists last command issued
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
105
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
106 no arg -> list absolute last
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
107 arg is integer -> list one history item, by index
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
108 - arg, arg - (integer) -> list up to or after #arg
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
109 arg is string -> list last command matching string search
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
110 arg is /enclosed in forward-slashes/ -> regular expression search
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 try:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
113 self.last_matching(arg).pr()
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
114 except:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
115 pass
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
116 do_hi = do_history
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
117 do_l = do_list
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
118 do_li = do_list
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
119
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
120 class HistoryItem(str):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
121 def __init__(self, instr):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
122 str.__init__(self, instr)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
123 self.lowercase = self.lower()
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
124 self.idx = None
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
125 def pr(self):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
126 print '-------------------------[%d]' % (self.idx)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
127 print self
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
128
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
129 class History(list):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
130 rangeFrom = re.compile(r'^([\d])+\s*\-$')
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
131 def append(self, new):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
132 new = HistoryItem(new)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
133 list.append(self, new)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
134 new.idx = len(self)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
135 def extend(self, new):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
136 for n in new:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
137 self.append(n)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
138 def get(self, getme):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
139 try:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
140 getme = int(getme)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
141 if getme < 0:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
142 return self[:(-1 * getme)]
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
143 else:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
144 return [self[getme-1]]
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
145 except IndexError:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
146 return []
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
147 except (ValueError, TypeError):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
148 getme = getme.strip()
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
149 mtch = self.rangeFrom.search(getme)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
150 if mtch:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
151 return self[(int(mtch.group(1))-1):]
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
152 if getme.startswith(r'/') and getme.endswith(r'/'):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
153 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
154 def isin(hi):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
155 return finder.search(hi)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
156 else:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
157 def isin(hi):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
158 return (getme.lower() in hi.lowercase)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
159 return [itm for itm in self if isin(itm)]
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
160
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
161
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
162