annotate cmd2.py @ 42:05c20d6bcec4

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