annotate cmd2.py @ 25:c99853267a44

introducing parameters
author devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
date Wed, 19 Dec 2007 15:37:45 -0500
parents 7a89805a47b1
children bb3fb24b6f5f
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
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
8
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
9 still to do:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
10 environment (maxrows, etc.)
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
11 edit
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
12
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
13 """
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
14 import cmd, re, os
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
15
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
16 class Cmd(cmd.Cmd):
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
17 caseInsensitive = True
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
18 multilineCommands = []
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
19 continuationPrompt = '> '
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
20 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'}
22
221095f3a4af all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 21
diff changeset
21 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
22 defaultExtension = 'txt'
25
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
23 settable = []
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
24 terminators = r';\n'
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
25 def do_cmdenvironment(self, args):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
26 self.stdout.write("""
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
27 Commands are %(casesensitive)scase-sensitive.
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
28 Commands may be terminated with: %(terminators)s
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
29 Settable parameters: %(settable)s
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
30 """ %
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
31 { 'casesensitive': 'not ' if self.caseInsensitive else '',
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
32 'terminators': ' '.join(self.terminators),
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
33 'settable': ' '.join(self.settable)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
34 })
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
35
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
36 def __init__(self, *args, **kwargs):
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
37 cmd.Cmd.__init__(self, *args, **kwargs)
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
38 self.history = History()
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
39
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
40 def precmd(self, line):
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
41 """Hook method executed just before the command line is
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
42 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
43
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
44 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
45 """
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
46 try:
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
47 (command, args) = line.split(None,1)
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
48 except ValueError:
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
49 (command, args) = line, ''
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
50 if self.caseInsensitive:
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
51 command = command.lower()
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
52 statement = ' '.join([command, args])
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
53 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
54 return statement
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
55 return self.finishStatement(statement)
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
56
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
57 def postcmd(self, stop, line):
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
58 """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
59 try:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
60 command = line.split(None,1)[0].lower()
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
61 if command not in self.excludeFromHistory:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
62 self.history.append(line)
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
63 finally:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
64 return stop
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
65
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
66 def finishStatement(self, firstline):
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
67 statement = firstline
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
68 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
69 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
70 return statement
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
71 # 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
72 # 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
73 # user input and users are slow.
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
74
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
75 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
76 """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
77
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
78 if self.use_rawinput:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
79 try:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
80 line = raw_input(prompt)
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
81 except EOFError:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
82 line = 'EOF'
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
83 else:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
84 self.stdout.write(prompt)
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
85 self.stdout.flush()
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
86 line = self.stdin.readline()
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
87 if not len(line):
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
88 line = 'EOF'
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
89 else:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
90 line = line[:-1] # chop \n
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
91 return line
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
92
22
221095f3a4af all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 21
diff changeset
93 def do_EOF(self, arg):
221095f3a4af all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 21
diff changeset
94 return True
221095f3a4af all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 21
diff changeset
95 do_eof = do_EOF
221095f3a4af all load done but a bad chop
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 21
diff changeset
96
25
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
97 statementEndPattern = re.compile(r'[%s]\s*$' % self.terminators)
17
25e908abf199 multiline into cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
98 def statementHasEnded(self, lines):
19
25f22e742a68 statement ends fixed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 18
diff changeset
99 return bool(self.statementEndPattern.search(lines))
25f22e742a68 statement ends fixed
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 18
diff changeset
100
25
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
101 def clean(self, s):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
102 """cleans up a string"""
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
103 if self.caseInsensitive:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
104 return s.strip().lower()
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
105 return s.strip()
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
106
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
107 def parseline(self, line):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
108 """Parse the line into a command name and a string containing
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
109 the arguments. Returns a tuple containing (command, args, line).
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
110 '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
111 """
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
112 line = line.strip()
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
113 if not line:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
114 return None, None, line
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
115 shortcut = self.shortcuts.get(line[0])
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
116 if shortcut and hasattr(self, 'do_%s' % shortcut):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
117 line = '%s %s' % (shortcut, line[1:])
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
118 i, n = 0, len(line)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
119 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
120 cmd, arg = line[:i], line[i:].strip()
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
121 return cmd, arg, line
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
122
25
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
123 def showParam(self, param):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
124 param = self.clean(param)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
125 if param in self.settable:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
126 val = getattr(self, param)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
127 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
128
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
129 def do_show(self, arg):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
130 'Shows value of a parameter'
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
131 if arg.strip():
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
132 self.showParam(arg)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
133 else:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
134 for param in self.settable:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
135 self.showParam(param)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
136
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
137 def cast(self, current, new):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
138 typ = type(current)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
139 if typ == bool:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
140 new = new.lower()
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
141 try:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
142 if (new=='on') or (new[0] in ('y','t')):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
143 return True
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
144 return False
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
145 except TypeError:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
146 None
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
147 try:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
148 return typ(new)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
149 except:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
150 print "Problem setting parameter (now %s) to %s; incorrect type?" % (current, new)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
151 return current
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
152
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
153 def do_set(self, arg):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
154 'Sets a parameter'
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
155 try:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
156 paramName, val = arg.split(None, 1)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
157 paramName = self.clean(paramName)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
158 if paramName not in self.settable:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
159 raise NotSettableError
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
160 currentVal = getattr(self, paramName)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
161 val = self.cast(currentVal, val.strip(self.terminators))
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
162 setattr(self, paramName, val)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
163 self.stdout.write(paramName, ' - was: %s\nnow: %s\n' % (currentVal, val))
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
164 except ValueError, AttributeError, NotSettableError:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
165 self.do_show(arg)
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
166
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
167 def do_shell(self, arg):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
168 'execute a command as if at the OS prompt.'
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
169 os.system(arg)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
170
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
171 def do_history(self, arg):
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
172 """history [arg]: lists past commands issued
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
173
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
174 no arg -> list all
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
175 arg is integer -> list one history item, by index
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
176 arg is string -> string search
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
177 arg is /enclosed in forward-slashes/ -> regular expression search
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
178 """
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
179 if arg:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
180 history = self.history.get(arg)
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
181 else:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
182 history = self.history
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
183 for hi in history:
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
184 self.stdout.write(hi.pr())
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
185 def last_matching(self, arg):
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
186 try:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
187 if arg:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
188 return self.history.get(arg)[-1]
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
189 else:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
190 return self.history[-1]
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
191 except:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
192 return None
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
193 def do_list(self, arg):
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
194 """list [arg]: lists last command issued
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
195
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
196 no arg -> list absolute last
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
197 arg is integer -> list one history item, by index
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
198 - arg, arg - (integer) -> list up to or after #arg
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
199 arg is string -> list last command matching string search
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
200 arg is /enclosed in forward-slashes/ -> regular expression search
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
201 """
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
202 try:
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
203 self.stdout.write(self.last_matching(arg).pr())
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
204 except:
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
205 pass
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
206 do_hi = do_history
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
207 do_l = do_list
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
208 do_li = do_list
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
209
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
210 def breakupStatements(self, txt):
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
211 """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
212 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
213 result = ['']
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
214 for line in txt.splitlines():
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
215 result[-1] += line
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
216 if self.statementHasEnded(result[-1]):
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
217 result.append('')
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
218
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
219 def do_load(self, fname):
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
220 """Runs command(s) from a file."""
24
7a89805a47b1 load cleaned up
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 23
diff changeset
221 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
222 try:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
223 self.stdin = open(fname, 'r')
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
224 except IOError, e:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
225 try:
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
226 self.stdin = open('%s.%s' % (fname, self.defaultExtension), 'r')
24
7a89805a47b1 load cleaned up
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 23
diff changeset
227 self.use_rawinput = False
7a89805a47b1 load cleaned up
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 23
diff changeset
228 self.prompt = self.continuationPrompt = ''
7a89805a47b1 load cleaned up
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 23
diff changeset
229 self.cmdloop()
7a89805a47b1 load cleaned up
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 23
diff changeset
230 self.stdin.close()
7a89805a47b1 load cleaned up
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 23
diff changeset
231 self.lastcmd = ''
7a89805a47b1 load cleaned up
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 23
diff changeset
232 except IOError:
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
233 print 'Problem opening file %s: \n%s' % (fname, e)
24
7a89805a47b1 load cleaned up
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 23
diff changeset
234 finally:
7a89805a47b1 load cleaned up
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 23
diff changeset
235 keepstate.restore()
7a89805a47b1 load cleaned up
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 23
diff changeset
236
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
237 class HistoryItem(str):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
238 def __init__(self, instr):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
239 str.__init__(self, instr)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
240 self.lowercase = self.lower()
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
241 self.idx = None
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
242 def pr(self):
21
8b55aaa52ce9 working on load, and preserving stdin/out
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 20
diff changeset
243 return '-------------------------[%d]\n%s\n' % (self.idx, str(self))
20
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
244
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
245 class History(list):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
246 rangeFrom = re.compile(r'^([\d])+\s*\-$')
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
247 def append(self, new):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
248 new = HistoryItem(new)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
249 list.append(self, new)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
250 new.idx = len(self)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
251 def extend(self, new):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
252 for n in new:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
253 self.append(n)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
254 def get(self, getme):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
255 try:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
256 getme = int(getme)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
257 if getme < 0:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
258 return self[:(-1 * getme)]
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
259 else:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
260 return [self[getme-1]]
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
261 except IndexError:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
262 return []
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
263 except (ValueError, TypeError):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
264 getme = getme.strip()
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
265 mtch = self.rangeFrom.search(getme)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
266 if mtch:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
267 return self[(int(mtch.group(1))-1):]
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
268 if getme.startswith(r'/') and getme.endswith(r'/'):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
269 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
270 def isin(hi):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
271 return finder.search(hi)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
272 else:
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
273 def isin(hi):
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
274 return (getme.lower() in hi.lowercase)
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
275 return [itm for itm in self if isin(itm)]
d6d64c2e3b98 shortcuts
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 19
diff changeset
276
25
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
277 class NotSettableError(Exception):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
278 None
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents:
diff changeset
279
25
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
280 class Statekeeper(object):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
281 def __init__(self, obj, attribs):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
282 self.obj = obj
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
283 self.attribs = attribs
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
284 self.save()
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
285 def save(self):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
286 for attrib in self.attribs:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
287 setattr(self, attrib, getattr(self.obj, attrib))
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
288 def restore(self):
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
289 for attrib in self.attribs:
c99853267a44 introducing parameters
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 24
diff changeset
290 setattr(self.obj, attrib, getattr(self, attrib))